0% found this document useful (0 votes)
19 views15 pages

TCS Digital Coding Questions

Uploaded by

saptarshi khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views15 pages

TCS Digital Coding Questions

Uploaded by

saptarshi khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Question 1

Problem Statement-:

Identify the logic behind the series

6 28 66 120 190 276….

The numbers in the series should be used to create a Pyramid. The base of the Pyramid
will be the widest and will start converging towards the top where there will only be one
element. Each successive layer will have one number less than that on the layer below
it. The width of the Pyramid is specified by an input parameter N. In other words there
will be N numbers on the bottom layer of the pyramid.

The Pyramid construction rules are as follows

1. First number in the series should be at the top of the Pyramid


2. Last N number of the series should be on the bottom-most layer of the Pyramid,
with Nthnumber being the right-most number of this layer.
3. Numbers less than 5-digits must be padded with zeroes to maintain the sanctity
of a Pyramid when printed. Have a look at the examples below to get a pictorial
understanding of what this rule actually means.

Example

If input is 2, output will be

• 00006
• 00028 00066

If input is 3, output will be

• 00006
• 00028 00066
• 00120 00190 00276

Formal input and output specifications are stated below


Input Format:

• First line of input will contain number N that corresponds to the width of the
bottom-most layer of the Pyramid

Output Format:

• The Pyramid constructed out of numbers in the series as per stated construction
rules

Constraints:

• 0 < N <= 14

Question 2

Problem Statement-: 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:

• 1 <= P <= 1000000


• 1 <=T <= 50
• 1<= N1 <= 30
• 1<= N2 <= 30

Input Format:

• First line: P principal (Loan Amount)


• Second line: T Total Tenure (in years).
• Third Line: N1 is the number of slabs of interest rates for a given period by Bank
A. First slab starts from the first year and the second slab starts from the end of
the first slab and so on.
• Next N1 line will contain the period and their interest rate respectively.
• After N1 lines we will receive N2 viz. the number of slabs offered by the second
bank.
• Next N2 lines are the number of slabs of interest rates for a given period by Bank
B. The first slab starts from the first year and the second slab starts from the end
of the first slab and so on.
• The period and rate will be delimited by single white space.

Output Format: Your decision either Bank A or Bank B.

Explanation:

• Example 1
o Input
o 10000
o 20
o 3
o 5 9.5
o 10 9.6
o 5 8.5
o 3
o 10 6.9
o 5 8.5
o 5 7.9
• Output: Bank B

• Example 2
o Input
o 500000
o 26
o 3
o 13 9.5
o 3 6.9
o 10 5.6
o 3
o 14 8.5
o 6 7.4
o 6 9.6
• Output: Bank A

Question 3

Problem Statement-:One person hands over the list of digits to Mr. String, But Mr.
String understands only strings. Within strings also he understands only vowels. Mr.
String needs your help to find the total number of pairs which add up to a certain digit D.

The rules to calculate digit D are as follow

• Take all digits and convert them into their textual representation
• Next, sum up the number of vowels i.e. {a, e, i, o, u} from all textual representation
• This sum is digit D

Now, once digit D is known find out all unordered pairs of numbers in input whose sum
is equal to D. Refer example section for better understanding.

Constraints

• 1 <= N <= 100


• 1 <= value of each element in second line of input <= 100
Number 100, if and when it appears in input should be converted to textual
representation as hundred and not as one hundred. Hence number of vowels in number
100 should be 2 and not 4

Input

• First line contains an integer N which represents number of elements to be


processed as input
• Second line contains N numbers separated by space

Output

• Lower case representation of textual representation of number of pairs in input


that sum up to digit D

Note: – (If the count exceeds 100 print “greater 100”)

Time Limit

Examples

Example 1

Input

12345
Output

one

Explanation

1 -> one -> o, e

2 -> two -> o

3 -> three -> e, e

4 -> four -> o, u

5 -> five – > i, e

Thus, count of vowels in textual representation of numbers in input = {2 + 1 + 2 + 2 + 2}


= 9. Number 9 is the digit D referred to in section above.

Now from the given list of numbers {1,2,3,4,5} -> find all pairs that sum up to 9.

Upon processing this we know that only a single unordered pair {4, 5} sum up to 9.
Hence the answer is 1. However, output specification requires you to print textual
representation of number 1 which is one. Hence output is one.

Note: – Pairs {4, 5} or {5, 4} both sum up to 9. But since we are asking to count only
unordered pairs, the number of unordered pairs in this combination is only one.

Example 2

Input

3
742

Output

zero

Explanation

7 -> seven -> e, e

4 -> four -> o, u

2 -> two -> o

Thus, count of vowels in textual representation of numbers in input = {2 + 2 + 1} = 5.


Number 5 is the digit D referred to in section above.

Since no pairs add up to 5, the answer is 0. Textual representation of 0 is zero. Hence


output is zero.

Question 4

Problem Statement:- Jaya invented a Time Machine and wants to test it by time-
traveling to visit Russia on the Day of Programmer (the 256thday of the year) during a
year in the inclusive range from 1700 to 2700. From 1700 to 1917 , Russia’s official
calendar was the Julian Calendar since 1919 they used the Gregorian calendar system.
The transition from the Julian to Gregorian calendar system occurred in 1918 , when the
next day after 31 January was February 14 . This means that in 1918, February 14 was
the 32nd day of the year in Russia. In both calendar systems, February is the only month
with a variable amount of days; it has 29 days during a leap year, and 28 days during all
other years. In the Julian calendar, leap years are divisible by 4 ; in the Gregorian
calendar, leap years are either of the following:

• Divisible by 400
• Divisible by 4 and not divisible by 100

Given a year, y, find the date of the 256th day of that year according to the official
Russian calendar during that year. Then print it in the format dd.mm.yyyy, where dd is the
two-digit day, mm is the two-digit month, and yyyy is y.

For example, the given year is 1984.1984 is divisible by 4, so it is a leap year. The 256
day of a leap year after 1918 is September 12, so the answer is 12.9.1984.

Function Description

• Complete the programmerday function in the editor below. It should return a


string representing the date of the 256th day of the year given.
• programmerday has the following parameter(s):
o year: an integer

Input Format

• A single integer denoting year y.

Output Format

• Print the full date of programmerday during year y in the format dd.mm.yyyy,
where dd is the two-digit day, mm is the two-digit month, and yyyy is y.

Sample Input

2017

Sample Output

13.09.2017
Question 5

Problem Statement:- Hobo’s Drawing teacher asks his class to open their books to a
page number. Hobo can either start turning pages from the front of the book or from the
back of the book. He always turns pages one at a time. When she opens the book, page
1 is always on the right side: When he flips page 1, he sees pages 2 and 3. Each page
except the last page will always be printed on both sides. The last page may only be
printed on the front, given the length of the book. If the book is n pages long, and he
wants to turn to page p, what is the minimum number of pages he will turn? He can
start at the beginning or the end of the book. Given n and p, find and print the minimum
number of pages Hobo must turn in order to arrive at page p

Function Description

Complete the countpage function in the editor below. It should return the minimum
number of pages Hobo must turn.

countpage has the following parameter(s):

• n: the number of pages in the book


• p: the page number to turn to

Input Format

• The first line contains an integer n, the number of pages in the book.
• The second line contains an integer, p, the page that Hobo’s teacher wants her to
turn to.

Output Format

• Print an integer denoting the minimum number of pages Hobo must turn to get to
page p

Sample Input
6
2

Sample Output

Question 6

Problem Statement:- 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 7

Problem Statement:- You will be given an array of integers and a target value.
Determine the number of pairs of array elements that have a difference equal to a target
value.

For example, given an array of [1, 2, 3, 4] and a target value of 1, we have three values
meeting the condition: 2-1 = 1, 3-2 = 1, and 4-3 = 1.
Function Description

Write a function pairs. It must return an integer representing the number of element
pairs having the required difference.

pairs has the following parameter(s):

• k: an integer, the target difference


• arr: an array of integers

Input Format

• The first line contains two space-separated integers n and k, the size of arr and
the target value.
• The second line contains n space-separated integers of the array arr.

Sample Input

52

15342

Sample Output

Question 8

Problem Statement:- A jail has a number of prisoners and a number of treats to pass
out to them. Their jailer decides the fairest way to divide the treats is to seat the
prisoners around a circular table in sequentially numbered chairs. A chair number will
be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed
to each prisoner sequentially around the table until all have been distributed.
The jailer is playing a little joke, though. The last piece of candy looks like all the others,
but it tastes awful. Determine the chair number occupied by the prisoner who will
receive that candy.

For example, there are 4 prisoners and 6 pieces of candy. The prisoners arrange
themselves in seats numbered 1 to 4 . Let’s suppose two are drawn from the hat.
Prisoners receive candy at positions 2,3,4,1,2,3. The prisoner to be warned sits in chair
number 3

Function Description

Write a function saveThePrisoner. It should return an integer representing the chair


number of the prisoner to warn.

saveThePrisoner has the following parameter(s):

• n: an integer, the number of prisoners


• m: an integer, the number of sweets
• s: an integer, the chair number to begin passing out sweets from

Input Format

• The first line contains an integer t, denoting the number of test cases.
• The next t lines each contain 3 space-separated integers:
o – : n the number of prisoners
o – : m the number of sweets
o – : s the chair number to start passing out treats at

Output Format

• For each test case, print the chair number of the prisoner who receives the awful
treat on a new line.

Sample Input
2
521
522

Sample Output

2
3

You might also like