Home Test Your Knowledge TCS Digital Coding Questions Previous Year

TCS Digital Coding Questions Previous Year

by Anup Maurya
9 minutes read

This article will explore some TCS Digital Coding Questions from prior TCS Digital Advanced Coding Questions round from the previous drives.

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

Question 2

Problem Description:  Given two non-negative integers n1 and n2, where n1

For example:

Suppose n1=11 and n2=15.

There is the number 11, which has repeated digits, but 12, 13, 14 and 15 have no repeated digits. So, the output is 4.

Example1:

Input:

  • 11 — Vlaue of n1
  • 15 — value of n2

Output:

  • 4

Example 2:

Input:

  • 101 — value of n1
  • 200 — value of n2

Output:

  • 72

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 Description: Roco is an island near Africa which is very prone to forest fire. Forest fire is such that it destroys the complete forest. Not a single tree is left.This island has been cursed by God , and the curse is that whenever a tree catches fire, it passes the fire to all its adjacent tree in all 8 directions,North, South, East, West, North-East, North-West, South-East, and South-West.And it is given that the fire is spreading every minute in the given manner, i.e every tree is passing fire to its adjacent tree.Suppose that the forest layout is as follows where T denotes tree and W denotes water.

Your task is that given the location of the first tree that catches fire, determine how long would it take for the entire forest to be on fire. You may assume that the lay out of the forest is such that the whole forest will catch fire for sure and that there will be at least one tree in the forest

Input Format:

  • First line contains two integers, M, N, space separated, giving the size of the forest in terms of the number of rows and columns respectively.
  • The next line contains two integers X,Y, space separated, giving the coordinates of the first tree that catches the fire.
  • The next M lines, where ith line containing N characters each of which is either T or W, giving the position of the Tree and Water in the  ith row of the forest.

Output Format:

Single integer indicating the number of minutes taken for the entire forest to catch fire

Constrains:

  • 3 ≤ M ≤ 20
  • 3 ≤ N ≤ 20

Sample Input 1:

3 3
W T T
T W W
W T T
Sample Output 1:

5

Explanation:
In the second minute,tree at (1,2) catches fire,in the third minute,the tree at (2,1) catches fire,fourth minute tree at (3,2) catches fire and in the fifth minute the last tree at (3,3) catches fire.
Sample Input 2:
6 6
1 6
W T T T T T
T W W W W W
W T T T T T
W W W W W T
T T T T T T
T W W W W W

Sample Output 2:

16

Question 5

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

Question 6

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 7

Problem Statement: Write a program to find the count of numbers that consists of unique digits.

Input Format:

Input consists of two Integer lower and upper values of a range

Output Format:

The output consists of a single line and prints the count of unique digits in a given range. Else Print”No Unique Number”

Sample Input 1:

10
15

Sample Output 1:

5

Question 8

Problem Statement: There is a range given n and m in which we have to find the count of all the prime pairs whose difference is 6. We have to find how many sets are there within a given range.

Output:

The output consists of a single line and prints the count of prime pairs in a given range. Else print”No Prime Pairs”.

Constraints:

2<=n<=1000

n<=m<=2000

Sample Input 1:

4

30

Sample Output 1:

6

Explanation:

(5, 11) (7, 13) (11, 17) (13, 19) (17, 23) (23, 29). we have 6 prime pairs.

Question 9

Problem Statement: Write a program to print all the combinations of the given word with or without meaning (when unique characters are given).

Sample Input 1:

abc

Sample Output 1:

abc

acb

bac

bca

cba

cab

Question 10

Problem Statement: Bastin once had trouble finding the numbers in a string. The numbers are distributed in a string across various test cases. There are various numbers in each test case you need to find the number in each test case. Each test case has various numbers in sequence. You need to find only those numbers which do not contain 9. For eg, if the string contains “hello this is alpha 5051 and 9475”.You will extract 5051 and not 9475. You need only those numbers which are consecutive and you need to help him find the numbers. Print the largest number.

Note: Use long long for storing the numbers from the string.

Input:

The first line consists of T test cases and the next T lines contain a string.

Output:

For each string output the number stored in that string if various numbers are there print the largest one. If a string has no numbers print -1.

Constraints:

1<=T<=100

1<=|S|<=10000

Example:

Sample Input 1:

1

This is alpha 5057 and 97

Sample Output 1:

5057

Sample Input 2:

1

dream job 100 and 101

Sample Output 1:

101

Question 11

Problem Statement: A group of friends are playing cards. Only numeric cards are being used, along with the joker card (equivalent to 0), and the symbols in the cards are not taken into account. Each player will receive a set of cards. The rule of the game is to rearrange the set of cards to the possible lowest number sequence. The player with the set of cards forming the smallest number wins the game. The number sequence of cards forming the smallest number wins the game. The number sequence of the cards should not start with a joker card. The task is to write a program for developing the logic of the game considering the set of cards as a number. The output will be the lowest possible number that can be formed not beginning with 0.

Example 1:

Sample Input 1:

34201

Sample Output 1:

1234

Explanation:

The lowest number formation by rearranging the digits of the number 34201 and not starting with a 0 will be 1234

related posts

Leave a Comment

Enable Notifications OK No thanks