Array_strings_TCS Questions
Array_strings_TCS Questions
Array Questions
Q1. Largest and smallest element in an array. Link
Q2. Second largest and second smallest element in an array. Link
Q3. Check if the array is sorted. Link
Q3. Left rotate an array by K places. Link
Q4. Right rotate an array by K places.
Q5. Reverse the array. Link
Q6. Find missing number in an array. Link
Q7. Find number that appears one and other appears twice. Link
Q8. Move all zero to end of the array. Link
Q9. Longest sub array with given sum. Link
Q10. Maximum Sub array sum (Kadane’s Algorithm) Link
Q11. 2 sum problem. Link
Q12. Sort an array of 0’s, 1’s and 2’s (Dutch National Flag Problem). Link
Q13. Majority Element in an array. Link
Q14. Leader in an array. Link
Q15. Maximum sum sub array of size k (Sliding window technique). Link
Strings Questions
Q1. Check if string is palindrome or not. Link
Q2. Check if strings are anagram or not. Link
Q3. Pangram checking of given string. Link
Q4. Count vowel in a string. Link
Q5. Convert Lower to upper case and upper to lower case in a string. Link
Q6. Longest common prefix. Link
Q7. Count number of sub string. Link
Q8. Remove spaces from a given string. Link
Q9. Encrypt the string. Link
Q10. Check if string is sub string of another. Link
Given a string S (input consisting) of ‘*’ and ‘#’. The length of the string is variable. The task is
to find the minimum number of ‘*’ or ‘#’ to make it a valid string. The string is considered
valid if the number of ‘*’ and ‘#’ are equal. The ‘*’ and ‘#’ can be at any position in the string.
Note: The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in the
input string.
(*>#): positive integer
(#>*): negative integer
(#=*): 0
Example 1:
Input 1: ###*** -> Value of S
Output : 0 → number of * and # are equal
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,
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:
Output: [1,2,2,3,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 :
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.
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)
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.
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[].
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.
Output: E K G
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.
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)
Remember that the Monkeys always eat Bananas and Peanuts, so there is no possibility of k and j
having a value zero
Example 1:
Tree:)
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
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 :
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
Return the maximum profit you can achieve from this transaction. If you cannot achieve
Example 1:
Output: 5
Q12. Jack is always excited about sunday. It is favourite day, when he gets to play all day. And goes to
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 :
difference of 1.
For example,
Input:
A = [5, 8, 3, 2, 1, 4], N = 6
Output: 5
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.
After finding 3 palindromes using above instructions, if any character of the original string remains
unconsumed.
Constraints
Input
First line contains the input string consisting of characters between [a-z].
Output
Time Limit
1
Examples
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:
The task is to find how many two-wheelers as well as four-wheelers need to manufacture as per the
given data.
Example :
Input :
Output :
TW =130 FW=70
Explanation:
Constraints :
2<=W
W%2=0
V<W
The candidate has to write the code to accept two positive numbers separated by a new line.
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”.
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:
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:
Q21. A furnishing company is manufacturing a new collection of curtains. The curtains are of two
colors aqua(a) and black (b). The curtains color is represented as a string(str) consisting of a’s and
b’s of length N. Then, they are packed (substring) into L number of curtains in each box. The box
with the maximum number of ‘aqua’ (a) color curtains is labeled. The task here is to find the number
of ‘aqua’ color curtains in the labeled box.
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.
The president and prime minister of India will always sit next to each other.
Example 1:
Input :
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:
So, total possible ways 10 members can be seated around a round table is