TCS CodeVita questions asked in previous TCS CodeVita seasons are discussed here. TCS CodeVita is a global coding contest and hence the level of questions is advanced. You need to be a pro at programming to crack TCS CodeVita.
Here are some of the questions which were asked in the previous year. You could take up the mock test by clicking through these links.
TCS CodeVita Coins Distribution Question
TCS CodeVita Philaland Coins Question
TCS CodeVita Divine Divisors Question
TCS CodeVita Dining Table Seating Arrangement Question
TCS CodeVita Exchange Digits Question
TCS CodeVita Forest Fire Question
TCS CodeVita Chakravyuha Question
Here are some of the most asked concepts/questions in TCS CodeVita. Have a look at them and start solving them now.
@@table::4@@
Here are some of the TCS CodeVita questions from previous year’s papers with detailed solutions.
Constellation
Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in the shape of vowels { A, E, I, O, U }. A collection of * in the shape of the vowels is a star. A star is contained in a 3×3 block. Stars cannot be overlapping. The dot(.) character denotes empty space.
Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them.
Note: Please pay attention to how vowel A is denoted in a 3×3 block in the examples section below.
Constraints
3 <= N <= 10^5
Input
Input consists of a single integer N denoting the number of columns.
Output
The output contains vowels (stars) in order of their occurrence within the given galaxy. The galaxy itself is represented by the # character.
Example 1
Input
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.
Example 2
Input
12
* . * # . * * * # . * .
* . * # . . * . # * * *
* * * # . * * * # * . *
Output
U#I#A
Explanation
As it can be seen the stars make the image of the alphabet U, I, and A.
Possible solution:
Input:
12
* . * # . * * * # . * .
* . * # . . * . # * * *
* * * # . * * * # * . *
@@coding::1@@
Prime Time Again
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 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 instances for a 24-hour 2-part day:
5~17
7~19
11~23
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 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
10 <= D < 500
2 <= P < 50
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
Output must be a single number, corresponding to the number of instances of equivalent prime number, as described above
Example 1
Input
36 3
Output
2
Explanation
In the given test case D = 36 and P = 3
Duration of each daypart = 12
2~14~X
3~15~X
5~17~29 – an instance of equivalent prime hours
7~19~31 – an instance of equivalent prime hours
11~23~X
Hence the answer is 2.
Possible solution:
Input:
49 7
@@coding::4@@
Minimum Gifts
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:
Each employee must receive at least one gift.
Employees having higher ranking get a greater number of gifts than their neighbours.
What is the minimum number of gifts required by the company?
Constraints
1 < T < 10
1 < N < 100000
1 < Rank < 10^9
Input
The first line contains integer T, denoting the number of test cases.
For each test case:
The first line contains integer N, denoting the number of employees.
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.
Example 1
Input
2
5
1 2 1 5 2
2
1 2
Output
7
3
Explanation
For test case 1, adhering to the rules mentioned above,
Employee # 1 whose rank is 1 gets one gift
Employee # 2 whose rank is 2 gets two gifts
Employee # 3 whose rank is 1 gets one gift
Employee # 4 whose rank is 5 gets two gifts
Employee # 5 whose rank is 2 gets one gift
Therefore, total gifts required is 1 + 2 + 1 + 2 + 1 = 7
Similarly, for test case 2, adhering to the rules mentioned above,
Employee # 1 whose rank is 1 gets one gift
Employee # 2 whose rank is 2 gets two gifts
Therefore, the total gifts required is 1 + 2 =
Possible solution:
Input:
2
5
1 2 1 5 2
2
1 2
@@coding::3@@
Minimize the sum
Given an array of integers, perform at most K operations so that the sum of elements of a final array is minimum. An operation is defined as follows –
Consider any 1 element from the array, arr[i].
Replace arr[i] by floor(arr[i]/2).
Perform the next operations on the updated array.
The task is to minimize the sum after utmost K operations.
Constraints
1 <= N, K <= 10^5.
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.
The second line contains N space-separated integers denoting the elements of the array, arr.
Output
Print a single integer denoting the minimum sum of the final array.
Input
4 3
20 7 5 4
Output
17
Explanation
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].
Operation 3 -> Select 7. Replace it with 3. New array = [5,3,5,4].
Sum = 17.
Possible Solution
Input:
4 3
20 7 5 4
@@coding::5@@
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 accommodate Train B on the same platform as Train A.
Constraints
1 <= N <= 10^5
0 <= a <= 86400
0 < b <= 86400
Number of platforms > 0
Input
The first line contains N denoting the number of trains.
Next N line contains 2 integers, a and b, denoting the arrival time and stoppage time of the train.
Output
A single integer denotes the minimum number of platforms needed to accommodate every train.
Example 1
Input
3
10 2
5 10
13 5
Output
2
Explanation
The earliest arriving train at time t = 5 will arrive at platform# 1. Since it will stay there till t = 15, the train arriving at time t = 10 will arrive at platform# 2. Since it will depart at time t = 12, the train arriving at time t = 13 will arrive at platform# 2.
Example 2
Input
2
2 4
6 2
Output
2
Explanation
Platform #1 can accommodate train 1.
Platform #2 can accommodate train 2.
Note that the departure of train 1 is the same as the arrival of train 2, i.e. 6, and thus we need a separate platform to accommodate train 2.
Possible Solution
Input:
2
2 4
6 2
@@coding::6@@
Count Pairs
Given an array of integers A, and an integer K find a number of happy elements.
Element X is happy if there exists at least 1 element whose difference is less than K i.e. an element X is happy if there is another element in the range [X-K, X+K] other than X itself.
Constraints
1 <= N <= 10^5
0 <= K <= 10^5
0 <= A[i] <= 10^9
Input
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
Print a single integer denoting the total number of happy elements.
Example 1
Input
6 3
5 5 7 9 15 2
Output
5
Explanation
Other than number 15, everyone has at least 1 element in the range [X-3, X+3]. Hence they are all happy elements. Since these five are in number, the output is 5.
Example 2
Input
3 2
1 3 5
Output
3
Explanation
All numbers have at least 1 element in the range [X-2, X+2]. Hence they are all happy elements. Since these three are in number, the output is 3.
Possible Solution
Input:
3 2
1 3 5
@@coding::7@@
Critical Planets
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 planets (represented by integers) so that these planets surrender due to the shortage of food and supplies. The Jedi council has taken note of the situation and they have assigned Jedi Knight Skywalker and his Padawan Ahsoka to save the critical planets from blockade (Those planets or systems of planets which can be accessed by only one path and may be lost if that path is blocked by separatist).
Skywalker is preparing with the clone army to defend the critical paths. He has assigned Ahsoka to find the critical planets. Help Ahsoka to find the critical planets(C) in ascending order. You only need to specify those planets which have only one path between them and they cannot be accessed by any other alternative path if the only path is compromised.
Constraints
M <= 10000
N <= 7000
Input
The first line contains two space-separated integers M and N, where M denotes the number of paths between planets and N denotes the number of planets.
Next M lines, each contains two space-separated integers, representing the planet numbers that have a path between them.
Output
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
Time Limit
1
Example 1
Input
3 4
0 1
1 2
2 3
Output
0
1
2
3
Explanation
Since all the planets are connected with one path and cannot be accessed by any alternative paths hence all the planets are critical.
Example 2
Input
7 6
0 2
0 1
1 2
2 3
4 5
3 4
3 5
Output
2
3
Explanation
If the republic loses the path between 2 and 3 then the two systems of planets will not be able to communicate with each other. Hence 2 and 3 are critical planets.
Possible Solution:
Input
3 4
0 1
1 2
2 3
@@coding::8@@
Practising the previous year’s TCS CodeVita questions will better understand the standard of questions asked.
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.
Input Format: The 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
Sample Possible solution:
#include <stdio.h>nint prime(int b)n{n int j,cnt;n cnt=1;n for(j=2;j<=b/2;j++)n {n if(b%j==0)n cnt=0;n }n if(cnt==0)n return 1;n elsen return 0;n}nint main() {n int i,j,n,cnt,a[25],c,sum=0,count=0,k=0;n scanf("%d",&n);n for(i=2;i<=n;i++)n {n cnt=1;n for(j=2;j<=n/2;j++)n {n if(i%j==0)n cnt=0;n }n if(cnt==1)n {n a[k]=i;n k++;n }n }n for(i=0;i<k;i++)n {n sum=sum+a[i];n c= prime(sum);n if(c==1)n count++;n }n printf("%d",count);n return 0;n}n
Output:
20n2n
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