TCS Codevita Previous Year Questions
TCS Codevita Previous Year Questions
Question -:You are given a binary string B of length L which contains K ones and
remaining zeros. You are required to place the K ones in the binary string in such a way
that the longest consecutive zeros have the least length possible. Once such a binary
string is constructed, you are required to print the length of the contiguous block of
zeros, which has the largest length.
Constraints
0 <= K <= L
Input
Output
Print a single integer denoting the length of the longest consecutive zeros as per the
problem.
Examples
Example 1
Input
31
Output
Explanation
So the possible strings as per the problem are 010, 001, 100.
In the first case, the maximum length of consecutive zeros is 1 whereas in the other two
cases it is 2. Hence the constructed binary string is 010 and the output is 1.
Example 2
Input
33
Output
Explanation
B is of length 3 and it has all three one’s. There is no block of zeros, hence the output is
0.
Question -: Consider a set of web pages, numbered from 1 to N. Each web page has
links to one or more web pages. Clicking on a link in a page, takes one to the other web
page. You are provided numbers of two web pages viz, starting web page and end web
page. Your task is to find the minimum number of clicks required to reach the end page
from the start page. If end page cannot be reached from start page, print -1 as the
output. For better understanding refer Examples section.
Constraints
0 < L < 10
Input
Next N lines contain L space separated integers depicting linked webpage number(s)
from that webpage
Output
Print the minimum number of clicks required to open the end page from start page. If
not possible, print -1 as output.
Example 1
Input
24
15
23
5
23
Output
Explanation:
Second line conveys that there are links from page 1 to pages 2 and 4.
Fourth line conveys that there are links from page 3 to pages 1 and 5.
Fifth line conveys that there are links from page 4 to pages 2 and 3.
Sixth line conveys that there is a links from page 5 to page 5 itself.
From page 2, we can open only page 1. From page 1, we can open page 4. From page
4, we can open page 3. So, minimum 3 clicks are required, and this is the output.
Example 2
Input
1
23
Output
-1
Explanation:
Second line conveys that there are links from page 1 to page 2.
Fourth line conveys that there are links from page 3 to page 1.
Question -: A picnic to a famous museum is being planned in a school for class VI.
When they reached the spot, the students started quarreling among themselves in the
queue. So the teacher came up with an idea of “good string” which is explained below.
Good String is provided as input. All letters in this string are good letters. Good letters
need to be used in further computations as explained below.
The teacher asked all the students to convert their names into good names with the
help of good string. While converting, they have to calculate the distance. Based on
that, she will arrange the students in a queue.
For converting a name into good name, for each letter i in the name, select the nearest
letter from the good name. Distance is calculated as the differences between the ASCII
values of i and selected good letter. If there are two letters which are equidistant from i,
select the letter which is nearest to the previously used good letter. In that case,
distance will be the difference of ASCII value of previously used good letter and
selected letter. If i is already present in the good string then no need to change it.
Initially, previous good letter will be the first letter of good string. Calculate the total
distance of the given name.
Given the name of the student who is confused of implementing this task. Help him to
calculate the total distance for his name.
Note: Letters from good string can be reused any number of times.
Constraints
1 <= len(good string) <= 100
1 <= len(name) <= 10^4
Good string will consist of lower, upper case alphabets, digits and symbols.
Name will consist of only space, lower and upper case alphabets.
Characters are case sensitive.
The ASCII values for all the characters in the good string and name will be between 32
to 126 (both inclusive).
Input
First line consists of good string.
Second line consists of the name of the student who is confused to implement the task.
Output
Print the total distance for that name.
Examples
Example 1
Input
(@HR*i{kcQl
Vyom
Output
10
Explanation
i
Previous good letter
Current good letter for i
V
(
R
y
R
{
o
{
l
m
l
l
The total distance will be |ASCII(V)-ASCII(R)| + |ASCII(y)-ASCII({)| + |ASCII(o)-ASCII(l)|
+ |ASCII(m)-ASCII(l)| = 4+2+3+1 = 10.
Example 2
Input
6*K4AQf]gpi
Nainika
Output
33
Explanation
i
Previous good letter
Current good letter for i
N
6
K
a
K
]
i
–
–
n
]
p
i
–
–
k
p
i
a
i
]
Initially, Previous good letter=6. Since K and Q are at the same distance from N, so we
select the character which is nearest to previous letter(6) which is K.
i is already present in the good string. So no need to change anything.
Therefore, total distance will be |ASCII(6)-ASCII(K)| + |ASCII(a)-ASCII(])| +
|ASCII(n)-ASCII(p)| + |ASCII(k)-ASCII(i)| + |ASCII(a)-ASCII(])| = 21+4+2+2+4 = 33.
Question -: A math game is introduced in a school competition to test the skills of
students. The game deals with Prime numbers.
The game rules are as follows:
● From the given set of distinct natural numbers as input, consider the smallest
natural number as q.
● Your task is to compute the smallest prime number (p) such that when p is
divided by all the distinct numbers in the input, except q, should result q as the
remainder.
Constraints :
● 1 < n < 11
● p < 10 ^ 10
Input :
Output :
Example 1 :
Input :
3451
Output : 61
Explanation : Here the n+1 numbers are 3, 4, 5 and 1 where q=1 (the least of the
numbers)
The smallest number that leaves remainder 1 when divided by 3, 4 and 5 is 61 and is
prime. Hence, output is 61.
Example 2 :
Input :
3452
Output : None
Explanation : Here q=2. Any number that when divided by 4 leaving remainder 2 must
be an even number e.g., 6, 10, 14 etc. Hence it can’t be prime. Hence, output is “None”.
1. Cups can be used integral number of times i.e., zero or more times, but never
partially i.e., a cup of 1L can be used 0, 1, 2 etc. times, but never 0.5, 1.5, 2.5 ..
times
2. The Jug must not overflow because of cup filling the Jug
3. The number of distinct cups (i.e., different cup sizes) used to fill the Jug must be
maximized
4. The summation of number of times all cups are used must be minimized.
optimisation goals.
For better understanding of how cups can be used to fill the Jug, go through the
Examples section. Both examples clearly explain, when there are multiple ways to
achieve the objective, what is the correct answer and why.
Constraints :
0 < N < 10
0 < Ci < L
Input :
Second line contains N space separated integers denoting the capacity of the cups.
Third line contains an integer L which denotes the capacity of Jug in liters.
Output :
Second line must comprise of equal number of space delimited integers which denote
the frequency i.e. the number of times the corresponding cup in first line is used to fill
the Jug.
Example 1 :
Input :
3 7 10 11
88
Output :
3 7 10 11
1261
Explanation :
The first and second lines indicate that you are provided with 4 cups of capacities – 3
liters, 7 liters, 10 liters and 11 liters. The third line indicates that the capacity of the Jug
is 88 liters.
523
i.e., one can use 7L cup for 5 times to get 35L. Next one can use the 10L cup twice.
After that the Jug will contain 55L. Finally, one can use 11L cup thrice. Thus, the Jug will
be filled. However, this solution uses cups of only 3 different capacities when 4 different
capacities are available. Hence the Jug is perhaps not filled according to the
specification. Let’s see if we can achieve our objective by using all 4 cup sizes.
3 7 10 11
1261
Example 2 :
Input :
2 5 10
50
Output :
2 5 10
523
Explanation :
The first and second lines indicate that you are provided with 3 cups of capacities – 2
liters, 5 liters, 10 liters. The third line indicates that the capacity of the Jug is 50 liters.
Here one can easily fill the Jug by using the 10L cup 5 times. However, this does not
obey the specifications. According to the specifications, one must use all available cups
of capacity 2L, 5L and 10L. If there are multiple ways in which the Jug can be filled by
using maximum number of distinct sized cups, then as per specifications one needs to
minimize the summation of number of times cups are used.
Solution 1
2 5 10
15 2 1
Solution 2
2 5 10
523
Both solutions use all available cups. However, sum of frequencies in Solution 1 is 18
(15 + 2 + 1), whereas sum of frequencies in Solution 2 is 10 (5 + 2 + 3). Solution 2
minimizes the summation of number of times any cup in used. This is also a
requirement as per the specification. Hence Solution 2 is the correct answer.
Question -: This is a resource to task allocation problem. There are two types of tasks
viz. one that the human needs to do and the other which as amenable to machine
processing. A human-oriented task cannot be performed by machine and vice-versa.
Also, assume that all human workers are equally efficient. So, the same task will be
performed in the same time, irrespective of which human worker does that task.
Given the number of workers, number of tasks and time taken to finish each task,
compute which task was finished at what time and which worker performed that task.
Some rules and conventions that need to be adhered to while doing task allocation to
workers are as below.
● Workers and tasks are identified by their suffixes i.e., Worker 1 is W1 and Task 1
is T1.
● A worker will still need to be assigned even if a task can be done only by the
machine
before T2
● Initially when all workers are free, worker Wi will be allocated task before worker
Wi+1 i.e., in general a worker with lower suffix will be assigned to the task before
processed lower suffix task will get the next task allocated prior to the other
workers.
Refer Examples section for better understanding of how tasks are allocated to workers
Compute which task was completed at what time and which worker performed that task.
The output specifications provide information on how output should be printed.
Constraints :
● 1 <= N <= 10
Input :
Next M lines contain a task information tuple as described in the previous section.
Output :
Example 1 :
Input :
T1 Machine
T2 Manual 5
T3 Machine
T4 Machine
Output :
W1 T1 0
W3 T3 0
W1 T4 0
W2 T2 5
Explanation :
First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 at the 0th minute,
respectively.
Now, Workers available for pending tasks in 0th minute = {W1, W3}
Here, W1 will be assigned with next pending task first, because the worker W1
completed task with lower suffix (T1) than W3 (T3).
W1 will be assigned the next task T4 at 0th minute which is a Machine-bound task
which will also be completed in 0th minute.
As the number of pending tasks are zero, no further assignment takes place.
First line of output is “W1 T1 0”, where T1 is the task completed by Worker W1 0th
minute.
Second line of output is “W3 T3 0”, where T3 is the task completed by Worker W3 in 0th
minute.
Third line of output is “W1 T4 0”, where T4 is the task completed by worker W1 in 0th
minute.
Fourth line of output is “W2 T2 5”, where T2 is the task completed by W2 in 5th minute.
So, the output is as follows:
W1 T1 0
W3 T3 0
W1 T4 0
W2 T2 5
Example 2 :
Input :
T1 Machine
T2 Machine
T3 Machine
T4 Machine
Output :
W1 T1 0
W2 T2 0
W3 T3 0
W1 T4 0
Explanation :
First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 in 0th minute,
respectively.
Here, T1, T2 and T3 are Machine-bound and will be completed in 0th minute, after
completion of these tasks W1, W2, W3 will be available for the remaining tasks.
Since W1 is the first worker in the list of available workers at 0th minute. W1 will pick up
the 4th task and will complete it in 0th minute, because it is also a machine bounded
task.
W1 T1 0
W2 T2 0
W3 T3 0
W1 T4 0
Question – : 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 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 = loanAmount * monthlyInterestRate / ( 1 – 1 / (1 +
monthlyInterestRate)^(numberOfYears * 12))
Constraints:
Input Format:
● 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
● Example 2
○ Input
○ 500000
○ 26
○ 3
○ 13 9.5
○ 3 6.9
○ 10 5.6
○ 3
○ 14 8.5
○ 6 7.4
○ 6 9.6
● Output: Bank A
Question -: Snake and Ladder is a board game consisting of snakes and ladders,
where the player must reach End position, starting from Start position. Here, snake on
the board makes player demotes the player to a lower numbered square and ladder
promotes player to higher numbered square on the board.
For e.g., given below is the snake and ladder board, where S(‘pos’) represents snake
and ‘pos’ indicates where the player’s coin will move down to once a player lands on
that square. Similarly, L(‘pos’) indicates ladder and ‘pos’ indicates where the player’s
coin will move up to once the player lands on that square. Player always starts from the
Start square and must go towards End based on the rolls of the die.
You are supposed to find if it is possible for a player to reach the End or not, based on
die inputs. If it is possible, display ‘Possible’ with number of snakes and ladders
encountered during his/her play else display ‘Not possible’ along with number of
snakes, number of ladders and square where the player’s coin has ended at.
Note: A player can reach the end if he has the exact number on die input to reach end.
For e.g., if the player is at 99, he/she can reach end only if the die input is 1 and not any
other input. So, he/she must wait till input on die is 1.
The actual Snake and Ladder board will look as depicted below. This format will be
used for providing inputs.
End 99 S(2) 97 96 95 94 93 92 91
81 82 83 84 85 86 87 88 89 90
80 79 78 77 76 L(99) 74 73 72 71
61 62 S(22) 64 65 66 67 68 69 70
60 59 58 57 56 55 54 53 52 51
41 42 43 44 L(68) 46 47 48 49 50
40 39 S(6) 37 36 35 34 33 32 31
21 22 23 24 25 26 27 28 29 30
20 19 18 17 S(9) 15 14 13 12 11
Start 2 3 4 5 6 7 8 9 10
Constraints :
Input :
First 10 lines contains snake and ladder board where each line has 10 tokens separated
by a space. The tokens can either be integers, Start, End, S(number), L(number) where
● Start denotes the left bottom position on the board from where player start the
game
● S(number) denotes that the current square has a snake that will take you down
● L(number) denotes that the current square has a ladder that will take you up to a
Output :
Find if the player is possible to reach the End or not, based on die_inputs and the
board. If it is possible, display ‘Possible’ with number of snakes and ladders
encountered during his/her play else display ‘Not possible’ along with number of
snakes, number of ladders and the square where the player’s coin has ended at.
Example 1 :
Input :
End S(Start) 98 97 96 95 94 93 92 91
81 82 83 84 L(98) 86 87 88 89 90
80 79 S(46) 77 76 75 74 73 72 71
61 62 63 64 65 66 67 68 69 70
60 59 58 57 56 55 S(25) 53 52 51
41 42 43 L(62) 45 46 47 48 49 50
40 39 38 37 36 35 34 33 32 31
21 22 23 L(74) 25 26 27 28 29 30
20 19 18 17 S(2) 15 14 13 12 11
Start 2 3 4 5 6 7 8 9 10
54241
Output :
Not possible 1 0 2
Explanation :
Based on die inputs, the player moves from start and goes to number 5 first on board
then to 9, 11, 15 and finally to S(2) i.e., square numbered 16. Now player encountered
snake which takes him to square numbered 2. So, output is ‘Not possible’, as the player
couldn’t reach the End and the number of snakes and ladders encountered during
his/her play are one and zero respectively.
Example 2 :
Input :
End 99 98 S(7) 96 95 94 93 92 91
81 82 L(99) 84 85 86 87 88 89 90
80 79 78 77 76 75 74 73 72 71
61 S(22) 63 64 65 66 67 68 69 70
60 59 58 S(14) 56 57 54 53 52 51
41 42 43 44 45 46 L(80) 48 49 50
40 39 38 37 36 35 34 33 32 31
21 22 23 L(63) 25 26 27 28 29 30
20 19 S(2) 17 16 15 14 13 12 11
Start 2 3 4 5 6 7 8 9 10
66654366656431
Output :
Possible 1 2
Explanation :
Based on the die inputs, the player encountered 1 snake and 2 ladders and was able to
reach the End.
Juan Marquinho 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 millions.
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.
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
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:
Example 1
● Input: 10 2
● 345 604 321 433 704 470 808 718 517 811
● 300 350
● 400 700
Output: 2 4
Explanation:
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)
and 4 samples in the second range (604, 433, 470, 517). Hence the two lines of the
output are 2 and 4
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:
There are 20 samples and 3 ranges. The samples are 921, 107 195. 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 are 1, 12 and 20 respectively. Hence the three lines
of the output are 1, 12 and 20.
Question : 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.
Constraints
Input
● First line contains the input string consisting of characters between [a-z].
Output
Time Limit
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
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
aaabaaa
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].
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
Input
● First line contains two integers N and K where N is size of the array and K is a
number as described above.
● Second line contains N integers separated by space.
Output
Example 1
Input
63
5 5 7 9 15 2
Output
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
32
135
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:
32
135
Question : Dr. Vishnu is opening a new world class hospital in a small town designed to
be the first preference of the patients in the city. Hospital has N rooms of two types –
with TV and without TV, with daily rates of R1 and R2 respectively.
However, from his experience Dr. Vishnu knows that the number of patients is not
constant throughout the year, instead it follows a pattern. The number of patients on any
given day of the year is given by the following formula –
(6-M)^2 + |D-15| ,
where M is the number of month (1 for jan, 2 for feb …12 for dec) and D is the date
(1,2…31).
All patients prefer without TV rooms as they are cheaper, but will opt for with TV rooms
only if without TV rooms are not available. Hospital has a revenue target for the first
year of operation. Given this target and the values of N, R1 and R2 you need to identify
the number of TVs the hospital should buy so that it meets the revenue target. Assume
the Hospital opens on 1st Jan and year is a non-leap year.
Constraints
● Hospital opens on 1st Jan in an ordinary year
● 5 <= Number of rooms <= 100
● 500 <= Room Rates <= 5000
● 0 <= Target revenue < 90000000
Input Format
● First line provides an integer N that denotes the number of rooms in the hospital
● Second line provides two space-delimited integers that denote the rates of rooms
with TV (R1) and without TV (R2) respectively
● Third line provides the revenue target
Output
● Minimum number of TVs the hospital needs to buy to meet its revenue target. If it
cannot achieve its target, print the total number of rooms in the hospital.
Test Case
Example-1 :
Input
20
1500 1000
7000000
Output
14
Explanation
Using the formula, the number of patients on 1st Jan will be 39, on 2nd Jan will be 38
and so on. Considering there are only twenty rooms and rates of both type of rooms are
1500 and 1000 respectively, we will need 14 TV sets to get revenue of 7119500. With
13 TV sets Total revenue will be less than 7000000
Example-2 :
Input
10
1000 1500
10000000
Output
10
Explanation
In the above example, the target will not be achieved, even by equipping all the rooms
with TV. Hence, the answer is 10 i.e. total number of rooms in the hospital.
Question : The parcel section of the Head Post Office is in a mess. The parcels that
need to be loaded to the vans have been lined up in a row in an arbitrary order of
weights. The Head Post Master wants them to be sorted in the increasing order of the
weights of the parcels, with one exception. He wants the heaviest (and presumably the
most valuable) parcel kept nearest his office.
You and your friend try to sort these boxes and you decide to sort them by interchanging
two boxes at a time. Such an interchange needs effort equal to the product of the
weights of the two boxes.
● The first line consists of two space-separated positive integers giving the number
of boxes (N) and the position of the Head Post Masters office (k) where the
heaviest box must be.
● The second line consists of N space-separated positive integers giving the
weights of the boxes. You may assume that no two weights are equal
Output Format:
● The output is one line giving the total effort taken to get the boxes in sorted order,
and the heaviest in position k.
Constraints:
Sample Input 1:
52
20 50 30 80 70
Sample Output 1:
3600
Question -: Imagine you are an MLA of a district and there are N number of villages in
your constituency.
Your job is to vaccinate all the people in your constituency in minimum amount of time.
There are two centres where vaccination is going on. First centre takes m1 minutes as
average time for vaccinating one person and second centre takes m2 minutes as
average time.
Population of every village is known to you prior to the vaccination drive. Schedule all
the villagers to any centre such that overall time for vaccinating all the people of all the
villages will be minimum. Assume that there is no wait time in between vaccinating two
people. Also, people belonging to the same village will need to be vaccinated in the
same centre.
For example:
v1 = 10, v2 = 30, v3 = 20
Hence,
minimum time required to vaccinate all the people will be = 120 min.
Constraints
Input
First line contains an integer m1 which is average time in minutes taken for vaccination
by the first centre
Second line contains an integer m2 which is average time in minutes taken for
vaccination by the second centre
Fourth line contains N space delimited integers denoting the population of villages
Output
Single integer value denoting the maximum time taken in minutes to vaccinate all
villagers from all villages in your constituency
Examples
Example 1
Input
5
10 50 20 30 40
Output
180
Explanation:
First centre takes 2 min as average time. Second centre takes 3 min as average time.
Your constituency has 5 villages.
Minimum time required to vaccinate all the people will be = 240 min
Minimum time required to vaccinate all the people will be = 180 min
Example 2
Input
1
100 90 70
Output
180
Explanation:
First centre takes 1 min as average time. Second centre takes 2 min as average time.
There are 3 villages in your constituency.
v1 = 100, v2 = 90, v3 = 70
Minimum time required to vaccinate all the people will be = 190 min
Question -: This is a resource to task allocation problem. There are two types of tasks
viz. one that the human needs to do and the other which as amenable to machine
processing. A human-oriented task cannot be performed by machine and vice-versa.
Also, assume that all human workers are equally efficient. So, the same task will be
performed in the same time, irrespective of which human worker does that task.
Given the number of workers, number of tasks and time taken to finish each task,
compute which task was finished at what time and which worker performed that task.
Some rules and conventions that need to be adhered to while doing task allocation to
workers are as below.
● Workers and tasks are identified by their suffixes i.e., Worker 1 is W1 and Task 1
is T1.
● A worker will still need to be assigned even if a task can be done only by the
machine
● Tasks need to be processed in sequential order i.e., T1 has to be allocated
before T2
● Initially when all workers are free, worker Wi will be allocated task before worker
Wi+1 i.e., in general a worker with lower suffix will be assigned to the task before
● If one or more workers are available at the same time, then the worker who
processed lower suffix task will get the next task allocated prior to the other
workers.
Refer Examples section for better understanding of how tasks are allocated to workers
Compute which task was completed at what time and which worker performed that task.
The output specifications provide information on how output should be printed.
Constraints :
● 1 <= N <= 10
Input :
Next M lines contain a task information tuple as described in the previous section.
Output :
● The tuple should be first sorted in ascending order of completion time and then in
Example 1 :
Input :
T1 Machine
T2 Manual 5
T3 Machine
T4 Machine
Output :
W1 T1 0
W3 T3 0
W1 T4 0
W2 T2 5
Explanation :
First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 at the 0th minute,
respectively.
Now, Workers available for pending tasks in 0th minute = {W1, W3}
Here, W1 will be assigned with next pending task first, because the worker W1
completed task with lower suffix (T1) than W3 (T3).
W1 will be assigned the next task T4 at 0th minute which is a Machine-bound task
which will also be completed in 0th minute.
As the number of pending tasks are zero, no further assignment takes place.
First line of output is “W1 T1 0”, where T1 is the task completed by Worker W1 0th
minute.
Second line of output is “W3 T3 0”, where T3 is the task completed by Worker W3 in 0th
minute.
Third line of output is “W1 T4 0”, where T4 is the task completed by worker W1 in 0th
minute.
Fourth line of output is “W2 T2 5”, where T2 is the task completed by W2 in 5th minute.
W1 T1 0
W3 T3 0
W1 T4 0
W2 T2 5
Example 2 :
Input :
T1 Machine
T2 Machine
T3 Machine
T4 Machine
Output :
W1 T1 0
W2 T2 0
W3 T3 0
W1 T4 0
Explanation :
First, Workers W1, W2, W3 will be assigned with tasks T1, T2, T3 in 0th minute,
respectively.
Here, T1, T2 and T3 are Machine-bound and will be completed in 0th minute, after
completion of these tasks W1, W2, W3 will be available for the remaining tasks.
Since W1 is the first worker in the list of available workers at 0th minute. W1 will pick up
the 4th task and will complete it in 0th minute, because it is also a machine bounded
task.
W1 T1 0
W2 T2 0
W3 T3 0
W1 T4 0
Questions - 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 subset formation and ranking logic is as described below
● Rank 1 will always be an empty set
● 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
● A subset with less elements will be ranked higher than a subset with more
● Refer Example 2 to get a better understanding of how subsets are formed and
ranked
● It is guaranteed that
● 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
create all possible subset of strings. 8 subsets can be formed from these strings. And
they are as follows:
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 subset (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
subsets.
Constraints:
0<N<=10^2
0<R<=10^18
Input
Second line contains an integer R, for which you have to find Rth subset from all legal
subsets.
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
Time Limit (secs)
1
Input
a,b
Output
a,b
Explanation:
{}-1
{a} -2
{b}-3
{a, b}-4
Question - 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:
Output Format:
Constraints:
Sample Input 1:
1
25
Sample Output 1:
10
Explanation:
R = 2, N = 5
Arrangements like
THANK YOU ❤️