TCS_BATCH_2025_updated_PYQ
TCS_BATCH_2025_updated_PYQ
Array Questions
Q1. Largest and smallest element in an array. Link done
Q2. Second largest and second smallest element in an array. Link done
Q3. Check if the array is sorted. Link done
Q3. Left rotate an array by K places. Link done
Q4. Right rotate an array by K places. done
Q5. Reverse the array. Link done
Q6. Find missing number in an array. Link done
Q7. Find number that appears one and other appears twice. Link done
Q8. Move all zero to end of the array. Link done
Q9. Longest sub array with given sum. Link done*
Q10. Maximum Sub array sum (Kadane’s Algorithm) Link done
Q11. 2 sum problem. Link done*
Q12. Sort an array of 0’s, 1’s and 2’s (Dutch National Flag Problem). Link done *
Q13. Majority Element in an array. Link done *
Q14. Leader in an array. Link done
Q15. Maximum sum sub array of size k (Sliding window technique). Link done*
Q16. Implement Upper Bound Link done*
Q19. Find first and last position of element in an sorted array. Link done*
Strings Questions
Q1. Check if string is palindrome or not. Link done
Q2. Check if strings are anagram or not. Link done
Q3. Pangram checking of given string. Link
Q4. Count vowel in a string. Link done*
Q5. Convert Lower to upper case and upper to lower case in a string. Link done
Q6. Longest common prefix. Link
Q7. Count number of sub string. Link
Q8. Remove spaces from a given string. Link done
Q9. Encrypt the string. Link done *
Q10. Check if string is sub string of another. Link
Q11. Conversion of String to Integer Link
Q2. Given an integer array Arr of size N the task is to find the count of elements whose value
is greater than all of its prior elements.
Note : 1st element of the array should be considered in the count of the result. For example,
Arr[]={7,4,8,2,9} As 7 is the first element, it will consider in the result. 8 and 9 are also the
elements that are greater than all of its previous elements. Since total of 3 elements is
present in the array that meets the condition.
Hence the output = 3.
Example 1:
Input 5 -> Value of N, represents size of Arr
7-> Value of Arr[0]
4 -> Value of Arr[1]
8-> Value of Arr[2]
2-> Value of Arr[3]
9-> Value of Arr[4]
Output : 3
Q3. At a fun fair, a street vendor is selling different colours of balloons. He sells N number of
different colours of balloons (B[]). The task is to find the colour (odd) of the balloon which is
present odd number of times in the bunch of balloons.
Note: If there is more than one colour which is odd in number, then the first colour in the
array which is present odd number of times is displayed. The colours of the balloons can all
be either upper case or lower case in the array. If all the inputs are even in number, display
the message “All are even”.
Example 1: 7 -> Value of N [r,g,b,b,g,y,y] -> B[] Elements B[0] to B[N-1], where each input
element is sepārated by ṉew line.
Output : r -> [r,g,b,b,g,y,y] -> “r” colour balloon is present odd number of times in the bunch.
Explanation:
From the input array above: r: 1 balloon g: 2 balloons b: 2 balloons y : 2 balloons Hence , r is
only the balloon which is odd in number.
Q4. You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and
two integers m and n,
representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside
the array nums1. To
accommodate this, nums1 has a length of m + n, where the first m elements denote the
elements that should be
merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
Q5. A party has been organized on cruise. The party is organized 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.
Example 1:
Input :
5 -> Value of T
[7,0,5,1,3] -> E[], Element of E[0] to E[N-1], where input each element is separated by new
line
[1,2,1,3,4] -> L[], Element of L[0] to L[N-1], while input each element is separate by new line.
Output :
8 -> Maximum number of guests on cruise at an instance.
Q6. There are total n number of Monkeys sitting on the branches of a huge Tree. As travelers
offer Bananas and Peanuts, the
Monkeys jump down the Tree. If every Monkey can eat k Bananas and j Peanuts. If total m
number of Bananas and p number
of Peanuts are offered by travelers, calculate how many Monkeys remain on the Tree after
some of them jumped down to eat.
At a time one Monkeys gets down and finishes eating and go to the other side of the road.
The Monkey who climbed down
does not climb up again after eating until the other Monkeys finish eating.
Monkey can either eat k Bananas or j Peanuts. If for last Monkey there are less than k
Bananas left on the ground or less than j
Peanuts left on the ground, only that Monkey can eat Bananas(<k) along with the
Peanuts(<j).
Write code to take inputs as n, m, p, k, j and return the number of Monkeys left on the Tree.
Where, n= Total no of Monkeys
k= Number of eatable Bananas by Single Monkey (Monkey that jumped down last may get
less than k Bananas)
j = Number of eatable Peanuts by single Monkey (Monkey that jumped down last may get
less than j Peanuts)
m = Total number of Bananas
p = Total number of Peanuts
Remember that the Monkeys always eat Bananas and Peanuts, so there is no possibility of k
and j having a value zero.
Q7. The Caesar cipher is a type of substitution cipher in which each alphabet in the plaintext
or messages is shifted by a number of places down the alphabet.
For example, with a shift of 1, P would be replaced by Q, Q would become R, and so on. To
pass an encrypted message from one person to another, it is first necessary that both parties
have the ‘Key’ for the cipher, so that the sender may encrypt and the receiver may decrypt
it. Key is the number of OFFSET to shift the cipher alphabet. Key can have basic shifts from 1
to 25 positions as there are 26 total alphabets.
As we are designing custom Caesar Cipher, in addition to alphabets, we are considering
numeric digits from 0 to 9. Digits can also be shifted by key places.
For Example, if a given plain text contains any digit with values 5 and key =2, then 5 will be
replaced by 7, “- ”(minus sign) will remain as it is.
Key value less than 0 should result into “INVALID INPUT”
Example 1: Enter your Plaintext: All the best
Enter the Key: 1
The encrypted Text is: Bmm uif Cftu
Q8. Sort one array according to another array You are given two arrays a[] (integer) and b[]
(char). The ith value of a[] corresponds to the ith value of b[].
Sort the array b[] with respect to a[].
Note: The output is whitespace and newline character sensitive. After every character print
a whitespace character. Also do not print any newline character at any point.
Example 1: Input: a[] = {3, 1, 2} b[] = {'G', 'E', 'K'}
Output: E K G
Explanation: Here 3 corresponds to G, 1 corresponds to 'E', 2 corresponds to 'K'[3,1,
Q9. There are total n number of Monkeys sitting on the branches of a huge Tree. As travelers
offer Bananas and Peanuts, the
Monkeys jump down the Tree. If every Monkey can eat k Bananas and j Peanuts. If total m
number of Bananas and p number
of Peanuts are offered by travelers, calculate how many Monkeys remain on the Tree after
some of them jumped down to eat.
At a time one Monkeys gets down and finishes eating and go to the other side of the road.
The Monkey who climbed down
does not climb up again after eating until the other Monkeys finish eating.
Monkey can either eat k Bananas or j Peanuts. If for last Monkey there are less than k
Bananas left on the ground or less than j
Peanuts left on the ground, only that Monkey can eat Bananas(<k) along with the
Peanuts(<j).
Write code to take inputs as n, m, p, k, j and return the number of Monkeys left on the Tree.
Where, n= Total no of Monkeys
k= Number of eatable Bananas by Single Monkey (Monkey that jumped down last may get
less than k Bananas)
j = Number of eatable Peanuts by single Monkey(Monkey that jumped down last may get
less than j Peanuts)
m = Total number of Bananas
p = Total number of Peanuts
Remember that the Monkeys always eat Bananas and Peanuts, so there is no possibility of k
and j having a value zero
Example 1:
Input Values -> 20 2 3 12 12
Output Values-> Number of Monkeys left on the tree:10
Note: Kindly follow the order of inputs as n,k,j,m,p as given
in the above example. And output must include the same
format as in above example(Number of Monkeys left on the
Tree:)
For any wrong input display INVALID INPUT
Q10. Airport security officials have confiscated several item of the passengers at the security
check point. 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 represent 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[0] 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
Q11. You are given an array prices where prices[i] is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and
choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve
any profit, return 0.
Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Q12. Jack is always excited about sunday. It is favourite day, when he gets to play all day. And
goes to
cycling with his friends.
So every time when the months starts he counts the number of sundays he will get to enjoy.
Considering
the month can start with any day, be it Sunday, Monday…. Or so on.
Count the number of Sunday jack will get within n number of days.
Example 1:
Input
mon-> input String denoting the start of the month.
13 -> input integer denoting the number of days from the start of the month.
Output :
2 -> number of days within 13 days.
Q14. Problem Description -: 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.
Cases not allowed –
After finding 3 palindromes using above instructions, if any character of the original string
remains unconsumed.
No character may be shared in forming 3 palindromes.
Constraints
1 <= the length of input sting <= 1000
Input
First line contains the input string consisting of characters between [a-z].
Output
Print 3 substrings one on each line.
Time Limit
1
Examples
Input -> nayannamantenet
Output->
nayan
naman
telnet
Q15. Problem Statement – An automobile company manufactures both a two wheeler (TW)
and a four wheeler (FW). A company manager wants to make the production of both types
of vehicle according to the given data below:
1st data, Total number of vehicle (two-wheeler + four-wheeler)=v
2nd data, Total number of wheels = W
The task is to find how many two-wheelers as well as four-wheelers need to manufacture as
per the given data.
Example :
Input :
200 -> Value of V
540 -> Value of W
Output :
TW =130 FW=70
Explanation:
130+70 = 200 vehicles
(70*4)+(130*2)= 540 wheels
Constraints :
2<=W
W%2=0
V<W
Print “INVALID INPUT” , if inputs did not meet the constraints.
The input format for testing
The candidate has to write the code to accept two positive numbers separated by a new
line.
First Input line – Accept value of V.
Second Input line- Accept value for W.
Q16. A parking lot in a mall has RxC number of parking spaces. Each parking space will either
be empty(0) or full(1). The status (0/1) of a parking space is represented as the element of
the matrix. The task is to find index of the prpeinzta row(R) in the parking lot that has the
most of the parking spaces full(1).
Note :
RxC- Size of the matrix
Elements of the matrix M should be only 0 or 1.
Example 1:
Input :
3 -> Value of R(row)
3 -> value of C(column)
[0 1 0 1 1 0 1 1 1] -> Elements of the array M[R][C] where each element is separated by new
line.
Output :
3 -> Row 3 has maximum number of 1’s
Q17. 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, it decides
time and water level which can be changed by menus given on the machine control area.
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.
Write a function which takes a numeric weight in the range [0,7000] as input and
produces estimated time as output is: “OVERLOADED”, and for all other inputs, the
output statement is
“INVALID INPUT”.
Example:
Input value
2000
Output value
Time Estimated: 25 minutes
Q18.
Problem Statement
A doctor has a clinic where he serves his patients. The doctor’s consultation fees are
different for different groups of patients depending on their age. If the patient’s age is
below 17, fees is 200 INR. If the patient’s age is between 17 and 40, fees is 400 INR. If
patient’s age is above 40, fees is 300 INR. Write a code to calculate earnings in a day for
which one array/List of values representing age of patients visited on that day is passed
as input.
Note:
Q19. 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”.
Constrains-
1<=N<=100
Example 1:
Input :
10 -> Integer
Output :
Explanation:
Binary representation of 10 is 1010. After toggling the bits(1010), will get 0101 which
represents “5”. Hence output will print “5”.
Q20. A supermarket maintains a pricing format for all its products. A value N is printed
on each product. When the scanner reads the value N on the item, the product of all the
digits in the value N is the price of the item. The task here is to design the software such
that given the code of any item N the product (multiplication) of all the digits of value
should be computed(price).
Example 1:
Input :
Output :
160 -> Price
Explanation:
5*2*4*4= 160
Note :
If ‘L’ is not a multiple of N, the remaining number of curtains should be considered as a
substring too. In simple words, after dividing the curtains in sets of ‘L’, any curtains left
will be another set(refer example 1)
Example 1:
Input :
3 -> Value of L
Output:
Explanation:
Set 1: {b,b,b}
Set 2: {a,a,a}
Set 3: {b,a,b}
Among all the sets, Set 2 has more number of a’s. The number of a’s in set 2 is 3.
Q22. An international round table conference will be held in india. Presidents from all
over the world representing their respective countries will be attending the conference.
The task is to find the possible number of ways(P) to make the N members sit around
the circular table such that.
The president and prime minister of India will always sit next to each other.
Example 1:
Input :
4 -> Value of N(No. of members)
Output :
Explanation:
Rest of the members can be arranged in (4-1)! ways.(1 is subtracted because the
previously selected two members will be considered as single members now).
So total possible ways 4 members can be seated around the circular table 2*6= 12.
Example 2:
Input:
Output :
Explanation:
Rest of the members can be arranged in (10-1)! Ways. (1 is subtracted because the
previously selected two members will be considered as a single member now).
So, total possible ways 10 members can be seated around a round table is
Example 2:
Input: nums = [2,1,4]
Output: true
Explanation:
There is only two pairs: (2,1) and (1,4), and both of them contain numbers with
different parity. So the answer is true.
Example 3:
Input: nums = [4,3,1,6]
Output: false
Explanation:
nums[1] and nums[2] are both odd. So, the answer is false.
Q24) Given an array of integers and a sum, the task is to count all subsets of
given array with sum equal to given sum.
Input :
The first line of input contains an integer T denoting the number of test cases. Then
T test cases follow. Each test case contains an integer n denoting the size of the
array. The next line contains n space separated integers forming the array. The last
line contains the sum.
Output :
Count all the subsets of given array with sum equal to given sum.
NOTE: Since result can be very large, print the value modulo 109+7.
Constraints :
1<=T<=100
1<=n<=103
1<=a[i]<=103
1<=sum<=103
Example :
Input :
2 3 5 6 8 10
10
12345
10
Output :
Explanation :
Q25) Before the outbreak of corona virus to the world, a meeting happened in
a room in Wuhan. A person who attended that meeting had COVID-19 and no
one in the room knew about it! So everyone started shaking hands with
everyone else in the room as a gesture of respect and after meeting
unfortunately every one got infected! Given the fact that any two persons
shake hand exactly once, Can you tell the total count of handshakes happened
in that meeting?
Input Format :
The first line contains the number of test cases T, T lines follow.
Each line then contains an integer N, the total number of people attended that
meeting.
Output Format :
Constraints :
Sample Input :
Output :
Explanation :
Q26) For enhancing the book reading, the school distributed story books to
students as part of the Children’s Day celebrations. To increase the reading
habit, the class teacher decided to exchange the books every week so that
everyone will have a different book to read. She wants to know how many
possible exchanges are possible.
If they have 4 books and students, the possible exchanges are 9. Bi is the book of i-
th student and after the exchange, he should get a different book, other than Bi.
B2 B3 B4 B1
B2 B4 B1 B3
B3 B1 B4 B2
B3 B4 B1 B2
B3 B4 B2 B1
B4 B1 B2 B3
B4 B3 B1 B2
B4 B3 B2 B1
Find the number of possible exchanges, if the books are exchanged so that every
student will receive a different book.
Constraints
Input Format
Input contains one line with N, indicates the number of books and number of
students.
Output Format
Sample Input :
Sample Output :
Q27) You are given a string A and you have to find the number of different sub-
strings of the string A which are fake palindromes.
Note:
1. Palindrome: A string is called a palindrome if you reverse the string yet the order
of letters remains the same. For example, MADAM.
4. Two sub-strings are considered same if their starting indices and ending indices
are equal.
Input Format:
Output Format:
Constraints:
Sample Input 1:
ABAB
Sample Output 1:
Explanation:
The fake palindrome for the string ABAB are A, B, A, B, ABA, BAB, ABAB.
Sample Input 2:
AAA
Sample output 2:
Explanation:
The fake palindrome for the string AAA are A, A, A, AA, AA, AAA
Q 28. Problem Statement:
You are given a binary tree with N nodes where each node contains a positive
integer value. The goal is to find the maximum sum path from the root to any leaf
node. However, you need to ensure that the sum of the values along the path
is not divisible by a given integer K.
Input:
• The first line of input contains an integer N, the number of nodes in the tree.
• The second line contains N space-separated integers representing the values
of the nodes.
• The third line contains N-1 pairs of integers u and v, representing the edges of
the tree (the tree is rooted at node 1).
• The fourth line contains a single integer K, the divisor.
Output:
• Print the maximum sum of the path from the root to any leaf node such that
the sum is not divisible by K. If no such path exists, print -1.
Input:
7 3 4 8 2 1 6 10 1 2 1 3 2 4 2 5 3 6 3 7 5
Output:
21
Explanation:
• The possible paths from the root (node 1) to the leaves are:
o 3 -> 4 -> 2 (sum = 9)
o 3 -> 4 -> 1 (sum = 8)
o 3 -> 8 -> 6 (sum = 17)
o 3 -> 8 -> 10 (sum = 21)
• The path with the maximum sum that is not divisible by 5 is 3 -> 8 -> 10, with a
sum of 21.
•
Notes: