# TCS Ninja Coding Questions and Answers 2023

## TCS Ninja Coding Questions and Answers 2023

TCS Ninja coding questions answers 2023 is available here. TCS Ninja coding questions paper with solutions is very important for TCS ninja exam. Also, you will find TCS ninja coding questions with solved papers. On PrepInsta you will find the Accurate TCS Ninja coding exam Pattern 2023.

• No Of Questions:- 2 Question
• Time – 45 mins ## TCS Ninja Coding Questions With Solutions

### TCS Coding Question Day 1 Slot 1 – Question 1

Mike has arranged a small party for the inauguration of his new startup. He has invited all of his fellow employees who are N in number. These employees are indexed with an array starting from 1 to N. In this startup, everyone knows each other’s salary. We will represent salary by an integer value. Mike has to arrange tables, where he will accommodate everyone. But he is a little thrifty in that, he wants to adjust everyone in as few tables as he can. Tables of various seating are available. Let’s say the cost of renting each table is K. All the employees have to seat in the order of the index. The only problem is that the employees with the same salary can get into arguments which can ruin the party.
Mike came across the term inefficiency of arrangement, which can be defined as the sum of the cost of tables + the total number of people getting into arguments. Given the number of employees, N, and their salaries in array a[ ], he wants to find the optimal inefficiency, i.e., the smallest possible value for the inefficiency of arranging the N employees.

Let’s understand it with an example.
Number of employees invited N = 5
A a = {5 1 3 3 3}
K = 1

Now let’s check all the combinations and how in-efficient is all of them.

When we make 1st, 2nd, and 3rd employee on table-1 and 4th and 5th on table-2
Cost of 2 tables = 2*1
Number of people getting into arguments = 2 (two 3’s: 4th and 5th employee)
Total = 2 + 2 = 4

When we make 1st, 2nd, 3rd, and 4th employees on table-1 and 5th on table-2
Cost of 2 tables = 2*1
Number of people getting into arguments = 2 (two 3’s: 4th and 5th employee)
Total = 2 + 2 = 4

When we make all of them sit at 1 table, then inefficiency will be
Cost of 1 table = 1
Number of people getting into arguments = 3 (all 3’s: 3rd, 4th and 5th employee)
Total = 1 + 3 = 4

When we make 1st, 2nd and 3rd employee on table-1 and 4th on table-2 and 5th on table-3
Cost of 3 tables = 3*1
Number of people getting into arguments = 0 (all 3’s are. sitting at different tables)
Total = 3 + 0 = 3

Hence the optimal in-efficiency is 3.
So, the output will be 3.

Example 1:

• Input:
5 1 -> Input Integer, N and K
{5, 1, 3, 3, 3) Input Integer, array elements a[i].
• Output:
3 -> Output
• Explanation:
Below is the seating for each case:

Case 1:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th and 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*1 + 2 = 4

Case 2:
Table 1: 1st, 2nd, 3rd, and 4th
Table 2: 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*1 + 2 = 4

Case 3:
Table 1: 1st, 2nd, 3rd, 4th, and 5th
Number of people getting into arguments: 3
Total in-efficiency: 1*1 + 3 = 4

Case 4:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th
Table 3: 5th
Number of people getting into arguments: 0
Total in-efficiency: 3*1 + 0 = 3

Choosing the minimum which is 3.

Example 2:

• Input:
5 14 -> Input Integer, N and K.
{1, 4, 2, 4, 4} -> Input Integer, array elements a[i].
• Output:
17 -> Output
• Explanation:
Below is the seating for each case:

Case 1:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th and 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*14 + 2 = 30

Case 2:
Table 1: 1st, 2nd, 3rd, and 4th
Table 2: 5th
Number of people getting into arguments: 2
Total in-efficiency: 2*14 + 2 = 30

Case 3:
Table 1: 1st, 2nd, 3rd, 4th, and 5th
Number of people getting into arguments: 3
Total in-efficiency: 1*14+3 = 17

Case 4:
Table 1: 1st, 2nd, and 3rd
Table 2: 4th
Table 3: 5th
Number of people getting into arguments: 3
Total in-efficiency: 3*14+ 0 = 42

Chose the minimum which is 17.

### TCS Coding Question Day 1 Slot 1 – Question 2

Jack and Jill are playing a string game. Jack has given Jill two strings A and B. Jill has to derive a string C from A, by deleting elements from string A, such that string C does not contain any element of string B. Jill needs help to do this task. She wants a program to do this as she is lazy. Given strings A and B as input, give string C as Output.

Example 1:

• Input:
tiger     -> input string A
ti          -> input string B
• Output:
ger       -> Output string C
• Explanation:
After removing “t” and “i” from “tiger”, we are left with “ger”.

Example 2:

• Input:
processed     -> input string A
esd                -> input string B
• Output:
proc               -> Output string C
• Explanation:
After removing “e” “s” and “d” from “processed”, we are left with “proc”.

Example 3:

• Input:
talent        -> input string A
tens          -> input string B
• Output:
al              -> Output string C

Explanation:
After removing “t” “e” and “n” from “talent”, we are left with “al”.

### TCS Coding Question Day 1 Slot 2 – Question 1

Mahesh and Suresh are playing a new game “Checkers“. This is a very simple game but becomes challenging when more expert players are playing. Below is the description of the game and rules: The game is played by 2 players. This game consists of an N*M matrix. Each of the cells is background lit by lights. And these cells are either Green or Black. The green and black cells are randomly lit and will be represented with 1’s and 0’s respectively. Green cells are the cells that need to be captured. Black cells cannot be captured. Everyone is in the race to capture the maximum number of cells possible.
In a single chance, a player can capture all those adjacent cells which share an edge. Once there is no adjacent edge the chance breaks and the next player will play.
Mahesh always starts the game and Suresh is second.
Both players are playing optimally, find out how many cells Suresh captures.

Input:
N and M, size of the matrix
A[i][j] for all 1<=i<=N and 1<=j<=M

Let us try to understand it with an example

Consider the matrix below
N = 4
M = 4
A = 1001
0110
0110
1001

If Mahesh plays first, he will try to capture most of the 1’s, he will capture A, A, A, and A. Now there are no adjacent cells left. So, the chance will be given to Suresh. Now Suresh’s turn. He can capture either A or A or A or A. He will capture any one cell, and as there is no adjacent deft, the chance will now be given to Mahesh. The game proceeds and then again Suresh’s turn will come, and he will again be able to choose only 1 cell finally Mahesh will end the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured only 2 cells.

Example 1:

• Input:
2 2       -> Input integer, N, M
1 1       -> Input integer, A[i]
1 1       -> Input integer, A[N]
• Output:
0          -> Output
• Explanation:
In the above scenario, it is very clear that if Mahesh plays first, he will capture all the cells as all the cells are adjacent to each other.
There will be nothing left for Suresh. Hence the cells captured by Suresh will be 0.

Example 2:

• Input:
4 4             -> Input integer, N, M
1001          -> Input integer, A[i]
0110          -> Input integer, A[i+1]
0110          -> Input integer, A[i+2]
1001          -> Input integer, A[N]
• Output:
2                -> Output
• Explanation:
If Mahesh plays first, he will try to cover most of the 1’s, he will cover A, A, A, and A. Now there are no adjacent cells left. So, the chance will be given to Suresh. Now Suresh’s turn. He can capture either of A or A or A or A. He will capture any one cell, and as there is no adjacent left, the chance will now be given to Mahesh. The game proceeds and then again Suresh’s turn will come, and he will again be able to choose only 1 cell, and finally, Mahesh will end the game by choosing the final cell.
Like this Mahesh has captured 6 cells and Suresh has captured only 2 cells.

### TCS Coding Question Day 1 Slot 2 – Question 2

Joe was reading an interesting novel when all of a sudden, his 5-year-old son came to him and started asking a few questions about functions. He tried making him understand various functions, but his son didn’t get find it interesting. Then he created his function Absent number function A(S) According to this function, there is always the smallest positive integer number in a sequence that is not available. In simple words, if you sort the given sequence, then the smallest integer number (other than 0) which is not present in the sequence is the Absent number. Consider a sequence S= [1, 2, 3], then B(S)=4. The minimum value greater than 0 which is not present here in the sequence is 4. Now his son found it interesting, so Joe extended this logic to sub-sequence. If there is a given sequence S, you have to find the Absent Number for each sub-sequence and then sum it up. If the answer is large, print the result modulo, 109 +7.

Let say there exist a sequence with N = 3, and sequence S = [1, 2, 1]
Below are the various sub-sequences of it,

It will be 2N:

[ ] : B([ ]) = 1
 : B() = 2
 : B() = 1
 : B() = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3

Total sum of all B(S) = 1+2+1+2+3+3+2+3 = 17.

Example 1:

• Input:
2 -> input Integer, N
1 1 -> input Integer, S
• Output:
7 -> Output
• Explanation:
In the above scenario below are the various sub-sequence and their respective functions of it:
[ ] : B(l) = 1
: B()= 2
: B() = 2
[1,1]: B([1,1]) = 2
Total sum of all B(S) = 1+2+2+2 = 7 Hence the answer is 7.

Example 2:

• Input:
3 -> Input integer, N
1 2 1 -> Input integer, S
• Output:
17->Output
• Explanation:
In the above scenario below are the various sub-sequences and their respective functions of it.
[ ] : B([ ]) = 1
 : B() = 2
 : B() =1
 : B() = 2
[1, 2] : B([1, 2]) = 3
[2, 1] : B([2, 1]) = 3
[1, 1] : B([1, 1]) = 2
[1, 2, 1] : B([1, 2, 1]) = 3
Total sum of all B(S) = 1 + 2 + 1 + 2 + 3 + 3 + 2 + 3 = 17.

### TCS Coding Question Day 2 Slot 1 – Question 1

James has a sequence of N numbers. There is also an integer X which is a random number from other sources. He is allowed to perform a specific operation on this sequence X number of times. Below is the operation:

• Pick exactly one element from the sequence and multiply it with -1.

Your task is to find out the number of different sequences which can be formed by performing the above operation. If the answer is large, print the result modulo 109 +7.

Let us try to understand it with an example,

N = 3
X = 2
S = [1, 2, 3]

There are 2 ways in which this operation can be performed.

• Way 1: Either -1 should be multiplied to the same element 2 times, OR
• Way 2: -1. Should be multiplied by two different elements once each.

Way 1:
If we multiply -1, to each element 2 times. It will become +1 (-1 *-1).
We will get the same sequence for each element:

• Multiply -1, 2 times to S : [1, 2, 3].
• Multiply -1, 2 times to S : [1, 2, 3].
• Multiply -1, 2 times to S : [1, 2, 3].

So, the unique sequence is just 1 which is [1, 2, 3].

Way 2:
If we multiply -1, by two different elements just 1 time each. We get:

• Multiply -1 to S & S : [-1, -2, 3].
• Multiply -1 to S & S : [1, -2, -3].
• Multiply -1 to S & S : [-1, 2, -3].

Hence, we get a total of 3 different sequences from Way 2.
Total 1 + 3 = 4 different sequences.

Example 1:

• Input:
3 1 -> Input integer, N, X
{1, 2, 1} -> Input integer, S
• Output:
3 -> Output
• Explanation:
In the given scenario, we have X =1. Hence, we can have this multiplication of -1 only once.
So, if we multiply -1, by different elements just 1 time. We get:
• Multiply -1 to S & S : [-1, -2, 1].
• Multiply -1 to S & S : [1, -2, -1].
• Multiply -1 to S & S : [-1, 2, -1].

Hence, we get a total of 3 different sequences.

Example 2:

• Input:
3 2 -> Input integer, N, X
{1, 2, 3} -> Input integer, S
• Output:
4 -> Output
• Explanation:
There are 2 ways in which this operation can be performed
• Way 1: Either – 1 should be multiplied to the same element 2 times, OR
• Way 2: -1 should be multiplied by different elements once.

As shown in the above Demo example, there will be a total of 4 different sequences which can be achieved from this.

### TCS Coding Question Day 2 Slot 1 – Question 2

Two parallel roads separated by a river are connected from cities A and B to an outer ring road. Both roads have a high flow of traffic throughout the day. People who want to travel from city A to city B or vice versa have to pass through the ring road which is a huge waste of time and money. To ease the traffic and also to make it convenient for commuters to travel from city A to city B and vice versa, the construction of a bridge over the river is planned.

The surveillance team submitted a report stating the bridge should be constructed in the following manner:

• The ground or soil is stronger at certain points on the road favorable for the construction of the bridge.
• The strong ground positions are given from the starting point of each road. Say, the road of city A has strong ground at 1,4 meaning there is a strong ground at a distance of 1 unit, another strong ground point at a distance of 4 units from the starting point of the road of city A.

Collate the strong ground positions of both roads. Sort them in ascending order. Calculate the middle point or median of the combined strong ground positions. The bridge should be constructed from road A as per the middle point calculated. Given the number of strong positions on roads A and B(N1 and N2 respectively) and the strong ground positions on each road, the task here is to calculate the midpoint of the combined strong positions on both roads.

NOTE: When the strong positions are combined, the repeated positions on the different roads are dropped.

Example 1:

• Input:
3 -> Value of N1
3 -> Value of N2
{3,5,2} -> a[ ], Elements ato a[N1-1], where each input element is separated by new line
{1,2,3} -> b[ ], Elements bto b[N2-1), where each input element is separated by new line
• Output: 2.5
• Explanation:
From the inputs given above:
Number of strong ground positions on road A:3
Number of strong ground positions on road B:3
The positions of strong ground from the starting point of road A are at a distance of 3,5,2
The positions of strong ground from the starting point of road B are at a distance of 1,2,3
Combining the strong ground positions of both the roads and sorting them in ascending order
1, 2, 3, 5
The Middle points are 2 and 3
2+3 = 5
5/2 = 2.5
So, the middle point from where the bridge should be constructed is 2.5.
Hence, the output is 2.5

Example 2:

• Input:
2 -> Value of N1
3 -> Value of N2
{2,3} -> all, Elements a[O]to a[N1-1), where each input element is separated by new line
{5,6,4} -> b[ ], Elements b[O]to b[N2-1], where each input element is separated by new line
• Output: 4
• Explanation:
From the inputs given above:
Number of strong ground positions on road A: 2
Number of strong ground positions on road B: 3
The positions of strong ground from the starting point of road A are at a distance of 2, 3 The positions of strong ground from the starting point of road B are at a distance of 5, 6, and 4 Combining the strong ground positions of both the roads and sorting them in ascending order: 2, 3, 4, 5, 6 > Middle point is 4
So, the middle point from where the bridge should be constructed is 4.
Hence, the output is 4.

### TCS Coding Question Day 2 Slot 2 – Question 1

A chocolate factory is packing chocolates into packets. The chocolate packets here represent an array arrt of N number of integer values. The task is to find the empty packets(0) of chocolate and push it to the end of the conveyor belt(array).

For Example:

• N=7 and arr = [4,5,0,1.9,0,5,0].
• There are 3 empty packets in the given set. These 3 empty packets represented as O should be pushed towards the end of the array

Example 1:

Input:

• 7  – Value of N
• [4,5,0,1,0,0,5] – Element of arr[O] to arr[N-1],While input each element is separated by newline

Output:

• 4 5 1 9 5 0 0

Example 2:

Input:

• 6
• — Value of N.
• [6,0,1,8,0,2] – Element of arr to arr[N-1], While input each element is separated by newline

Output:

• 6 1 8 2 0 0

### TCS Coding Question Day 2 Slot 2 – Question 2

Joseph is learning digital logic subject which will be for his next semester. He usually tries to solve unit assignment problems before the lecture. Today he got one tricky question. The problem statement is “A positive integer has been given as an input. Convert decimal value to binary representation. Toggle all bits of it after the most significant bit including the most significant bit. Print the positive integer value after toggling all bits”.

Constraints :

1<=N<=100

Example 1:

Input :

10  -> Integer

Output :

5    -> result- Integer

Explanation:

Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which represents “5”. Hence output will print “5”.

### TCS Coding Question Day 3 Slot 1 – Question 1

Airport security officials confiscated several items of the passengers at the security checkpoint. All the items have been dumped into a huge box (array). Each item possesses a certain amount of risk[0,1,2]. Here, the risk severity of the items represents an array[] of N number of integer values. The task here is to sort the items based on their levels of risk in the array. The risk values range from 0 to 2.

Example :

Input :

7  -> Value of N

[1,0,2,0,1,0,2]-> Element of arr to arr[N-1], while input each element is separated by new line.

Output :

0 0 0 1 1 2 2  -> Element after sorting based on risk severity

Example 2:

input : 10  -> Value of N

[2,1,0,2,1,0,0,1,2,0] -> Element of arr to arr[N-1], while input each element is separated by a new line.

Output :

0 0 0 0 1 1 1 2 2 2  ->Elements after sorting based on risk severity.

Explanation:

In the above example, the input is an array of size N consisting of only 0’s, 1’s, and 2s. The output is a sorted array from 0 to 2 based on risk severity.

### TCS Coding Question Day 3 Slot 1 – Question 2

Given N gold wires, each wire has a length associated with it. At a time, only two adjacent small wires are assembled at the end of a large wire and the cost of forming is the sum of their length. Find the minimum cost when all wires are assembled to form a single wire.

For Example:

Suppose, Arr[]={7,6,8,6,1,1,}

{7,6,8,6,1,1}-{7,6,8,6,2} , cost =2

{7,6,8,6,2}- {7,6,8,8}, cost = 8

{7,6,8,8} – {13,8,8}, cost=13

{13,8,8} -{13,16}, cost=16

{13, 16} – {29}, cost =29

2+8+13+16+29=68

Hence , the minimum cost to assemble all gold wires is 68.

Constraints

• 1<=N<=30
• 1<= Arr[i]<=100

Example 1:

Input

6  -> Value of N, represent size of Arr

7  -> Value of Arr, represent length of 1st wire

6 -> Value of Arr, represent length of 2nd wire

8 -> Value of Arr , represent length of 3rd wire

6 -> Value of Arr, represent length of 4th wire

1 -> Value of Arr, represent length of 5th wire

1 -> Value of Arr, represent length of 6th wire

Output :

68

Example 2:

Input

4   -> Value of N, represents size of Arr

12  -> Value of Arr, represents length of 1st wire

2   -> Value of Arr, represent length of 2nd wire

2   -> Value of Arr, represent length of 3rd wire

5  -> Value of Arr, represent length of 4th wire

Output :

34

### TCS Coding Question Day 3 Slot 2 – Question 1

Given an array Arr[] of size T, contains binary digits, where

• 0 represents a biker running to the north.
• 1 represents a biker running to the south.

The task is to count crossing biker in such a way that each pair of crossing biker(N, S), where 0<=N<S<T, is passing when N is running to the north and S is running to the south.

Constraints:

0<=N<S<T

Example 1:

Input :

• 5 -> Number of elements i.e. T
• 0 -> Value of 1st element.
• 1 -> Value of 2nd element
• 0 -> Value of 3rd element.
• 1 -> Value of 4th element.
• 1 -> Value of 5th element

Output :

• 5

Explanation:

The 5 pairs are (Arr, Arr), (Arr, Arr), (Arr, Arr), (Arr,Arr) and (Arr, Arr).

### TCS Coding Question Day 3 Slot 2 – Question 2

Mr. Rao is relocating from place A to B. The moving truck has a maximum capacity C. There are ‘N’ items in the house where each item has a corresponding value (Vi) and weight(Wi). Mr. Rao has to carry only the most valuable items whose total weight does not exceed the capacity of truck. The task here is to find those items (single or combination of items) whose total value (v) will be the maximum and their corresponding weight(w) will not exceed truck capacity(c). Here,

• N= No. of items
• C= Maximum capacity of the truck, an integer value,
• W[0 to N-1]- An array consisting weight of each item
• V[0 to N-1] – An array consisting value of each item.

Example 1:

Input :

• 4  -> Value of N
• 80 -> Value of C
• [10,45,60,90] -> Elements of array v[], where each element is separated by new line.
• [15,20,30,40] -> Elements of array  w[], where each element is separated by new line.

Output: 150

Explanation:

• Value=10 weight=15
• Value=45 weight = 20
• Value = 60 weight=30
• Value=90 weight=40

The subsets that can  be formed from the array V[], their corresponding weight W[] and comparison with c=80

ValueTotal ValueWeightTotal Weight

Valid (total weight(<C)

Invalid (total weight>C)

10+455515+2035Valid
10+607015+3045Valid
10+9010015+4055Valid
45+6010520+3050Valid
45+9013520+4060Valid
60+9015020+4070Valid
10+45+6011520+4065Valid
10+60+9016020+4085Invalid
10+45+9014520+4075Valid

From the above table, it is perceived that particularly for the valid items, maximum value=150 and their corresponding weight is 70. So the output should be 150.

The input format for testing

First Input – Accept value for N (positive integer number).

Second input : Accept value for C (Positive integer number),

Third input – Accept N number of positive integer number for array v[0…N-1], where each value is separated by a new line.

Fourth input – Accept N positive integer numbers for array [0….N-1], where each value is separated by a new line.

The output format for testing

The output should be a positive integer number (Check the output in Example 1)

Constraints:

• 0<=N<=10
• 0 <= C<=500
• 1<=V[i]<=500

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## 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

• sucharita.700

Qs 11 may be solved in python as….
def prime(n):
i,j=0,2
while i2: return(fibo(n-1)+fibo(n-2))
n=int(input(‘Enter N:’))
if n%2==0: print(prime(n//2))
else: print(fibo(n//2+1)) 2
• Syed Mamon

I think Programming 11 has some issue, plz chek it 0
• HelpPrepInsta

ok Syed we’ll see to it 0
• Pritam

Please share the Code of WaterMark Solution Problem of TCS phase 2 test slot 1 on 1st sep 2019. 0
• PULIKANTI VASANTHA

Will any command line programs come in TCS NQT 2020 in last round of coding section 0
• Prep Insta

Hi,

Command Line will not be used as a coding language in TCS Ninja Test. However, you need to learn command line programming, it is very important that you do. As 1 MCQ question in programming Logic section will be asked from command line programming basics. 0
• Rohit Sharma

Thank you sir. The questions that you gave above was repeated in my Bsc for tcs that happened on July 5, 2019. Thank you so much.

It was exact same I even knew the code as I did it a day before from your website. I hope that I clear my interview as well 3
• Arun kumar

what is the cut-off marks for TCS Ninja and how much we did need to score in order to get selected for TCS Digital . 14
• Shreya

Thank you sir. I got similar questions in the exam as well. Not the same question but the pattern was same. 0
• Ali

Tcs paper pattern has changed.
They have kept args prog in mcq and coding section has changed.
Where can i get coding qstns according to the new one? 9