Beruflich Dokumente
Kultur Dokumente
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.
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
500
1 3 1470
12
6000
2 3 17824
5.9
3 500 1000
2
0
Program logic:
here, a while loop is used to calculate present amount of every month for withdrawing the given
amount
interest%=interest rate/100
since we know last required amount we only need to iterate the loop T-1 times and later subtract last
interest from the required amount.
** using your own logic is better. Program seems easy but your logical approach matters. Also
suggest your solution to this problem by commenting.
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
1. F denotes forward displacement in meters
2. B denotes backward displacement in meters
3. T denotes time taken to cover 1 meter
4. FD denotes distance from Robot’s starting position and the ditch in forward direction
5. 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
3
63 F
9 4 3 13 10
1 25 F
9 7 1 11 13
No Ditch
4 4 3 8 12
5
133 F
8 4 7 11 22
216 B
4 5 4 25 6
2 231 B
4 9 3 6 29
408 B
7 10 6 24 12
9F
10 10 1 9 7
Sample Solution in C :
#include<stdio.h>
int main()
int N,F,B,T,FD,BD,d;
scanf("%d",&N);
while(N>0)
scanf("%d %d %d %d %d",&F,&B,&T,&FD,&BD);
if(F==B && F<FD && B<BD)
printf("NO Ditch\n");
else
d=0;
d+=F;
FD-=F;
BD+=F;
if(FD>0)
d+=B;
FD+=B;
BD-=B;
if(FD<=0)
d+=FD;
printf("%d F\n",d*T);
else if(BD<=0)
{
d+=BD;
printf("%d B\n",d*T);
N--;
return 0;
Input Format:
First line contains number of test cases N
Next N lines, each contain a positive integer Li which corresponds
to the demand of milk.
Output Format:
For each input Li, print the minimum number of bottles required to
fulfill the demand
Constraints:
1<= N <= 1000
Li> 0
1<= i <= N
Explanation:
Number of test cases is 2
o 1 x 7 litres
o 1 x 5 litres
Solved Previous years TCS CodeVita questions – Milk Man and His Bottles
int main()
int b,n,l;
scanf("%d",&n);
while(n>0)
scanf("%d",&l);
b=0;
while(l>0)
{
b++;
if(l>=10)
l=l-10;
else if(l>=7)
l=l-7;
else if(l>=5)
l=l-5;
else
l=l-1;
printf("%d\n",b);
n--;
return 0;
2. Querying
Query the element at row K and column L, presented as input in form of Q K L
3. Updation
Update the element at row X and column Y with value Z, presented as input in form of U X Y Z
Note:
Angle of rotation will always be multiples of 90 degrees only.
All Update operations happen only on the initial matrix. After update all the previous rotations
have to be applied on the updated matrix
Output Format:
For each Query operation print the element present at K-L location of the matrix in its current state.
Constraints:
1<=N<=1000
1<=Aij<=1000
0<=S<=160000
1<=K, L<=N
1<=Q<=100000
Sample Input and Output
2
12
34
A 90
3
Q11
1
1 Q12
4
A 90
6
Q11
U116
Q22
-1
Explanation:
Initial Matrix
12
34
Again the angle of rotation is 90 degree, now after the rotation the matrix will become
43
21
Now the element at A11 is 4.
After updating, apply all the previous rotations (i.e. 180 = two 90 degree rotations).
The matrix will now become
43
26
Now A22 is 6.
int i,j,n,arr1[1000][1000],arr2[1000][1000];
void swaparray()
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr1[i][j]=arr2[i][j];
}
int main()
char opt[4];
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&arr1[i][j]);
scanf("%s",opt);
while(strcmp(opt,"-1"))
if(opt[0]=='A')
if(!strcmp(opt,"A90"))
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr2[j][n-i-1]=arr1[i][j];
swaparray();
else if(!strcmp(opt,"A180"))
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr2[n-i-1][n-j-1]=arr1[i][j];
swaparray();
else if(!strcmp(opt,"A270"))
for(i=0;i<n;i++)
for(j=0;j<n;j++)
arr2[n-j-1][i]=arr1[i][j];
swaparray();
else if(opt[0]=='Q')
printf("%d\n",arr1[opt[1]-48][opt[2]-48]);
else if(opt[0]='U')
arr1[opt[1]-48][opt[2]-48]=opt[3]-48;
scanf("%s",opt);
return 0;
}
Problem: A futuristic company is building an autonomous car. The scientists at the company are
training the car to perform Reverse parking. To park, the car needs to be able to move in backward as
well as forward direction. The car is programmed to move backwards B meters and forwards again,
say F meters, in a straight line. The car does this repeatedly until it is able to park or collides with
other objects. The car covers 1 meter in T units of time. There is a wall after distance D from car’s
initial position in the backward direction.
The car is currently not without defects and hence often hits the wall. The scientists are devising a
strategy to prevent this from happening. Your task is to help the scientists by providing them with
exact information on amount of time available before the car hits the wall.
#include<stdio.h>
int main()
int D,F,B,T,d,t;
scanf("%d",&D);
scanf("%d",&F);
scanf("%d",&B);
scanf("%d",&T);
if(F>B)
printf("Car wont hit the wall");
else if(D<F)
else
d=D;t=0;
while(d>0)
d-=B;
t+=(B);
if(d>0)
d+=F;
t+=F;
if(d==0)
else
d+=B;
t-=B;
while(d!=0)
{
d--;
t++;
return 0;
Raj is a newbie to the programming and while learning the programming language he came to know
the following rules:
– Each program must start with ‘{‘ and end with ‘}’.
– Each program must contain only one main function. Main function must start with ‘<‘ and end
with ‘>’.
– A program may or may not contain user defined function(s). There is no limitation on the
number of user defined functions in the program. User defined function must start with ‘(‘ and end with
‘)’.
– Loops are allowed only inside the functions (this function can be either main function or user
defined function(s)). Every loop must start with ‘{‘ and end with ‘}’.
– User defined function(s) are not allowed to be defined inside main function or other user
defined function(s).
– Number of instructions inside any user defined function must not be more than 100.
If any of the above conditions is not satisfied, then the program will generate compilation errors. Today
Raj has written a few programs, but he is not sure about the correctness of the programs. Your task is
to help him to find out whether his program will compile without any errors or not.
Input Format:
First line starts with T, number of test cases. Each test case will contain a single line L, where L is a
program written by Raj.
Output Format:
Print “No Compilation Errors” if there are no compilation errors, else print “Compilation Errors”.
Constraints:
1<=T<=100
L is a text and can be composed of any of the characters {, }, (, ), <, >and P, where P will
represents the instruction.
3
No Compilation Errors
{<>(P)}
1 Compilation Errors
{<{}>({}))
Compilation Errors
{({})}
Note:
Please do not use package and namespace in your code. For object oriented languages your code
should be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not
exist in gcc
In a social network, online communities refer to the group of people with an interest towards the
same topic. People connect with each other in a social network. A connection between
Person I and Person J is represented as C I J. When two persons belonging to different
communities connect, the net effect is merger of both communities which I and J belonged to.
We are only interested in finding out the communities with the member count being an even
number. Your task is to find out those communities.
Input Format:
Output Format:
For each query Q, output the number of communities with even member-count
Constraints:
1<=N<=10^6
1<=I, J<=N
SNo
Input Output
.
1
5 0
Q00 1
C12 0
Q00 1
C23
Q00
C45
Q00
-1
Explanation:
For first query there are no members in any of the groups hence answer is 0.
After C 1 2, there is a group (let's take it as G1) with 1 and 2 as members hence total count at this
moment is 1.
After C 2 3 total members in G1 will become {1, 2, 3} hence there are no groups with even count.
After C 4 5 there formed a new group G2 with {4, 5} as members, hence the total groups with even
count is 1.
Note:
Please do not use package and namespace in your code. For object oriented languages your code should
be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not
exist in gcc
You are given a square matrix of dimension N. Let this matrix be called A. Your task is to
rotate A in clockwise direction byS degrees, where S is angle of rotation. On the matrix, there will
be 3 types of operations viz.
1. Rotation
2. Querying
3. Updation
Update the element at row X and column Y with value Z, presented as input in
form of U X Y Z
Output Format:
For each Query operation print the element present at K-L location of the matrix in its current
state.
Constraints:
1<=N<=1000
1<=Aij<=1000
0<=S<=160000
1<=K, L<=N
1<=Q<=100000
SNo
Input Output
.
2
12
34
A 90
3
Q11
1 1
Q12
4
A 90
6
Q11
U116
Q22
-1
Explanation:
Initial Matrix
12
34
Again the angle of rotation is 90 degree, now after the rotation the matrix will become
43
21
Now the element at A is 4.
11
After updating, apply all the previous rotations (i.e. 180 = two 90 degree rotations).
The matrix will now become
43
26
Now A is 6.
22
Note:
Please do not use package and namespace in your code. For object oriented languages your code should
be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not
exist in gcc
Question / Problem
Solution
/**
*
* @author VIK VIKASH VIKKU
* Business Logic class
*/
import java.util.ArrayList;
import java.util.Scanner;
ls.add(num);
} else {
if(ls.size()>temp.length)
temp=ls.toArray(temp);
ls.clear();
ls.add(num);
}
} else {
ls.add(num);
}
}
if(ls.size()>temp.length)
{
temp=ls.toArray(temp);
ls.clear();
}
for (int i = 0; i < temp.length; i++) {
if(i<temp.length-1)
System.out.print(temp[i]+" ");
else
System.out.println(temp[i]);
}
}
Problem 1
Statement: Write a program to calculate the sum of n numbers and print the sum. (Sum Of
Number)
Input Format
Line 1 An integer n - the number of integers to be added.
Line 2 various integers.
Output Format
Line 1 Single integer which corresponds to sum, followed by a new line
Problem 2
Statement: Develop a program to sort the given sequence.(Sort The Sequence)
Program should accept input sequence to be sorted via a file. The file format of the input files is
described below. Your program should parse the input file. In case of a bad input, program should
output appropriate message as mentioned in output specification below.
Input Format:
Line 1 Absolute path of the first input file
Output Format:
Line 1 For Valid inputs
Values Before the sort:
For Invalid inputs
Invalid Input
Line 2 Corresponding element of input file where elements are seperated by space
Line 3 Values after the Ascending sort:
Line 4 Sorted sequence in ascending order where elements are seperated by space
Line 5 Values after the Descending sort:
Line 6 Sorted sequence in descending order where elements are seperated by space
Problem 3
Statement: Write a program to find Nth prime number in a given range, inclusive of the first and
the last element in that range. Program should accept input using commmand line arguments. In
case of a bad input, program should output appropriate message as mentioned in output
specification below. (Pop Out Primes)
Information: A prime number (or a prime) is a natural number greater than 1 that has no
positive divisors other than 1 and itself.
Input Format
Output Format
3 1 No prime number
15 is present at this
9 index
4 59 Invalid Input
96
0
5 1 Invalid Input
a
2
Problem 4
Statement: Develop a program to add two matrices. Program should accept input matrices to be
added via files. The file format of the input files is described below. Your program should parse the
input files and construct the input matrices. In case of a bad input, program should output
appropriate message as mentioned in output specification below. Once the matrices are formed,
apply appropriate logic to see if addition is possible. If addition is possible, add the input matrices
and output the result in format specified below. (Matrix Addition)
Information: Matrix addition is the operation of adding two matrices that in turn produces
resultant third matrix which is a sum of the two input matrices.
File Format
Each row is delimited by new line character
Columns within each row are space delimited
e.g. A 3x3 matrix will be represented in the file as :
123
456
789
Input Format
Line 1 Absolute path of the first input file
Line 2 Absolute path of the second input file
Output
1. Input matrices which are inserted
2. Resultant matrix or appropriate error message in format as mentioned below
Output Format
Line 1 For Valid inputs
Input Matrix 1:
For Invalid inputs
INVALID INPUT
Line 2 to Line N+1 Corresponding rows of input matrix 1
Problem 5
Statement: Develop a program to find simple interest and compund interest for a given principal
amount, rate and tenure. Program should accept the user input using commmand line arguments.
In case of a bad input, program should output appropriate message as mentioned in output
specification below. (Interest Calculator)
Information: Simple interest is interest paid only on the original principal, not on the interest
accrued Simple interest is determined by multiplying the interest rate by the principal by the
number of periods. When the interest rate is applied to the original principal and any accumulated
interest, this is called compound interest.
Input Format
Line 1 Principal amount
Line 2 Rate of interest per annum Line 3 Tenure (in years)
Output Format
Line 1 For Valid input
Simple Interest:
where value is simple interest rounded upto 2 digits
For Invalid input
Invalid Input
Line 2 Compound Interest:
where value is compound interest rounded upto 2 digits,
followed by a new line
Reverse Gear
Credit and Risk Calculator
Accico Equi Pairs
Drunkard's Walk
Minimum Distance
Recurring Deposit
Earthquake Damage Estimator
CodeVita
CodeVita
Valid Segments
Close Strings
Employee DB Design
Employee Hierarchy
Close Friends
Cyclic Palindrome
CodeVita
Query Engine
Loan Structuring
Expense Solver
Pagination
Credits
Count The Factor
Isotope Fusion
Brokerage
Profit or Loss statement for Transactions
Helloworld
Hexogonal Triangle