TCS Coding Questions With Answers
TCS Coding Questions With Answers
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 : The output will be a positive or negative integer based on number of ‘*’
and ‘#’ in the input string.
Example 1:
Input 1:
Output :
s=input()
a=0
b=0
for i in s:
if i=='*':
a+=1
elif i=='#':
b+=1
print(a-b)
Link to this Question
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}
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.
Example 1:
Input
Output :
Example 2:
Output :
Constraints
1<=N<=20
1<=Arr[i]<=10000
import sys
n=int(input())
c=0
m=-sys.maxsize-1
while n:
n-=1
a=int(input())
if a>m:
m=a
c+=1
print(c)
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 :
Example 1:
Input :
Output :
Example 2:
input :
Output :
C++
Java
Python
r=int(input())
c=int(input())
sum=0
m=0
id=0
for i in range(r):
for j in range(c):
sum+=int(input())
if sum>m:
m=sum
id=i+1
sum=0
print(id)
A party has been organised on cruise. The party is organised 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 :
Explanation:
1st hour:
Entry : 7 Exit: 1
2nd hour :
Entry : 0 Exit : 2
Entry: 5 Exit: 1
Hour 4:
Entry : 1 Exit : 3
Hour 5:
Entry : 3 Exit: 4
Example 2:
Input:
4 -> Value of T
[3,5,2,0] -> E[], Element of E[0] to E[N-1], where input each element is
separated by new line.
[0,2,4,4] -> L[], Element of L[0] to L[N-1], while input each element in
separated by new line
Output:
Cruise at an instance
Explanation:
Hour 1:
Entry: 3 Exit: 0
Hour 2:
Entry : 5 Exit : 2
Hour 3:
Entry : 2 Exit: 4
Hour 4:
Entry: 0 Exit : 4
Constraints:
1<=T<=25
0<= E[i] <=500
0<= L[i] <=500
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:
r: 1 balloon
g: 2 balloons
b: 2 balloons
y : 2 balloons
Example 2:
Input:
10 -> Value of N
[a,b,b,b,c,c,c,a,f,c] -> B[], elements B[0] to B[N-1] where input each
element is separated by new line.
Output :
b-> ‘b’ colour balloon is present odd number of times in the bunch.
Explanation:
a: 2 balloons
b: 3 balloons
c: 4 balloons
f: 1 balloons
Here, both ‘b’ and ‘f’ have odd number of balloons. But ‘b’ colour balloon
occurs first.
The output should be a single literal (Check the output in example 1 and
example 2)
Constraints:
3<=N<=50
B[i]={{a-z} or {A-Z}}
Question 1
There is a JAR full of candies for sale at a mall counter. JAR has the capacity
N, that is JAR can contain maximum N candies when JAR is full. At any point
of time. JAR can have M number of Candies where M<=N. Candies are
served to the customers. JAR is never remain empty as when last k candies
are left. JAR if refilled with new candies in such a way that JAR get full.
Write a code to implement above scenario. Display JAR at counter with
available number of candies. Input should be the number of candies one
customer can order at point of time. Update the JAR after each purchase and
display JAR at Counter.
Output should give number of Candies sold and updated number of Candies
in JAR.
Given,
K =< 5, where k is number of minimum candies that must be inside JAR ever.
Input Value
o 3
Output Value
o NUMBER OF CANDIES SOLD : 3
o NUMBER OF CANDIES AVAILABLE : 7
Example : (N=10, k<=5)
Input Value
o 0
Output Value
o INVALID INPUT
o NUMBER OF CANDIES LEFT : 10
total_candies = 10
no_of_candies = int(input())
if no_of_candies in range(1, 6):
print('No. of Candies Sold:',no_of_candies)
print('No. of Candies Left:',total_candies-no_of_candies)
else:
print("Invalid Input")
print('No. of Candies Left:',total_candies)
Question 2
Display the most fit trainee (or trainees) and the highest average oxygen
level.
Note:
The oxygen value entered should not be accepted if it is not in the
range between 1 and 100.
If the calculated maximum average oxygen value of trainees is below 70
then declare the trainees as unfit with meaningful message as “All
trainees are unfit.
Average Oxygen Values should be rounded.
Example 1:
INPUT VALUES
95
92
95
92
90
92
90
92
90
OUTPUT VALUES
o Trainee Number : 1
o Trainee Number : 3
Note:
Input should be 9 integer values representing oxygen levels entered in order
as
Round 1
Round 2
Round 3
Problem Statement
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
Solution
n = int(input())
if n==0:
print("Time Estimated : 0 Minutes")
elif n in range(1,2001):
print("Time Estimated : 25 Minutes")
elif n in range(2001,4001):
print("Time Estimated : 35 Minutes")
elif n in range(4001,7001):
print("Time Estimated : 45 Minutes")
else:
print("INVALID INPUT")
Question 4
Problem Statement
Example 1:
Enter your PlainText: All the best
Enter the Key: 1
Python
C++
def ceaser(text,key):
result = ""
# transverse the plain text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters in plain text
if (char.isupper()):
result += chr((ord(char) + key-65) % 26 + 65)
# Encrypt lowercase characters in plain text
elif (char.islower()):
result += chr((ord(char) + key - 97) % 26 + 97)
elif(char.isdigit()):
result += str(int(char) + key)
elif(char == '-'):
result += '-'
elif (char.isspace()):
result += " "
return result
#check the above function
text = input("Enter your plain text:")
key = int(input("Enter the key:"))
print(ceaser(text,key))
Question 5
Problem Statement
Take input as
1. Number of Interior walls
2. Number of Exterior walls
3. Surface Area of each Interior 4. Wall in units of square feet
Surface Area of each Exterior Wall in units of square feet
If a user enters zero as the number of walls then skip Surface area
values as User may don’t want to paint that wall.
6
3
12.3
15.2
12.3
15.2
12.3
15.2
10.10
10.10
10.00
Total estimated Cost : 1847.4 INR
Question 6
Problem Statement
A City Bus is a Ring Route Bus which runs in circular fashion.That is, Bus
once starts at the Source Bus Stop, halts at each Bus Stop in its Route and at
the end it reaches the Source Bus Stop again.
If there are n number of Stops and if the bus starts at Bus Stop 1, then after
nth Bus Stop, the next stop in the Route will be Bus Stop number 1 always.
If there are n stops, there will be n paths.One path connects two stops.
Distances (in meters) for all paths in Ring Route is given in array Path[] as
given below:
Path = [800, 600, 750, 900, 1400, 1200, 1100, 1500]
Fare is determined based on the distance covered from source to destination
stop as Distance between Input Source and Destination Stops can be
measured by looking at values in array Path[] and fare can be calculated as
per following criteria:
Path is circular in function. Value at each index indicates distance till current
stop from the previous one. And each index position can be mapped with
values at same index in BusStops [] array, which is a string array holding
abbreviation of names for all stops as-
“THANERAILWAYSTN” = ”TH”, “GAONDEVI” = “GA”, “ICEFACTROY” =
“IC”, “HARINIWASCIRCLE” = “HA”, “TEENHATHNAKA” = “TE”,
“LUISWADI” = “LU”, “NITINCOMPANYJUNCTION” = “NI”,
“CADBURRYJUNCTION” = “CA”
Example 1:
Input Values
ca
Ca
Output Values
INVALID OUTPUT
Example 2:
Input Values
NI
HA
Output Values
23.0 INR
C++
Python
import math
def getFare(source,destination):
route=[ [ "TH", "GA", "IC", "HA", "TE", "LU", "NI", "CA"],
[800,600,750,900,1400,1200,1100,1500]
]
fare = 0.0
if not (source in route[0] and destination in route[0]):
print("Invalid Input")
exit()
if route[0].index(source) < route[0].index(destination):
for i in
range(route[0].index(source),route[0].index(destination)+1):
fare+=route[1][i]
elif route[0].index(destination) < route[0].index(source):
for i in range(route[0].index(source)+1,len(route[0])):
fare+=route[1][i]
for i in range(0,route[0].index(destination)+1):
fare+=route[1][i]
return float(math.ceil(fare*0.005))
source = input()
destination = input()
fare = getFare(source,destination)
if fare == 0:
print("Invalid Input")
else:
print(fare)
Question 7
Problem Statement
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
Question 8
Problem Statement
Problem Statement
Coffee
1. Espresso Coffee
2. Cappuccino Coffee
3. Latte Coffee
Tea
1. Plain Tea
2. Assam Tea
3. Ginger Tea
4. Cardamom Tea
5. Masala Tea
6. Lemon Tea
7. Green Tea
8. Organic Darjeeling Tea
Soups
Beverages
Welcome to CCD
Enjoy your
Example 1:
Input:
o c
o 1
Output
o Welcome to CCD!
o Enjoy your Espresso Coffee!
Example 2:
Input
o t
o 9
Output
o INVALID OUTPUT!
menu = [['Espresso Coffee','Cappuucino Coffee','Latte Coffee'], ['Plain Tea',
'Organic Darjeeling Tea'], ['Hot and Sour Soup','Veg Corn Soup','Tomato Soup',
'Badam-Pista Drink']]
m = input()
if m=='c':
submenu = int(input())
if submenu in range(3):
else:
print("INVALID INPUT")
if m=='t':
submenu = int(input())
if submenu in range(8):
else:
print("INVALID INPUT")
if m=='s':
submenu = int(input())
if submenu in range(4):
else:
print("INVALID INPUT")
if m=='b':
submenu = int(input())
if submenu in range(3):
else:
print("INVALID INPUT")
else:
print("INVALID INPUT!")
Question 10
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:
Example 1:
Input
20
30
40
50
2
3
14
Output
Total Income 2000 INR
Note: Input and Output Format should be same as given in the above
example.
For any wrong input display INVALID INPUT
Output Format
for i in range(20):
m = input()
if m == "":
break
age.append(int(m))
else:
print("INVALID INPUT")
exit()
fees = 0
for i in age:
if i < 17:
fees+=200
elif i <40:
fees+=400
else:
fees+=300
Explanation:
Step 2:
Step 3:
num = int(input("Enter the year you want to check if is leap year or not: "))
if(num%4 == 0):
if(num%100 == 0):
#check if the input year is divisible by 100 and if true move to next loop
if(num%400 == 0):
#the input year is divisible by 4, 100 and 400, hence leap year.
else:
else:
print("The year {} is a leap year".format(num))
else:
#if the input num is not divisible by 4 then it can not be a leap year altogether.
It is positive then call the function prime and check whether the
take positive number is prime or not.
def prime(n):
if n > 1:
if (n % i) == 0:
break
else:
prime(num)
else:
print("please enter a positive number")
0,0,7,6,14,12,21,18, 28
Explanation : In this series the odd term is increment of 7 {0, 7, 14, 21, 28, 35
––––––}
a=0
b=0
for i in range(1,num+1):
if(i%2!=0):
a= a+7
else:
b = b+6
if(num%2!=0):
else:
Consider the following series: 1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243, 64, 729,
128, 2187 …
This series is a mixture of 2 series – all the odd terms in this series form a
geometric series and all the even terms form yet another geometric
series. Write a program to find the Nth term in the series.
The value N in a positive integer that should be read from STDIN. The Nth
term that is calculated by the program should be written to STDOUT. Other
than value of n th term,no other character / string or message should be
written to STDOUT. For example , if N=16, the 16th term in the series is 2187,
so only value 2187 should be printed to STDOUT.
a= 1
b= 1
if(i%2!=0):
a = a*2
else:
b = b*3
if(n%2!=0):
else:
print('\n{} term of series is {}\t'.format(n,a/2))
This series is a mixture of 2 series all the odd terms in this series form even
numbers in ascending order and every even terms is derived from the
previous term using the formula (x/2)
The value n in a positive integer that should be read from STDIN the nth term
that is calculated by the program should be written to STDOUT. Other than
the value of the nth term no other characters /strings or message should be
written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th
term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value
4 should be printed to STDOUT.
a=0
b=0
for i in range(1,n+1):
if(i%2!=0):
a= a+2
else:
b= b+1
if(n%2!=0):
print('{}'.format(a-2))
else:
print('{}'.format(b-1))
1. These three words will be read one at a time, in three separate line
2. The first word should be changed like all vowels should be replaced by
%
3. The second word should be changed like all consonants should be
replaced by #
4. The third word should be changed like all char should be converted to
upper case
5. Then concatenate the three words and print them
For example if you print how are you then output should be h%wa#eYOU.
You can assume that input of each word will not exceed more than 5 chars
Number 1 – 20
Number 2 – 20.38
Sum = 40.38
There were a total of 4 test cases. Once you compile 3 of them will be shown
to you and 1 will be a hidden one. You have to display error message if
numbers are not numeric.
This series is a mixture of 2 series all the odd terms in this series form even
numbers in ascending order and every even terms is derived from the
previous term using the formula (x/2)
The value n in a positive integer that should be read from STDIN the nth term
that is calculated by the program should be written to STDOUT. Other than
the value of the nth term no other characters /strings or message should be
written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th
term in the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value
4 should be printed to STDOUT.