TCS Codevita Questions |

# Question 1

**Problem : Catch-22** A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot’s path there is a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot. Your task is to calculate amount of time taken, before the Robot falls in either ditch, if at all it falls in a ditch. **Input Format:** First line contains total number of test cases, denoted by N Next N lines, contain a tuple containing 5 values delimited by space **F B T FD BD, where**

**F**denotes forward displacement in meters**B**denotes backward displacement in meters**T**denotes time taken to cover 1 meter**FD**denotes distance from Robot’s starting position and the ditch in forward direction**BD**denotes distance from Robot’s starting position and the ditch in backward direction

**Output Format:** For each test case print time taken by the Robot to fall in the ditch and also state which ditch he falls into. Print F for forward and B for backward. Both the outputs must be delimited by whitespace **OR** Print No Ditch if the Robot does not fall in either ditch

**Constraints:** **First move will always be in forward direction** **1 <= N <= 100** **forward displacement > 0** **backward displacement > 0** **time > 0 ** **distance of ditch in forward direction (FD) > 0** **distance of ditch in backward direction (BD) > 0** **All input values must be positive integers only** **Sample Input and Output**

SNo. | Input | Output |

1 | 3 9 4 3 13 10 9 7 1 11 13 4 4 3 8 12 | 63 F 25 F No Ditch |

2 | 5 8 4 7 11 22 4 5 4 25 6 4 9 3 6 29 7 10 6 24 12 10 10 1 9 7 | 133 F 216 B 231 B 408 B 9 F |

# Question 2

**Problem : Saving for a rainy day** By nature, an average Indian believes in saving money. Some reports suggest that an average Indian manages to save approximately 30+% of his salary. Dhaniram is one such hard working fellow. With a view of future expenses, Dhaniram resolves to save a certain amount in order to meet his cash flow demands in the future. Consider the following example. Dhaniram wants to buy a TV. He needs to pay Rs 2000/- per month for 12 installments to own the TV. If let’s say he gets 4% interest per annum on his savings bank account, then Dhaniram will need to deposit a certain amount in the bank today, such that he is able to withdraw Rs 2000/- per month for the next 12 months without requiring any additional deposits throughout. Your task is to find out how much Dhaniram should deposit today so that he gets assured cash flows for a fixed period in the future, given the rate of interest at which his money will grow during this period.

**Input Format:** First line contains desired cash flow **M**Second line contains period in months denoted by **T**Third line contains rate per annum **R** expressed in percentage at which deposited amount will grow

**Output Format:** Print total amount of money to be deposited now rounded off to the nearest integer **Constraints:** **M > 0** **T > 0** **R >= 0** **Calculation should be done upto 11-digit precision**

**Sample Input and Output**

SNo. | Input | Output |

1 | 500 3 12 | 1470 |

2 | 6000 3 5.9 | 17824 |

3 | 500 2 0 | 1000 |

# Question 3

**Problem : Sheldon Cooper and his beverage paradigm** Sheldon Cooper, Leonard Hofstadter and Penny decide to go for drinks at Cheese cake factory. Sheldon proposes to make a game out of this. Sheldon proposes as follows,

- To decide the amount of beverage they plan to consume, say X.
- Then order for a random number of different drinks, say {A, B, C, D, E, F} of quantities {a, b, c, d, e, f} respectively.
- If quantity of any three drinks add up to X then we’ll have it else we’ll return the order. E.g. If a + d + f = X then True else False

You are given

- Number of bottles N corresponding to different beverages and hence their sizes
- Next N lines, contain a positive integer corresponding to the size of the beverage
- Last line consists of an integer value, denoted by X above

Your task is to help find out if there can be any combination of three beverage sizes that can sum up to the quantity they intend to consume. If such a combination is possible print True else False

**Input Format:**

- First line contains number of bottles ordered denoted by N
- Next N lines, contains a positive integer A
_{i}, the size of the i^{th}bottle - Last line contains the quantity they intend to consume denoted by X in text above

**Output Format:**True, if combination is possible False, if combination is not possible

**Constraints:** **N >= 3** **A _{i} > 0**

**1 <= i <= N**

**X > 0**

**Sample Input and Output**

SNo. | Input | Output |

1 | 6 1 4 45 6 10 8 22 | True |

2 | 4 1 3 12 4 14 | False |

**Explanation for sample input and output 1:** The sum of 2nd, 5th and 6th beverage size is equal to 22. So the output will be True.

**Explanation for sample input and output 2:** Since no combination of given beverage sizes sum up to X i.e. 14, the output will be False.

**You may also like: Code vita answers 2015**

# Question 4

**Problem : Water Management** There are N tubs of water, numbered from 1 to N. Initially there is a few litres of water in each tub. Each water tub has 2 taps attached to it. Incoming Tap with speed x litres / second and Outgoing Tap with speed y litres / second. Let water(i) denote the final volume of water in i^{th} tub. Amit wants to attain such a situation that water(i) < water(i+1) for 1<=i <=N. i.e. water in each tub must be less than water in the tub next to it. He wants to do this as quickly as possible. You task is to find out and tell Amit, what is the minimum number of seconds required to attain in this situation.

**Input Format:** First line will contains the number of tubs, denoted by N Next N lines contain a tuple with 3 integers delimited by white space. The tuple contents are

- W
_{i}– volume of water present initially in i^{th}tub (in litres) - x – speed of incoming tap of i
^{th}tub ( in litres/second) - y – speed of outgoing tap of i
^{th}tub ( in litres/second)

**Output Format:** Minimum time in seconds, needed to arrange water in N tubs, in ascending order of volume.

**Constraints:** **2 <= N <= 100000** **0 <= W <= 1000000000 (1 billion) for each tub** **1 <= x <= 10000 for each tub** **1 <= y <= 10000 for each tub** **A tap can be used only for integral number of seconds i.e. you cannot use a tap for 0.3 or 0.5 seconds. It can be used either for 1,2,3…. Seconds** **Capacity of each tub is infinite.** **Volume of water in any tub cannot be less than zero at any point of time.** **Any number of taps can be turned on or off simultaneously.** **Sample Input and Output**

**Explanation for sample input and output 1:**Here we have 3 tubs with following information about each of them

Tub Number | Initial Volume | Incoming Tap speed | Outgoing Tap speed |

Tub 1 | 2 | 3 | 3 |

Tub 2 | 3 | 4 | 5 |

Tub 3 | 3 | 5 | 5 |

**Explanation for sample input and output 2:**Here we have 3 tubs with following information about each of them

Tub Number | Initial Volume | Incoming Tap speed | Outgoing Tap speed |

Tub 1 | 6 | 2 | 1 |

Tub 2 | 4 | 1 | 3 |

Tub 3 | 4 | 1 | 4 |

# Question 5

**Problem : Optimum Cost Calculator** A town A is located on a river. We have to send cargo to town B which is located ‘a’ kilometers downstream and ‘d’ kilometers from the river. Government wants to construct a sea link between B and the river such that the cost of transportation of goods from A to B is the cheapest. The transport cost of a unit of cargo per kilometer by waterway is half the cost incurred by taking the highway. Your task is to help the government find a point in the river from which to construct a highway to town B so that the government’s objective of reducing transportation cost is achieved. More specifically, calculate the distance from town A where the highway has to be constructed and the length of the highway to be constructed.

Sea link Planning Input Format |

**Fig:Sea link Planning**

**Input Format:**

**Output Format:**Print the distance of the point in the river denoted by D from Town A. Print the length of the highway that needs to be built from D to B.

**OR**Print “Invalid Input”, if any constraint is violated

**Constraints:**

**0 < a <= (57 * d)**

**0 < d <= (1.7 * a)**

**Calculations and printing of output should be done upto 11-digit precision**

**Sample Input and Output**

SNo. | Input | Output |

1 | 50 10 | X= 44.22649730810 Y= 11.54700538379 |

2 | 40 10 | X= 34.22649730810 Y= 11.54700538379 |

3 | 172 3 | Invalid Input |

# Question 6

**Problem : Hotel trip**Tom, Dick and Harry go to a restaurant. Each of them has a certain amount of money. When their monies are pooled they discover they have S Rs. Next, they start going through the menu card to choose items they would like to order. Your task is to help them find out how many different menu item combinations they can order. Since there can be many menu items with the same price, we are interested only in unique price combinations. Refer output specifications and examples to get a better understanding.

**Input Format:**First line contains N positive integers delimited by space, corresponding to prices of N menu items Second line contains an integer S corresponding to the amount of money pooled between them

**Output Format:**

- Print all possible combinations of item prices whose sum is equal to S. Enclose each such combination in square brackets [ ]. Elements should be separated by, followed by a space character.
- Item price combinations must be unique (See example 2 to understand this better)
- If more than one combinations exists, then the print order should be as follows
- Combinations should be printed in descending order item prices
- Two combinations can be differentiated by first comparing their costliest, followed by second costliest, and followed by the next costliest item, so on and so forth. Refer example outputs for better understanding of print order.

**Constraints:**

**P**

_{i}> 0 ; where i = 1 to N and Pi denotes the price of the i^{th}item**S > 0**

**Sample**

**Input and Output**

SNo. | Input | Output |

1 | 10 7 6 5 3 2 1 23 | [10, 7, 6] [10, 7, 5, 1] [10, 7, 3, 2, 1] [10, 6, 5, 2] [7, 6, 5, 3, 2] |

2 | 50 100 1 1 1 1 1 51 | [50, 1] |

3 | 50 100 4 3 1 1 1 1 54 | [50, 4] [50, 3, 1] [50, 1, 1, 1, 1] |

**Explanation:**In example 1 there are 7 items on the menu each having a fixed price. Tom, Dick and Harry have 23 Rs/- amongst them. So they can order the following combinations

- [10, 7, 6]
- [10, 7, 5, 1]
- [10, 7, 3, 2, 1]
- [10, 6, 5, 2]
- [7, 6, 5, 3, 2]

- [50, 4]
- [50, 3, 1]
- [50, 1, 1, 1, 1]

# Question 7

**Problem : Portfolio**Imagine a typical stock market scenario

- Trading happens 5 days a week. Exchanges are closed on weekends
- There are a fixed set of securities on which trading happens
- If one buys at low price and sells at a high price, one makes a profit. Vice-versa results in a loss

**For the purpose of this problem, assume that**

- 1
^{st}of the month is a Monday and a month comprises of 30 days. Hence there are 22 working days - There are no holidays on any weekday
- There are only 5 securities listed on the exchange
- Price of a given security is constant for a given day

**Given above assumptions, the following conventions are followed**

- Input data comprises of price of the securities for all 22 working days of the month and your trade records
- Output is to find out position in terms of securities held and / or Profit/ Loss at the end of the month

**Input Format:**

- First 5 lines of the input provides a security name and its prices for the whole month
- Columns in these lines are space delimited. First column provides security name and next 22 columns has price for 22 working days of the month
- Sixth line onwards is your trade data in ascending order of trade dates
- Trade data comprises of 4 values viz. <date, Buy(B)/ Sell(S), Security name, quantity>
- Trade data is terminated by a line comprising -1
- Refer example section on how to read input

**Output Format:**

- Print security name and holdings for all securities where holding is non-zero, delimited by space. Print order for securities where holdings are non-zero is governed by the rule –
- Securities and their holdings should be printed in the order in which their prices are provided in input.
- See Explanation section below for better understanding.

- Print Profit or Loss incurred due to trades in the format “Profit = <Value>” or “Loss = <Value>”

**Constraints:**

**Date will be always be between 1 and 30 (both inclusive)**

**Sample Input and Output**

SNo. | Input | Output |

1 | tcs 220 250 260 230 200 210 250 260 230 200 260 230 200 220 250 270 260 230 200 260 230 200 inf 220 250 260 230 200 260 230 200 220 270 250 260 230 200 220 250 270 260 230 260 230 200 cts 260 230 200 220 250 270 260 230 200 220 270 250 260 230 260 230 220 250 260 230 200 260 xyz 220 250 260 230 200 260 230 200 220 250 270 260 230 200 220 250 250 260 230 260 230 220 pqr 220 250 270 260 230 200 220 270 250 260 230 260 230 220 250 200 220 250 250 260 230 260 1 B tcs 100 3 B tcs 150 5 B xyz 40 8 S tcs 50 9 B pqr 70 -1 | tcs 200 xyz 40 pqr 70 Loss = 1700 |

2 | tcs 220 250 260 230 200 510 250 260 230 200 260 230 200 220 250 270 260 230 200 260 230 200 inf 220 250 260 230 200 260 230 200 220 270 250 260 230 200 220 250 270 260 230 260 230 200 cts 260 230 200 220 250 270 260 230 200 220 270 250 260 230 260 230 220 250 260 230 200 260 xyz 220 250 260 230 200 260 230 200 220 250 270 260 230 200 220 250 250 260 230 260 230 220 pqr 220 250 270 260 230 200 220 270 250 260 230 260 230 220 250 200 220 250 250 260 230 260 1 B tcs 100 5 B xyz 40 8 S tcs 50 9 B pqr 70 -1 | tcs 50 xyz 40 pqr 70 Profit = 14500 |

3 | tcs 300 290 350 300 300 390 250 250 230 200 260 230 200 320 250 270 260 230 200 260 230 200 wip 350 250 260 330 370 270 280 300 230 390 350 360 230 350 220 250 270 260 230 260 230 200 Per 370 330 280 370 250 370 360 330 280 220 270 350 260 260 260 330 290 250 260 330 230 260 syn 240 290 380 240 230 260 300 250 250 250 270 260 230 200 220 250 250 260 230 260 230 220 sie 250 350 270 260 240 260 220 270 250 260 230 260 230 240 250 280 240 250 250 260 230 260 3 B tcs 100 4 S tcs 70 5 S tcs 30 -1 | Loss = 5000 |

**Explanation for sample input and output 1:**

- There are 5 securities viz. {tcs, inf, cts, xyz and pqr}
- Price for working days follow security names. First 5 prices are for dates 1 – 5. Next 5 prices are for dates 8 – 12. Next 5 prices are for dates 15 – 19. Similarly until month-end
- Trade data indicate the following
- On 1
^{st}, there was a Buy transaction for security named tcs and quantity was 100 - On 3
^{rd}, there was another Buy transaction for same security and quantity 150 - On 5
^{th}, there was another Buy transaction for security named xyz and quantity 40 - On 8
^{th}, there was a Sell transaction for security named tcs and quantity was 50 - On 9
^{th}, there was another Buy transaction for security named pqr and quantity 70 - End of trade data is marked by -1

- On 1
- Upon looking up, prices for appropriate days and doing the maths, we get output as depicted above
- Securities prices are provided in input in order
*tcs, inf, cts, xyz*and*pqr*. Since holding is non-zero only for*tcs, xyz*and*pqr*, print order is*tcs*first, followed by*xyz*and lastly*pqr*. - Since a Loss has been incurred print it in format “Loss = <Value>” i.e. Loss = 1700

**Explanation for sample input and output 2:**

- Refer explanation 1 for instructions on how to read the input
- Upon doing the maths, we get output as depicted above
- Securities prices are provided in input in order
*tcs, inf, cts, xyz*and*pqr*. Since holding is non-zero only for*tcs, xyz*and*pqr*, print order is*tcs*first, followed by*xyz*and lastly*pqr*. - Since a profit is made, print it in format “Profit = <Value>” i.e. Profit = 14500

**Explanation for sample input and output 3:**

- Refer explanation 1 and 2 for instructions on how to read the input and compute required output
- Only notable change is, since holdings are zero for all securities, only print Loss = 5000

# Question 8

**Problem : Milk Man and His Bottles**A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply desired quantity using as less bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand of milk.

**Input Format:**First line contains number of test cases N Next N lines, each contain a positive integer L

_{i}which corresponds to the demand of milk.

**Output Format:**For each input L

_{i}, print the minimum number of bottles required to fulfill the demand

**Constraints:**

**1 <= N <= 1000**

**L**

_{i}> 0**1 <= i <= N**

**Sample Input and Output**

SNo. | Input | Output |

1 | 2 17 65 | 2 7 |

**Explanation:**Number of test cases is 2

- In first test case, demand of milk is 17 litres which can be supplied using minimum of 2 bottles as follows
- 1 x 10 litres and
- 1 x 7 litres

- In second test case, demand of milk is 65 litres which can be supplied using minimum of 7 bottles as follows
- 6 x 10 litres and
- 1 x 5 litres

**Hope You will like this post share and enjoy with your friends. If you have any answers of the following questions comment it below. Thank you for reading.**

**You may also like: Code vita answers 2015**

This codevita questions are important for programmers #codevita #codevitaanswers

can anyone tell me where i can find solved questions for tcs codevita

tqs bro