Académique Documents
Professionnel Documents
Culture Documents
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 lets 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
Input
Output
500
1
1470
12
6000
2
17824
5.9
3
500
1000
2
0
Important TCS CodeVita Questions Solved Saving for a rainy day
Sample Solution C Program:
#include<stdio.h>
#include<math.h>
int main()
{
int M,T;
float R,A,I,RM;
scanf(%d,&M);
scanf(%d,&T);
scanf(%f,&R);
RM=M;
T;
while(T>0)
{
A=RM/(1+R/(float)1200);
I=RM-A;
RM+=M-I;
T;
}
A=RM/(1+R/(float)1200);
I=RM-A;
RM-=I;
double r=ceil(RM-0.5);
if(r>RM)
r=ceil(RM);
else
r=floor(RM);
printf(%.lf,r);
return 0;
}
Program logic:
here, a while loop is used to calculate present amount of every month for withdrawing the given
amount
required amount= present amount+ present amount * interset% / deposit period
deposit period=1/12
interest%=interest rate/100
=> present amount=required amount/(1+interest rate/1200)
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 Robots 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.
2.
3.
4.
FD denotes distance from Robots starting position and the ditch in forward direction
5.
BD denotes distance from Robots 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
Input
3
9 4 3 13 10
9 7 1 11 13
4 4 3 8 12
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
Output
63 F
25 F
No Ditch
133 F
216 B
231 B
408 B
9F
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
Sample Input and Output
SNo.
Input
Output
2
1
17
65
Explanation:
Number of test cases is 2
1.
1 x 10 litres and
1 x 7 litres
6 x 10 litres and
1 x 5 litres
Solved Previous years TCS CodeVita questions Milk Man and His Bottles
Sample Solution in C Programming:
#include<stdio.h>
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;
}
Rotation
Rotate the matrix A by angle S, presented as input in form of A S
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
Input Format:
Input will consist of three parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N)
3. Various operations on the matrix, one operation on each line. (Beginning either with A, Q or U)
-1 will represent end of input.
Note:
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
SNo.
Input
Output
2
12
34
A 90
Q11
1
Q12
A 90
Q11
U116
Q22
-1
Explanation:
3
1
4
6
Initial Matrix
12
34
After 90 degree rotation, the matrix will become
31
42
Now the element at A11 is 3 and A12 is 1.
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.
As the next operation is Update, update initial matrix i.e.
62
34
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 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(!strcmp(opt,"A0") || !strcmp(opt,"A360")); //not required
}
else if(opt[0]=='Q')
{
if(opt[1]-48<n && opt[2]-48<n)
printf("%d\n",arr1[opt[1]-48][opt[2]-48]);
}
else if(opt[0]='U')
{
if(opt[1]-48<n && opt[2]-48<n)
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 cars
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.
d--;
t++;
}
printf("Time left is %d units",t*T);
}
}
return 0;
}
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.
L, comprised of characters mentioned above should be single spaced delimited.
Number of characters in the text, |L| < = 10000
Sample Input and Output
SNo.
Input
3
{<>(P)}
{<{}>({}))
{({})}
Output
No Compilation Errors
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
#include<stdio.h>
int main()
{
int T;
char arr[100][10000];
scanf(%d,&T);
while(T>0)
{
int i,length,e=0,mf=0,nmf=0,uf=0,l=0;
scanf(%s,arr[T]);
length=strlen(arr[T]);
if(arr[T][0]!='{ || arr[T][length-1]!=})
e++;
for(i=1;i<length-1;i++)
{
if(arr[T][i]=='< && mf==0 && uf==0 && l==0)
{
mf++;
nmf++;
}
else if(arr[T][i]==> && mf==1 && uf==0 && l==0)
mf;
else if(arr[T][i]=='( && mf==0 && uf==0 && l==0)
uf++;
else if(arr[T][i]==) && mf==0 && uf==1 && l==0)
uf;
else if(arr[T][i]=='{ && (mf==1 || uf==1))
l++;
else if(arr[T][i]==} && l>0 && (mf==1 || uf==1))
l;
else if(arr[T][i]==P);
else
e++;
}
if(e!=0 || uf!=0 || mf!=0 || nmf=1 || l!=0)
printf(\nCompilation Error\n);
else
printf(\nNo Compilation Error\n);
T;
}
return 0;
}
SNo
Input
.
1
5
Q00
C12
Q00
C23
Q00
C45
Output
0
1
0
1
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
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
Input Format:
Input will consist of three parts, viz.
1. Size of the matrix (N)
2. The matrix itself (A = N * N)
3. Various operations on the matrix, one operation on each line. (Beginning either with A, Q or U)
-1 will represent end of input.
Note:
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
SNo
Input
.
2
12
34
A 90
Q11
Q12
A 90
Q11
U116
Q22
-1
Explanation:
Output
3
1
4
6
Initial Matrix
12
34
12
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
Views 6535
/**
*
* @author VIK VIKASH VIKKU
* Business Logic class
*/
import java.util.ArrayList;
import java.util.Scanner;
public class LongestProgressiveSequence {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
ArrayList<Integer> ls = new ArrayList<Integer>();
Integer[] temp = new Integer[0];
for (int i = 0; i < t; 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
Sample Inputs and Outputs
Sr.No Input Output
1 3, 1,2,3 6
2 3, 10,10,10 30
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.
The program should obey the following constraints
1. The program should detect that inputs are either positive or negative numbers
2. The program should treat any alphabets or non-numeric characters as invalid inputs
3. Use double precision data types for handling valid inputs
File Format: Each element is delimited by a comma.
Input Format:
Line 1
Output Format:
Line 1
Line 3
Line 4
Line 5
Line 6
Sr.N
o
1
Input
C:/sort.txt
Input In File
Output
sort.txt : 1,2,3,2,3,1,3,1,3
Descending sort:
333322111
2
C:/sort.txt
sort.txt :
0,0.001,0.01,0.0001,0.0001,0.00
00001
3
4
C:/sort.txt
C:/sort.txt
sort.txt : 2,3,2,a,b,3
sort.txt : 2,3,3,?
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)
The problem should obey following constraints :
1. Valid range for this problem statement is between 0 and 150000 (One lakh fifty thousand)
2. First element of the range should be less than the last element in the range
3. Prime number index starts from 1
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
Line 1
Line 2
Line 3
Output Format
Line 1 For Valid inputs
Prime Number at Nth position in a given range,
followed by a new line
For Invalid input
Invalid Input,followed by a new line
OR
No prime number is present at this index,
followed by a new line as is applicable.
Sample Inputs and Outputs
Sr.No.
Input
Output
1
15
6
13
5
1000
94
503
1
15
9
No prime number
is present at this
index
59
96
0
Invalid Input
1
a
2
Invalid Input
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
Line N+2
Input Matrix 2:
Next Line
Next Line
Sr.No
1
Input
C:/matrix1.txt
C:/matrix2.txt
Input In File
matrix1.txt :
331
592
642
matrix2.txt:
211
155
389
Output
Input Matrix 1 :
331
592
642
Input Matrix 2 :
211
155
389
Valid Matrix dimensions for addition.
54 2
6 14 7
9 12 11
C:/matrix1.txt
C:/matrix2.txt
matrix1.txt :
731
725
264
matrix2.txt :
21
15
38
Input Matrix 1 :
731
725
264
Input Matrix 2 :
211
538
Invalid Matrix dimensions for addition. Cannot add
matrix of dimensions 3 with 3x2
C:/matrix1.txt
C:/matrix2.txt
matrix1.txt :
948
237
921
matrix2.txt :
123
456
1?3
INVALID INPUT
C:/matrix1.txt
C:/matrix2.txt
matrix1.txt:
abc
def
ghi
matrix2.txt:
285
852
024
INVALID INPUT
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.
Simple interest is calculated using the formula SI=PxNxR
Compound interest is calculated using the formula CI = {[P * (1+(R/100))^N] - P}
where, P = Principal Amount, N=Tenure in years and R=Rate of interest per annum,where N>0 &
P>0
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
Sr.No
1
Input
5000
8
2
Output
Simple Interest: 800.0
Compound Interest: 832.0
16000
0
8
-7856
3
6
1000
12.256
3
5680
10.67
?
8964
14
0
Invalid Input
Invalid Input
Reverse Gear
Drunkard's Walk
Minimum Distance
Recurring Deposit
MF Tracker
TestVita
ISL Schedule
Find Captures
Zombie World
Matrix Rotations
Valid Segments
Close Strings
Employee DB Design
Employee Hierarchy
Close Friends
Cyclic Palindrome
Zombie World
Game of Primes
CodeVita
Query Engine
Loan Structuring
Expense Solver
Pagination
Credits
Isotope Fusion
Brokerage
Helloworld
Hexogonal Triangle