TCS ITP Advanced coding questions

TCS ITP Advanced Coding Questions

Following page contains TCS ITP Advance Coding Questions with Answers for freshers, along with sectional details, importance, difficulty etc.

The TCS Integrated test Pattern allows you to showcase your talent and skills and based on your test performance, you will either be shortlisted for Prime Interviews or Digital Interviews.

Here below you will find similar type programming questions that are asked constantly in TCS ITP test.

TCS ITP Coding Questions

Question 1

Problem Description -: Given an array Arr[ ] of N integers and a positive integer K. The task is to cyclically rotate the array clockwise by K.

Note : Keep the first of the array unaltered.

Example 1:

  • 5  —Value of N
  • {10, 20, 30, 40, 50}  —Element of Arr[ ]
  • 2  —–Value of K

Output :

40 50 10 20 30

Example 2:

  • 4  —Value of N
  • {10, 20, 30, 40}  —Element of Arr[]
  • 1  —–Value of K

Output :

40 10 20 30

Output :

4 5 1 2 3
Get placed in TCS with PrepInsta. Use Coupon Code CT10 and get flat 10% off!!

Question 2

Problem Statement-:

Identify the logic behind the series

6 28 66 120 190 276….

The numbers in the series should be used to create a Pyramid. The base of the Pyramid will be the widest and will start converging towards the top where there will only be one element. Each successive layer will have one number less than that on the layer below it. The width of the Pyramid is specified by an input parameter N. In other words there will be N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows

  1. First number in the series should be at the top of the Pyramid
  2. Last N number of the series should be on the bottom-most layer of the Pyramid, with Nthnumber being the right-most number of this layer.
  3. Numbers less than 5-digits must be padded with zeroes to maintain the sanctity of a Pyramid when printed. Have a look at the examples below to get a pictorial understanding of what this rule actually means.

Example

If input is 2, output will be

  • 00006
  • 00028 00066

If input is 3, output will be

  • 00006
  • 00028 00066
  • 00120 00190 00276

Formal input and output specifications are stated below

Input Format:

  • First line of input will contain number N that corresponds to the width of the bottom-most layer of the Pyramid

Output Format:

  • The Pyramid constructed out of numbers in the series as per stated construction rules

Constraints:

  • 0 < N <= 14

 

Output :

00006 
00028 00066 

Question 3

Problem Description -:  In this 3 Palindrome, Given an input string word, split the string into exactly 3 palindromic substrings. Working from left to right, choose the smallest split for the first substring that still allows the remaining word to be split into 2 palindromes.

Similarly, choose the smallest second palindromic substring that leaves a third palindromic substring.

If there is no way to split the word into exactly three palindromic substrings, print “Impossible” (without quotes). Every character of the string needs to be consumed.

Cases not allowed –

  • After finding 3 palindromes using above instructions, if any character of the original string remains unconsumed.
  • No character may be shared in forming 3 palindromes.

Constraints

  • 1 <= the length of input sting <= 1000

Input

  • First line contains the input string consisting of characters between [a-z].

Output

  • Print 3 substrings one on each line.

Time Limit

1

Examples

Example 1

Input

nayannamantenet

Output

nayan

naman

tenet

Explanation

  • The original string can be split into 3 palindromes as mentioned in the output.
  • However, if the input was nayanamantenet, then the answer would be “Impossible”.

Example 2

Input

aaaaa

Output

a

a

aaa

Explanation

  • The other ways to split the given string into 3 palindromes are as follows –
  • [a, aaa, a], [aaa, a, a], [aa, aa, a], etc.
  • Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, a, aaa].

Example 3

Input

aaaabaaaa

Output

a

aaabaaa

a

Explanation

  • The other ways to split the given string into 3 palindromes are as follows –
  • [aaaa, b, aaaa], [aa, aabaa, aa], etc.
  • Since we want to minimize the length of the first palindromic substring using left to right processing, the correct way to split is [a, aaabaaa, a].

Question 4

Problem Statement -: In this even odd problem Given a range [low, high] (both inclusive), select K numbers from the range (a number can be chosen multiple times) such that sum of those K numbers is even.

Calculate the number of all such permutations.

As this number can be large, print it modulo (1e9 +7).

Constraints

  • 0 <= low <= high <= 10^9
  • K <= 10^6.

Input

  • First line contains two space separated integers denoting low and high respectively
  • Second line contains a single integer K.

Output

  • Print a single integer denoting the number of all such permutations

Time Limit

1

Examples

Example 1

Input

4 5

3

Output

4

Explanation

There are 4 valid permutations viz. {4, 4, 4}, {4, 5, 5}, {5, 4, 5} and {5, 5, 4} which sum up to an even number.

Example 2

Input

1 10

2

Output

50

Explanation

There are 50 valid permutations viz. {1,1}, {1, 3},.. {1, 9} {2,2}, {2, 4},… {2, 10} . . . {10, 2}, {10, 4},… {10, 10}. These 50 permutations, each sum up to an even number.

Question 5

Problem Statement:- Jaya invented a Time Machine and wants to test it by time-traveling to visit Russia on the Day of Programmer (the 256thday of the year) during a year in the inclusive range from 1700 to 2700. From 1700 to 1917 , Russia’s official calendar was the Julian Calendar since 1919 they used the Gregorian calendar system. The transition from the Julian to Gregorian calendar system occurred in 1918 , when the next day after 31 January was February 14 . This means that in 1918, February 14 was the 32nd day of the year in Russia. In both calendar systems, February is the only month with a variable amount of days; it has 29 days during a leap year, and 28 days during all other years. In the Julian calendar, leap years are divisible by 4 ; in the Gregorian calendar, leap years are either of the following:

  • Divisible by 400
  • Divisible by 4 and not divisible by 100

Given a year, y, find the date of the 256th day of that year according to the official Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year is 1984.1984 is divisible by 4, so it is a leap year. The 256 day of a leap year after 1918 is September 12, so the answer is 12.9.1984. 

Function Description

  • Complete the programmerday function in the editor below. It should return a string representing the date of the 256th day of the year given.
  • programmerday has the following parameter(s):
    • year: an integer 

Input Format

  • A single integer denoting year y.

Output Format

  • Print the full date of programmerday during year y in the format dd.mm.yyyy, where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Sample Input

           2017

Sample Output

           13.09.2017

Question 6

Problem Description

Question -: A positive integer d is said to be a factor of another positive integer N if when N is divided by d, the remainder obtained is zero. For example, for number 12, there are 6 factors 1, 2, 3, 4, 6, 12. Every positive integer k has at least two factors, 1 and the number k itself.Given two positive integers N and k, write a program to print the kth largest factor of N.

Input Format: The input is a comma-separated list of positive integer pairs (N, k).

Output Format: The kth highest factor of N. If N does not have k factors, the output should be 1.

Constraints:

  • 1<N<10000000000
  • 1<k<600.

You can assume that N will have no prime factors which are larger than 13.

Example 1

  • Input: 12,3
  • Output: 4

Explanation: N is 12, k is 3. The factors of 12 are (1,2,3,4,6,12). The highest factor is 12 and the third largest factor is 4. The output must be 4.

Example 2

  • Input: 30,9
  • Output: 1

Explanation: N is 30, k is 9. The factors of 30 are (1,2,3,5,6,10,15,30). There are only 8 factors. As k is more than the number of factors, the output is 1.

Question 7

Problem Statement:- In a Conference ,attendees are invited for a dinner after the conference.The Co-ordinator,Sagar arranged around round tables for dinner and want to have an impactful seating experience for the attendees.Before finalizing the seating arrangement,he wants to analyze all the possible arrangements.These are R round tables and N attendees.In case where N is an exact multiple of R,the number of attendees must be exactly N//R,,If N is not an exact multiple of R, then the distribution of attendees must be as equal as possible.Please refer to the example section before for better understanding.
For example, R = 2 and N = 3
All possible seating arrangements are
(1,2) & (3)
(1,3) & (2)
(2,3) & (1)
Attendees are numbered from 1 to N.

Input Format:

  • The first line contains T denoting the number of test cases.
  • Each test case contains two space separated integers R and N, Where R denotes the number of round tables and N denotes the number of attendees.

Output Format:

Single Integer S denoting the number of possible unique arrangements.

Constraints:

  • 0 <= R <= 10(Integer)
  • 0 < N <= 20 (Integer)
Sample Input 1:
1
2 5
Sample Output 1:

10

Explanation:

R = 2, N = 5

(1,2,3) & (4,5)

(1,2,4) & (3,5)

(1,2,5) & (3,4)

(1,3,4) & (2,5)

(1,3,5) & (2,4)

(1,4,5) & (2,3)

(2,3,4) & (1,5)

(2,3,5) & (1,4)

(2,4,5) & (1,3)

(3,4,5) & (1,2)

Arrangements like

(1,2,3) & (4,5)

(2,1,3) & (4,5)

(2,3,1) & (4,5) etc.

But as it is a round table,all the above arrangements are same.

Question 8

Problem Description

In the theory of numbers, square free numbers have a special place.  A square free number is one that is not divisible by a perfect square (other than 1).  Thus 72 is divisible by 36 (a perfect square), and is not a square free number, but 70 has factors 1, 2, 5, 7, 10, 14, 35 and 70.  As none of these are perfect squares (other than 1), 70 is a square free number.

For some algorithms, it is important to find out the square free numbers that divide a number.  Note that 1 is not considered a square free number. 

In this problem, you are asked to write a program to find the number of square free numbers that divide a given number.

Input:-

  • The only line of the input is a single integer N which is divisible by no prime number larger than 19.

Output:-

  • One line containing an integer that gives the number of square free numbers (not including 1).

Constraints:-

  • N   < 10^9

Complexity:-

  • Simple

Time Limit:-

  • 1

Examples


Example 1

  • Input:-
    20

  • Output:-
    3

Explanation

N=20

If we list the numbers that divide 20, they are

1, 2, 4, 5, 10, 20

1 is not a square free number, 4 is a perfect square, and 20 is divisible by 4, a perfect square.  2 and 5, being prime, are square free, and 10 is divisible by 1,2,5 and 10, none of which are perfect squares.  Hence the square free numbers that divide 20 are 2, 5, 10.  Hence the result is 3.

Example 2

  • Input:-
    72

  • Output:-
    3

Explanation

N=72.  The numbers that divide 72 are

1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72

1 is not considered square free.   4, 9 and 36 are perfect squares, and 8,12,18,24 and 72 are divisible by one of the.  Hence only 2, 3 and 6 are square free.  (It is easily seen that none of them are divisible by a perfect square).  The result is 3.

Question 9

Problem Statement

Find the minimum number of coins required to form any value between 1 to N,both inclusive.Cumulative value of coins should not exceed N. Coin denominations are 1 Rupee, 2 Rupee and 5 Rupee.Let’s Understand the problem using the following example. Consider the value of N is 13, then the minimum number of coins required to formulate any value between 1 and 13, is 6. One 5 Rupee, three 2 Rupee and two 1 Rupee coins are required to realize any value between 1 and 13. Hence this is the answer.However, if one takes two 5 Rupee coins, one 2 rupee coin and two 1 rupee coin, then too all values between 1 and 13 are achieved. But since the cumulative value of all coins equals 14, i.e., exceeds 13, this is not the answer.

  • Input Format:
    • A single integer value.
  • Output Format:
    • Four space separated integer values.
      • 1st – Total number of coins.
      • 2nd – number of 5 Rupee coins.
      • 3rd – number of 2 Rupee coins.
      • 4th – number of 1 Rupee coins.
  • Constraints:
    • 0 < n < 1000

Refer the sample output for formatting

Sample Input

    13

Sample Output

   6 1 3 2

Explanation

  • The minimum number of coins required is 6 with in it:
    • minimum number of 5 Rupee coins = 1
    • minimum number of 2 Rupee coins = 3
    • minimum number of 1 Rupee coins = 2

Using these coins, we can form any value with in the given value and itself, like below:

Here the given value is 13

  • For 1 = one 1 Rupee coin
  • For 2 = one 2 Rupee coin
  • For 3 = one 1 Rupee coin and one 2 Rupee coins
  • For 4 = two 2 Rupee coins
  • For 5 = one 5 Rupee coin
  • For 6 = one 5 Rupee and one 1 Rupee coins
  • For 7 = one 5 Rupee and one 2 Rupee coins
  • For 8 = one 5 Rupee, one 2 Rupee and one 1 Rupee coins
  • For 9 = one 5 Rupee and two 2 Rupee coins
  • For 10 = one 5 Rupee, two 2 Rupee and one 1 Rupee coins
  • For 11 = one 5 Rupee, two 2 Rupee and two 1 Rupee coins
  • For 12 = one 5 Rupee, three 2 Rupee and one 1 Rupee coins
  • For 13 = one 5 Rupee, three 2 Rupee and two 1 Rupee coins

Question 10

Problem Statement:-

Compute the nearest larger number by interchanging its digits updated.Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1.

  • Input Format
    2 numbers a and b, separated by space.
  • Output Format
    A single number greater than b.

    If not possible, print -1

  • Constraints

    1 <= a,b <= 10000000

Example 1:

Sample Input:

    459 500

Sample Output:
    549

Example 2:

Sample Input:

    645757 457765

Sample Output:
    465577

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription