TCS CodeVita Previous Year Questions
TCS CodeVita Previous Year Questions
● 2 Pointers Approach
er
● Searching Sorting
● Number Theory
● Stacks and Queues
op
● Binary Search
● Dynamic Programming
● Graphs
el
● Trees
● Recursion
● Segment
ev
● Heaps
Resources:
ud
● https://www.youtube.com/watch?v=tyB0ztf0DNY&list=PLgUwDviBIf0pwFf-BnpkX
sh
xs0Ra0eU2sJY (DP)
● https://www.geeksforgeeks.org/top-50-tree-coding-problems-for-interviews/
ya
(Tree)
● https://www.youtube.com/playlist?list=PLgUwDviBIf0rAuz8tVcM0AymmhTRsfaL
U (LinkedList)
@
er
If more than one date time combination may be formed, Arun needs to give the latest
valid date time possible in the year 2018.
Constraints
op
Single digits (any of 0-9)
Input Format
el
A line consisting of a sequence of 12 (not necessarily distinct) single digits (any of 0-9)
separated by commas. The sequence will be non-decreasing.
ev
Output
ud
The maximum possible valid date time in the year 2018. The output must be in the
format
MM/DD HH:MM
If no date time can be constructed, the output should be 0.
sh
Example1 :
ya
Input 0,0,1,2,2,2,3,5,9,9,9,9
Output 12/30 22:59
@
Explanation:
Example 2
Input: 3,3,3,3,3,3,3,3,3,3,3,3
Output: 0
Explanation
As no digit less than 3 is present in the input, a valid month cannot be formed. Hence no
valid Date time can be formed with the input digits.
er
Problem 2 : Digital Time
op
The objective is to form the maximum possible time in the HH:MM:SS format using any
six of nine given single digits (not
necessarily distinct)
Given a set of nine single (not necessarily distinct) digits, say 0, 0, 1, 3, 4, 6, 7, 8, 9, it is
el
possible to form many distinct times
in a 24 hour time format HH:MM:SS, such as 17:36:40 or 10:30:41 by using each of the
digits only once. The objective is to
ev
find the maximum possible valid time (00:00:01 to 24:00:00) that can be formed using
some six of the nine digits exactly
once. In this case, it is 19:48:37.
ud
Input : A line consisting of a sequence of 9 (not necessarily distinct) single digits (any of
0-9) separated by commas. The sequence will
sh
be non-decreasing
six of the nine given digits (in any order) precisely once each. If no combination of any
six digits will form a valid time, the
output should be the word Impossible
@
Example 1
Input: 0,0,1,1,3,5,6,7,7
Output: 17:57:36
Explanation:
The maximum valid time in a 24 hour clock that can be formed using some six of the 9
digits precisely once is 17:57:36
Example 2
Input: 3,3,3,3,3,3,3,3,3
Output: Impossible
Explanation:
er
No set of six digits from the input may be used to form a valid time.
op
Problem3 : Greedy Hostel Owner
You know that summers are at peak this year and every day is hot and due to this
el
everyone is using coolers and ACs and a lot of electricity is consumed by the people.
You are living in a hostel and your hostel owner decided to charge extra for electricity
consumption. To achieve this he put one separate electricity meter for every room and
ev
connected all those meters to central meter.
But the hostel owner is a bit greedy and wants to manipulate the meters to show a
reading that is more than the actual
ud
consumption of electricity. He also encrypted all the meters with alphabets. The
technique he used for encrypting is as follows:
Every meter has 6 Alphabets i.e. 6 digits.
sh
Output Format:
er
If the first line is GREEDY, the next line should contain the difference (as a decimal
number) between the central meter reading
and the consumption shown in the rooms.
op
Constraints:
el
Number of rooms <= 100
Example 1
ev
Input
3
ud
105
ABCDEFGH
ya
Example 2
Input
@
8
JAACJA JAABCH JAABHD JAACAF JAJAJJ JAABEJ JAACJJ JAACDI
1500
Output
INNOCENT
Explanation
The readings are,
000200, 000127, 000173, 000205, 0000099, 000149, 000299, 000238
The sum of these readings is 1490 < 1500, the central meter reading. Hence the owner
is INNOCENT.
er
In this superhero epic, the denizens of the Marvel Universe are forced to pick sides
when Captain America and Iron Man come to blows over ideological differences.
The government decides to push for the Hero Registration Act, a law that limits a hero’s
op
actions. This results in a division in The Avengers. Iron Man stands with this Act,
claiming that their actions must be kept in check otherwise cities will continue to be
destroyed, but Captain America feels that saving the world is daring enough and that
they cannot rely on the government to protect the world. And here the civil war begins.
el
They are trying make their team stronger by adding more avengers to their team. There
ev
are N avengers lined up.
● Any team can start first. But they will alternatively only.
sh
● They can select avenger from any side. But if they start from one side they can’t
move to other side in current chance.
ya
● They will stop only when all the avengers are part of either side.
@
● There are some spurious avengers who will decrease the overall power of the
team.
Both teams will select players optimally. Find the difference of powers of the two teams
Constraints
1<= N <= 10^6
-10^9 <= p[i] <= 10^9
Input
First line contains an integer denoting the number of Avengers(N).
er
Next lines contain N space separated values denoting power of every avenger(P[i]).
op
Output
Print the difference of the powers of teams
– Time Limit (secs)
1
el
Examples :
ev
Input
5
2-78-1 20
ud
Output
2
sh
You are given N comma-separated Strings. You need to form all possible legal subsets
of these N strings. These subsets will be a combination of zero or more of these N
Strings After forming the subsets, they will be ranked in a particular onder. The legal
@
● Next N ranks will be the N Strings that appear in the order they are provided in
the input
● After N + 1 ranks, you need to combine N strings such that all legal combinations
are formed
● Legal combination simply means that while combinations are formed, the string
that appears to the left of a particular string in the input, can never appear to the
right of that particular string, when subsets are formed
● A subset with less elements will be ranked higher than a subset with more
er
elements (NOTE-Rank 1 is higher than rank 2)
op
● Refer Example 2 to get a better understanding of how subsets are formed and
ranked
el
● It is guaranteed that
ev
● N>=1
Example: you are having an input string “aa,cc,bb” in this string we can see we have
three strings which are comma separated. Now from this group of string we have to
sh
create all possible subset of strings. 8 subsets can be formed from these strings. And
they are as follows:
ya
1. {}
2. {aa}
@
3. {cc}
4. {bb}
5. {aa,}
Note: here we can see the ranks given to the subsets are first by size i.e., the subset
with lesser number of strings is ranked higher than the subset with higher size. If the
subsets have equal number of strings then, the combination which is formed earlier (by
virtue of combining strings in order they appear in input), gets a higher rank.
For example, rank of su bset (aa,cc) > rank of (aa,bb) because string cc is appearing
prior to string bb in the input. Similarly, rank of (cc) > rank of (bb).
You are provided one rank R and for that you have to print the Rth subset from all legal
er
subsets.
op
Constraints:
0<N<=10^2
el
0<R<=10^18
ev
Input
ud
Second line contains an integer R, for which you have to find Rth subset from all legal
sh
subsets.
ya
Third line contains N comma-separated strings basis which the subsets should be
formed
@
Output:
From all possible legal subsets find the subset whose rank is R
a,b
er
Output
op
a,b
el
Explanation:
ev
Given that N = 2, given
{}-1
ya
{a} -2
@
{b}-3
{a, b}-4
You are a caretaker of a waiting room and you have to take care of empty seats such
that all the people should sit together. Imagine the seats are in a straight line like in a
movie theatre. People are seated on random seats initially. Your task is to make them sit
together so that minimum number of people change their position. Also, they can be
made to sit together in many ways. Find the number of ways you can make them sit
er
together by requiring only minimal people movement.
“E” depicts an empty seat and “O” depicts an occupied seat. Input will be given in the
op
form of a string.
Example: OEOEO
el
As we can see, only seat number 1, 3, 5 are occupied and 2 and 4 are empty.
ev
Case 1: If we move 5th person to 2nd position, they can all be together with only one
person moving his/her place.
ud
Case 2: If we movement 1st person to 4th position, they can all be together with only
one person moving his/her place.
sh
They can all be together with only one movement and this can be done in 2 ways. Print
the minimum number of movements required and the number of ways this minimum
ya
Constraints
0 <N <= 100000
Input
First line contains an integer N which depicts the number of seats
Second line contains N characters each of which are either “O” or “E”. “O” denotes an
occupied seat and “E” denotes an empty seat.
Output
Print minimum number of movements required and the number of ways in which all
people can be made to sit together without exceeding minimum number of movements
er
by space
Time Limit (secs)
1
op
Examples
Input
el
5
ev
OEOEO
Output
ud
12
Explanation:
sh
Seat number 2 and 4 are unoccupied and all the other seats are occupied.
@
We can make them sit together by moving only one person near to the other. It can be
done in 2 ways:
You are given N number of coordinates and you have to create a polygon from these
points such that they will make a polygon with maximum area.
Note: coordinates provided in the input may or may not be in sequential form.
Constraints
1 <= N <= 10
er
Input:
op
First line contains an integer N which depicts number of co-ordinates
el
Next N lines consist of two space separated integer depicting coordinates of in form of
xy
ev
Output:
Print the maximum possible area possible by creating a polygon by joining the
ud
coordinates.
Examples:
@
Input:
0 0
2 0
0 2
2 2
Output: 4
er
Explanation:
op
As we can imagine that these points will make a square shape and the maximum
possible area made by the polygon will be 4.
el
ev
Problem 8: Consecutive Prime Sum
● For example
sh
○ 5 = 2 + 3,
ya
○ 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.
Input Format: First line contains a number N
Output Format: Print the total number of all such prime numbers which are less than or
equal to N.
Constraints: 2<N<=12,000,000,000
er
Problem 9 Counting Rock Sample
op
Question – : Juan Marquinho is a geologist and he needs to count rock samples in
order to send it to a chemical laboratory. He has a problem: The laboratory only accepts
rock samples by a range of its size in ppm (parts per million).
el
Juan Marquinho receives the rock samples one by one and he classifies the rock
ev
samples according to the range of the laboratory. This process is very hard because the
number of rock samples may be in millions.
ud
Juan Marquinho 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.
sh
Input Format: An 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
ya
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:
Example 1
● Input: 10 2
er
● 345 604 321 433 704 470 808 718 517 811
op
● 300 350
el
● 400 700
ev
Output: 2 4
Explanation:
ud
There are 10 samples (S) and 2 ranges ( R ). The samples are 345, 604,811. The
ranges are 300-350 and 400-700. There are 2 samples in the first range (345 and 321)
sh
and 4 samples in the second range (604, 433, 470, 517). Hence the two lines of the
output are 2 and 4
ya
Constraints:
● 1<N<10000000000
er
● 1<k<600.
op
You can assume that N will have no prime factors which are larger than 13.
Example 1
el
● Input: 12,3
ev
● Output: 4
ud
Example 2
● Input: 30,9
ya
● Output: 1
@
Problem Description
One person hands over the list of digits to Mr. String, But Mr. String understands only
strings. Within strings also he understands only vowels. Mr. String needs your help to
find the total number of pairs which add up to a certain digit D.The rules to calculate
digit D are as follow :-
Take all digits and convert them into their textual representation.
Next, sum up the number of vowels i.e. {a, e, i, o, u} from all textual representation.
er
This sum is digit D
op
Now, once digit D is known find out all unordered pairs of numbers in input whose sum
is equal to D. Refer example section for better understanding.
el
Constraints
ev
1 <= N <= 100
ud
1 <= value of each element in second line of input <= 100
Input
as input
Output
Lower case representation of textual representation of number of pairs in input that sum
up to digit D
Examples
Input : 5
er
12345
op
Output : one
el
Input : 3
ev
742
Output : zero
ud
Elections are going on, and there are two candidates A and B, contesting with each
other. There is a queue of voters and in this queue some of them are supporters of A
and some of them are supporters of B. Many of them are neutral. The fate of the
ya
election will be decided on which side the neutral voters vote. Supporters of A and
supporters of B make attempt to win the votes of neutral voters.
@
1. The voter queue is denoted by three characters, viz {-, A, B}. The – denotes neutral
candidate, A denotes supporter of candidate A and B denotes supporter of candidate B.
2. Supporters of A can only move towards the left side of the queue.
3. Supporters of B can only move towards the right side of the queue.
5. They both will try and influence the neutral voters by moving in their direction in the
queue. If supporter of A reaches the neutral voter before supporter of B reaches him,
then that neutral voter will become a supporter of candidate A.
er
6. Similarly, if supporter of B reaches the neutral voter before supporter of A reaches
him, then that neutral voter will become a supporter of candidate B.
op
7. Finally, if both reach at the same time, the voter will remain neutral. A neutral vote
cannot decide the outcome of the election.
el
8. If finally, the queue has more votes for candidate A, then A wins the election. If B has
ev
more votes, then B wins that election. If both have equal votes, then it will be a coalition
government.
ud
Refer Examples section for understanding the dynamics of how the supporters influence
the neutral voters.
sh
Note: There are no test cases where all votes are neutral.
ya
Input
@
· – = neutral voter
Output
Print candidate with maximum number of votes. If they have equal number of votes,
er
print “Coalition government“.
Examples
op
Input :14
el
–AB–AB—A–
ev
Output : A
Input : 4
ud
A—
sh
Output : A
ya
Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples
section below.
Constraints
er
3 <= N <= 10^5
op
Input
Output
el
ev
The output contains vowels (stars) in order of their occurrence within the given galaxy.
The galaxy itself is represented by the # character.
ud
Example 1
sh
Input
ya
18
*.*#***#***#***.*.
@
*.*#*.*#.*.#******
***#***#***#****.*
Output
U#O#I#EA
Explanation
As can be seen, the stars make the image of the alphabets U, O, I, E, and A
respectively.
er
Problem 14: Prime Time Again
op
Here on earth, our 24-hour day is composed of two parts, each of 12 hours. Each hour
in each part has a corresponding hour in the other part separated by 12 hours: the hour
essentially measures the duration since the start of the daypart. For example, 1 hour in
el
the first part of the day is equivalent to 13, which is 1 hour into the second part of the
day.
Now, consider the equivalent hours that are both prime numbers. We have 3 such
ev
instances for a 24-hour 2-part day:
5~17
ud
7~19
11~23
sh
Accept two natural numbers D, P >1 corresponding respectively to several hours per
day and the number of parts in a day separated by a space. D should be divisible by P,
meaning that the number of hours per part (D/P) should be a natural number. Calculate
the number of instances of equivalent prime hours. Output zero if there is no such
ya
instance. Note that we require each equivalent hour in each part of a day to be a prime
number.
@
Example:
Input: 24 2
Output: 3 (We have 3 instances of equivalent prime hours: 5~17, 7~19, and 11~23.)
Constraints
Input
The single line consists of two space-separated integers, D and P corresponding to the
number of. hours per day and number of parts in a day respectively
Output
er
Output must be a single number, corresponding to the number of instances of
equivalent prime number, as described above
op
Example 1
Input
el
36 3
Output
ev
2
Explanation
ud
3~15~X
5~17~29 - an instance of equivalent prime hours
7~19~31 - an instance of equivalent prime hours
ya
11~23~X
Hence the answer is 2.
@
A Company has decided to give some gifts to all of its employees. For that, the
company has given some rank to each employee. Based on that rank, the
company has made certain rules for distributing the gifts.
The rules for distributing the gifts are:
Employees having higher ranking get a greater number of gifts than their
neighbours.
er
What is the minimum number of gifts required by the company?
Constraints
op
1 < T < 10
el
1 < N < 100000
ev
1 < Rank < 10^9
Input
ud
The first line contains integer T, denoting the number of test cases.
sh
The second line contains N space-separated integers, denoting the rank of each
@
employee.
Output
For each test case print the number of minimum gifts required on a new line.
Input
12152
er
2
op
12
Output
7
el
ev
3
ud
Explanation
sh
er
Therefore, the total gifts required is 1 + 2 =
op
el
Problem 16 :Minimize the sum
Constraints
Input
The first line contains two integers N and K representing the size of the array and
the maximum number of operations that can be performed on the array
respectively.
er
Output
Print a single integer denoting the minimum sum of the final array.
op
Input
43
el
ev
20 7 5 4
ud
Output
17
sh
Explanation
ya
Operation 1 -> Select 20. Replace it with 10. New array = [10, 7, 5, 4]
@
Operation 2 -> Select 10. Replace it with 5. New array = [5, 7, 5, 4].
Sum = 17.
Problem 17 :Railway Station
Given the schedule of trains and their stoppage time at a Railway Station, find a
minimum number of platforms needed.
Note -
If Train A's departure time is x and Train B's arrival time is x, then we can't
er
accommodate Train B on the same platform as Train A.
op
Constraints
el
0 <= a <= 86400
ev
0 < b <= 86400
ud
Input
Next N line contains 2 integers, a and b, denoting the arrival time and stoppage
@
Output
Input
10 2
er
5 10
op
13 5
el
Output
ev
2
Constraints
@
The first line contains two integers N and K where N is the size of the array and K
is a number as described above. The second line contains N integers separated
by space.
Output
er
Print a single integer denoting the total number of happy elements.
op
Example 1
Input
63
el
ev
5 5 7 9 15 2
ud
Output
sh
The war between the Republic and the Separatists is escalating. The Separatists
are on a new offensive. They have started blocking the path between the republic
@
Constraints
er
M <= 10000
op
N <= 7000
Input
el
The first line contains two space-separated integers M and N, where M denotes
ev
the number of paths between planets and N denotes the number of planets.
Output
sh
Clines containing one integer representing the critical planet that they need to
save in ascending order of the planet number if no planet is critical then print -1
ya
Time Limit
@
Example 1
Input
34
01
12
23
er
Output
op
0
el
1
ev
2
3
ud
sh
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.
Constraints:
er
1 <=T <= 50
op
1<= N1 <= 30
1<= N2 <= 30
el
Input Format:
ev
● First line: P principal (Loan Amount)
ud
● 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.
ya
● The 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.
er
op
el
ev
ud
sh
ya
@