Practice TCS NQT Advanced Coding - Questions Only
Practice TCS NQT Advanced Coding - Questions Only
Question 1) Alice and her friends are playing a game of verbal Kho-Kho. Alice is acting
as a mediator, and the rest of the N friends are seated on N chairs, one each. Alice starts
by providing a paper with a single-digit number to the friend present at number 1. Let’s
denote friends by F, where F will be of size N.
Given N number of friends and digit array D, denoting the digit understood by each
friend F, finds out how many of Alice’s friends have not enacted well OR did not
understand the enactment by the previous friend correctly.
Example 1:
3 -> N, number of friends
4 4 4 – array D. denoting digit understanding by N friends
Output:
0
Explanation:
All of them understood the digits correctly.
Example 2:
5
12322
Output:
4
Explanation:
1st, 2nd, 3rd, and 4th friends could not enact OR understand the enactment.
Question 2)
Bob is going to bet today on horse riding. There are N horses listed in a sequence of 1
to N.
The probability of winning each horse is different so the prices for making a bet on the
horses are not the same. There is no limit on the number of horses on which he can bet,
but he thinks that if he bets on a continuous sequence of horses then he has a better
chance to win. Bob will get K units of money if any horse on which he bets will win.
But as the award is only K units so he wants to put money less than K.
Given the number of horses(N), reward money(K), and price of betting on N horses in
order.
Hint: For each starting index of a horse, its end index in sequences will be equal
to or greater than the end index of the previous starting index.
Example 1:
Input:
90 100 -> N = 10, K=100
30 40 50 20 20 10 90 10 10 10 -> Price to make bet on each horse in order
Output:
3
Explanation:
There are 10 horses, and the reward money is 100. So, Bob will put money in less than
100. There are two possible o sequences of length three whose total money for betting
is less than 100, i.e. [50 20 20] (sum is 90) and [10 10 10] (sum is 30). Bob will choose
randomly one sequence from these two. As none of the other sequences with a length
greater than 3 will have a price less than 100 so the answer will be 3.
Example 2:
Input:
10 100 -> N = 10, K=100
10 90 80 20 90 60 40 60 70 75 -> Price to make bet on each horse in order
Output:
1
Explanation:
There are no two consecutive horses for which the sum of price is less than 100. So,
Bob will choose randomly any one horse. And the max length of the sequence will be
1.
Constraints:
2<=N<=105
1<= K<=109
1<=A1, A3… AN<=109
This vacation you went to visit the golden house. There are N rooms in this golden
house and its owner needs someone to take care of the management of this house. As
you have been unemployed for a long time, you are interested in this job. The owner of
this house wanted an intelligent manager for this role, so he created one puzzle within
that golden house. The person who will be able to solve that puzzle will be the manager
of the golden house.
The owner of the house kept some number of golden coins in each room. You have to
choose two rooms, one from where you will enter and the other one from where you
will exit. From any room either you can exit, or you can move to the next room. While
visiting any room you will collect all the gold coins, and if you enter any room then you
can’t skip collecting gold coins from that room, you have to take those coins. The owner
wants to have exactly K coins, when you exit the room, he guarantees that there will be
at least one possible solution for this puzzle.
Given several rooms (N), and several gold coins in N rooms. You have to find room
numbers from where you will start and from where you will exit. If there is more than
one solution possible, then you have to provide a solution with a smaller starting room
number.
You can assume that room numbers will start from 1 and end at N.
Hint: Find a continuous subsequence whose sum will be exactly equal to K.
Example 1:
Input:
10 15 -> N =10, K = 15
5 3 7 14 18 1 18 4 8 3 -> Number of gold coins in each room.
Output:
13
Explanation: There are ten rooms in the house. You want to have the Total sum of gold
coins in a continuous sequence of rooms to be 15 There are two solutions to this i.e. [1,
3] and [8, 10] then the solution with a smaller starting room number will be printed
hence [1, 3] is output.
Question 4)
Tina was given a piece of silk cloth. There are already a few points or coordinates
mentioned on it. She is creating something which needs an exact square shape
cloth. Help Tina to find out how many minimum points she can add, to cut the perfect
square from the given cloth.
There are already a few points marked on one cloth, Tina has to include the most
number of points from the given points and should try to include the minimum number
of points or coordinates from her side.
Let us try to understand it with an example Let’s say there are 3 points given, which
means N=3 These points are
00
22
33
Example 2:
Input:
3 -> Input integer. N
00 -> Input integer, x[i], y[i]
22 -> Input integer, x[i], y[i]
33 -> Input integer, x[i], y[i]
Output:
2 -> Output
Question 5)
Jack is a sports teacher at St. Patrick’s School. He makes games not only to make the
student fit but So smart. So, he lined up all the N number classes. of students in his
class. At each position, he has fixed a board with the Integer number printed on it.
Each number is unique and in exactly the range of N. Let us say there are 10 students,
then the boards will be printed with numbers from 1 to 10 in a random order given by
the sequence A[ ]. As a rule, all students wear a jersey with their numbers printed on it.
So if there are students, each will have a unique jersey number just like a football team.
Now, in the beginning, all the students will stand as per the increasing order of their
jersey numbers, from left to right. The only difference will be their respective board
number which is placed at their respective location. The board location is fixed and
cannot be changed. We can consider the arrangement as below. Suppose there are
students, and the board is placed in the order of [2 3 1 5 4].
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
After every beat of the drum, each student will have to move to that location (index),
where his board is pointing to. In the above case student with jersey #1 is standing with
board #2, so now he will have to move to location #2. Similarly, all the other students
will do.
So after the first beat of the drum, the alignment will be:
Board — 2, 3, 1, 5, 4.
This keeps going on and on until all the students are back to the way they were at the
beginning. So, after 6 beats of the drum, all the students will be aligned the same way
as before.
Given N and the order of the board of the respective positions, find the number of
beats required to bring back the students to their original position.
So, for the above case, the answer is 6
Input:
3 -> Input integer, N
{1, 2, 3} ->Input integer. B[], board alignment.
Output:
1 -> Output
Explanation:
All the students will be standing in board positions;
Board — 1, 2, 3
Student’s Jersey –1, 2, 3
After the first beat of the drum:
Jersey #1 will move to index 1.
Jersey #2 will move to index 2.
Jersey #3 will move to index 3.
Hence, they will be back in their position in just 1 beat.
So, the answer is 1.
Example 2:
Input:
5 -> Input integer, N
{2, 3, 1, 5, 4} -> Input integer, B[ ], board alignment.
Output:
6 -> Output
Explanation:
All the students will be standing as below, with the board positions:
Board — 2, 3, 1, 5, 4
Student’s Jersey — 1, 2, 3, 4, 5
After Beat-1of the drum:
Jersey #1 has moved to index 2.
Jersey #2 has moved to index 3.
Jersey #3 has moved to index 1.
Jersey #4 has moved to index 5.
Jersey #5 has moved to index 4.
Board – 2, 3, 1, 5, 4
Student’s Jersey — 3, 1, 2, 5, 4
Board — 2, 3, 1, 5, 4
Student’s Jersey — 2, 3, 1, 4, 5
Q 6)
A washing machine works on the principle of Fuzzy System, the weight of clothes put
inside it for washing is uncertain but based on weight measured by sensors and the
water level chosen, it decides total time needed.
For low level water, the time estimate is 25 minutes, where approximately
weight is between 2000 grams or any nonzero positive number below that.
For medium level water, the time estimate is 35 minutes, where approximately
weight is between 2001 grams and 4000 grams.
For high level water, the time estimate is 45 minutes, where approximately
weight is above 4000 grams.
Assume the capacity of machine is maximum 7000 grams. When the weight is zero,
time needed is 0 minutes. If the weight exceeds maximum weight limit, then, print
“OVERLOADED”, and for all negative weights, the output is “INVALID INPUT”.
Sample Input-1: 2000, L
Sample Output-1: Time Estimated: 25 minutes
Input should be in the form of integer value.
Output must have the following format: Time Estimated: NN Minutes
Question 7)
At the security checkpoint, airport security personnel have seized a number of travellers’
belongings. Everything has been thrown into a big box (array). Each product carries a
specific level of risk [0,1,2]. The risk severity of the items in this case is represented by
an array [] of N integer values. Sorting the elements in the array according to the degrees
of danger is the task at hand. Between 0 and 2 are the risk values.
Question 8
For all of its products, a supermarket maintains a pricing structure. Each product has a
value N printed on it. The price of the item is determined by multiplying the value N,
which is read by the scanner, by the sum of all its digits. The goal here is to create
software that, given the code of any item N, will compute the product (multiplication)
of all the value digits (price).
Example 1:
Input :
5244 -> Value of N
Output :
160 -> Price
Explanation:
From the input above
Product of the digits 5,2,4,4
5*2*4*4= 160
Hence, output is 160.
There are two banks – Bank A and Bank B. Their interest rates vary. You have received
offers from both banks in terms of the annual rate of interest, tenure, and variations of
the rate of interest over the entire tenure. You have to choose the offer which costs you
the least interest and reject the other. Do the computation and make a wise choice.
The loan repayment happens at a monthly frequency and Equated Monthly Installment
(EMI) is calculated using the formula given below :
EMI = loan amount * monthly interest rate / ( 1 – 1 / (1 + monthly interest rate)^(number
of years * 12))
Constraints:
1 <= P <= 1000000
1 <=T <= 50
1<= N1 <= 30
1<= N2 <= 30
Input Format:
First line: P principal (Loan Amount)
Second line: T Total Tenure (in years).
Third Line: N1 is the number of slabs of interest rates for a given period by Bank A.
The first slab starts from the first year and the second slab starts from the end of the
first slab and so on.
Next N1 line will contain the interest rate and their period.
After N1 lines we will receive N2 viz. the number of slabs offered by the second bank.
Next N2 lines are the number of slabs of interest rates for a given period by Bank B.
The first slab starts from the first year and the second slab starts from the end of the
first slab and so on.
The period and rate will be delimited by a single white space.
Output Format: Your decision is either Bank A or Bank B.
Example 1
Input
10000
20
3
5 9.5
10 9.6
5 8.5
3
10 6.9
5 8.5
5 7.9
Output: Bank B
Question 10
Some prime numbers can be expressed as a sum of other consecutive prime numbers.
For example, 5 = 2 + 3, 17 = 2 + 3 + 5 + 7, 41 = 2 + 3 + 5 + 7 + 11 + 13. Your task is
to find out how many prime numbers which satisfy this property are present in the range
3 to N subject to a constraint that summation should always start with number 2.
Write code to find out the number of prime numbers that satisfy the above-mentioned
property in a given range.
Question 11:
K-th largest factor of N
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 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. Since given k value is more than the number of factors, the output is 1.
import java.util.*;
class Main { public static void main(String[] args) {
int n, k, i, c = 0;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
k = sc.nextInt();
for (i = n; i >= 1; i--) {
if ((n % i) == 0)
c++;
if (c == k) {
System.out.println(i);
break;
}
}
if (c != k)
System.out.println("1");
}
}
Krishna loves candies a lot, so whenever he gets them, he stores them so that he can eat
them later whenever he wants to.
He has recently received N boxes of candies each containing Ci candies where Ci
represents the total number of candies in the ith box. Krishna wants to store them in a
single box. The only constraint is that he can choose any two boxes and store their joint
contents in an empty box only. Assume that there are an infinite number of empty boxes
available.
At a time, he can pick up any two boxes for transferring and if both the boxes contain
X and Y number of candies respectively, then it takes him exactly X+Y seconds of time.
As he is too eager to collect all of them he has approached you to tell him the minimum
time in which all the candies can be collected.
Input:
1
4
1234
Output:
19
Explanation:
4 boxes, each containing 1, 2, 3 and 4 candies respectively. Adding 1 + 2 in a new box
takes 3 seconds. Adding 3 + 3 in a new box takes 6 seconds. Adding 4 + 6 in a new box
takes 10 seconds. Hence total time taken is 19 seconds. There could be other
combinations also, but overall time does not go below 19 seconds.
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
Given N number of numbers, perform the following operations and print the output.
You have to efficiently handle potentially large numbers (x) by focusing only on the
last two digits of 2^x.
Your program needs to read N integers. For each integer x, it has to calculate 2^x,
extract the last two digits of this number, and add these digits to a running total sum.
Finally, it has to print the last two digits of the total sum.
Input
First line contains an integer N
Second line will contain N numbers delimited by space
Output
Number that is the output of the given code by taking inputs as specified above
Example 1
Input
4
8674
Output
64
Example 2
Input
3
123
Output
14
Explanation
import java.util.Scanner;
This imports the Scanner class, which is used for reading input from the user.
2. Initialization:
int N = sc.nextInt();
java
Copy code
String str = Long.toString((long) Math.pow(1 << 1, x));
oThis line ensures that only the last two digits of the computed power
are kept.
o If the length of the string representation of 2x2^x2x is greater than 2, it
keeps only the last two characters. If it’s less than or equal to 2, it uses
the whole string.
6. Add to Sum:
sum += Integer.parseInt(str);
System.out.println(sum % 100);
Summary:
The program reads N integers. For each integer x, it calculates 2^x, extracts the
last two digits of this number, and adds these digits to a running total sum.
Finally, it prints the last two digits of the total sum.
This code efficiently handles potentially large numbers by focusing only on the last
two digits of 2^x, making it suitable for scenarios where you only need the last two
digits of results from operations involving powers of 2.
There are n houses built in a line, each of which contains some value in it. A thief is
going to steal the maximal value of these houses, but he can’t steal in two adjacent
houses because the owner of the stolen houses will tell his two neighbors left and right
side. What is the maximum stolen value?
Input Format
First an integer n, denoting how many houses are there.
Then n space separated integers denoting the values for the n houses.
Output Format
An integer denoting the maximum value possible to steal.
Input
7
6713825
Output
20
Explanation
6+1+8+5 = 20.
It is the max possible value.
Question 16
Juan Marquinhos is a geologist and he needs to count rock samples to send them to a
chemical laboratory. He has a problem: The laboratory only accepts rock samples in a
range of its size in ppm (parts per million).
Juan Marquinhos receives the rock samples one by one and he classifies the rock
samples according to the range of the laboratory. This process is very hard because the
number of rock samples may be in the millions.
Juan Marquinhos needs your help, your task is to develop a program to get the number
of rocks in each of the ranges accepted by the laboratory.
Input Format
A positive integer S (the number of rock samples) separated by a blank space, and a
positive integer R (the number of ranges of the laboratory); A list of the sizes of S
samples (in ppm), as positive integers separated by space R lines where the ith line
containing two positive integers, space separated, indicating the minimum size and
maximum size respectively of the ith range.
Output Format
R lines where the ith line contains a single non-negative integer indicating the number
of the samples which lie in the ith range.
Constraints
10 < S < 10000
1 < R < 1000000
1 size of each sample (in ppm) < 1000
Example 2
Input: 20 3
921 107 270 631 926 543 589 520 595 93 873 424 759 537 458 614 725 842 575 195
1 100
50 600
1 1000
Output: 1 12 20
Explanation:
Consider the Input: 20 3 which means 20 numbers followed by 3 more lines of
input.
20 numbers are 921 107 270 631 926 543 589 520 595 93 873
424 759 537 458 614 725 842 575 195
There are 20 samples and 3 ranges. The ranges are 1-100, 50-600, and 1-1000.
Note that the ranges are overlapping.
The number of samples in each of the three ranges is 1, 12, and 20 respectively.
Note that, the number of samples in range 1 to 100 : 1. The number of samples in
range 50to 600: 12. The number of samples in range 1 to 1000 : 20 (which means
that all the 20 numbers are in the range 1 to 1000).
Hence the three lines of the output are 1, 12, and 20.
Sample Input 1:
10
1233414512
Sample Output 1:
1 occurs 3 times
2 occurs 2 times
3 occurs 2 times
4 occurs 2 times
5 occurs 1 times
Q18) Problem Statement –
Write a method to solve the following equation a3 + a2b + 2a2b + 2ab2 + ab2 + b3.
Write a program to accept three values in order of a, b and c and get the result of the
above equation.
Q19) Problem Statement – Your job is to calculate how many tyres would be there
in each dealership and find the total number of tyres.
Input
42
40
12
Output
44
Explanation:
There are total 3 dealerships: Dealerships1 contains 4 cars and 2 bikes. Dealerships2
contains 4 cars and 0 bikes. Dealerships3 contains 1 car and 2 bikes
Q21) Write a Program to Find the Roots of a Quadratic Equation. If roots are
possible, then, there are four cases to be handled as explained below:
Case1) Roots are real and different. For this case, print “RD”.
Case2) Roots are real and same. For this case, print “RS”.
Case3) Roots are complex. For this case, print “RC”.
Case4) In case of wrong inputs, print “Invalid”.
Q22) Ritik wants a magic board, which displays a character for a corresponding
number for his science project. Help him to develop such an application.
For example, when the digits 65,66,67,68 are entered, the alphabet ABCD are to be
displayed.
Sample Input 1:
65-A
66-B
67-C
68-D
Note: The input other than 4-digit positive number [includes negative and 0] is
considered as invalid.
Sample Input:
7 – number of rats
2 – food needed for each rat
8 – number of houses
2 8 3 5 7 4 1 2 – food available in each house
Explanation: Total amount of food required for all rats = r * unit; which is = 7 * 2 =
14. The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4
houses is sufficient for all the rats. Thus, output is 4.
The Binary number system only uses two digits, 0 and 1 and number system can be
called binary string. You are required to implement the following method:
The method accepts a string str as its argument. The string str consists of binary digits
separated with an alphabet as follows: A denotes AND operation; B denotes OR
Sample Output:1
Explanation: The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1
AND 0 OR 1”, result of the expression becomes 1, hence 1 is returned.
Q26)
N-base notation is a system for writing numbers that uses only n different symbols,
This symbols are the first n symbols from the given notation list(Including the symbol
for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9,
10:A,11:B and so on upto 35:Z)
Implement the following method Char[] DectoNBase(int n, int num): The method
accept positive integer n and num Implement the method to calculate the n-base
equivalent of num and return the same as a string. Steps:
Divide the decimal number by n, Treat the division as the integer division
Write the the remainder (in n-base notation)
Divide the quotient again by n, Treat the division as integer division
Repeat step 2 and 3 until the quotient is 0
The n-base value is the sequence of the remainders from last to first
Assumption: 1 < base < = 36
Example Input:
12 base
718 n
Output
4BA
Explanation
Input 1: 451
349
800
Output 1: 2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is
carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned.
Sample Input2:
Num 1: 23
Num 2: 563
Sample Output2: 0
Q30)
A party has been organised on cruise. The party is organised for a limited time(T). The
number of guests entering (E[i]) and leaving (L[i]) the party at every hour is represented
as elements of the array. The task is to find the maximum number of guests present on
the cruise at any given instance within T hours.
Sample Input 1: 5
7,0,5,1,3
1,2,1,3,4
Sample Output :8
Explanation:
1st hour: Entry : 7 Exit: 1
No. of guests on ship : 6
2nd hour : Entry : 0 Exit : 2
No. of guests on ship : 6-2=4
Hour 3: Entry: 5 Exit: 1
No. of guests on ship : 4+5-1=8
Hour 4: Entry : 1 Exit : 3
No. of guests on ship : 8+1-3=6
Hour 5: Entry : 3 Exit: 4
No. of guests on ship: 6+3-4=5
Hence, the maximum number of guests within 5 hours is 8.
The input format for testing
The candidate has to write the code to accept 3 input.
First input- Accept value for number of T (Positive integer number)
Second input- Accept T number of values, where each value is separated by a
new line.
Third input- Accept T number of values, where each value is separated by a
new line.
Q32) You have write a method that accepts, a string which length is “len”, the string
has some “#”, in it you have to move all the hashes to the front of the string and return
the whole string back and print it.
Question 33)
Let ‘s’ be the letter that alphabetically comes before all other letters in the subset. Let
‘s’ be called the ‘smallest element’ in the subset.
Replace all the elements of the subset with the letter ‘s’
Find the minimum number of moves which is required to perform the conversion. If it
is not possible to convert the string from A to B then return -1
Operation 2:
Original string [abbab]. Now we have chosen a subset S, let’s say we have taken an
index 1,2,4 from A then the subset becomes [aba]. Next, we have to choose the
smallest element, which is here in a & b. Next, we have to replace the smallest with
all the other elements in the subset. So ‘a’ will replace everything in [aba]. Now we
will place all the respective elements back in their respective index. This will update
the original string as [aabab]. This is the same as String B. Hence it is possible to
convert string A to B, with 2 operations.
So, the answer is 2.
Example 1:
Input:
2 -> Input integer, N
de -> input string, A
cd -> Input string, B
Explanation:
In the above example, we can see that there is an alphabet in A which is completely
different from B. hence it is not possible to convert A to B
So the answer is -1
Example 2:
Input:
4-> input integer, N
abab-> input string, A
abaa-> input string A
Output:
1 -> Output