Important TCS CodeVita questions solved – 2015
Problem : Catch22
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 
Important TCS CodeVita Questions Solved – catch22 Problem
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; while(FD>0 && BD>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; } //validate the constraints too ** check other solved codevita questions
Kindly update your program, because it does not satisfy all kinds of input,
take this input : 8 8 2 10 6
This should give output No ditch, since first move will always be in forward direction.
But your program will give that because in no ditch condition, you have included B<BD, which wont satisfy for above input and hence it will produce infinite loop.
I have written a C++ program which satisfy all conditions.
friend, the robot moves in both forward and backward directions, we need to consider both – first move is in forward direction and second move is in backward direction. As in your input at second move the backward distance that should be moved is 8 and their is a ditch after 6. So definetly, its not a no ditch condition. The robot will fall in the ditch at a distance 10(FD)+6(BD) = at 16th unit of time.
is there any other test failures ?
Let me explain you mathematically,
suppose R be robot, FD forward ditch, BD backward ditch
my input was 8 8 2 10 6
Initial position of R : 0
FD : 10
BD : 6 (negative as it is backward)
Step 1: Intial diagram before any movement
BD(6)_______R(0)______________FD(10)
Step 2: Robot moves 8m forward as first move will always be forward
BD(6)_______(0)____>>____R(8)__FD(10)
Step 3: Now Robot will move 8m backward,
BD(6)_______R(0)___<>____R(8)__FD(10)
Step 4: Now again robot will move 8m forward,
BD(6)_______R(0)___<<_____(8)__FD(10)
hence the robot will continously move between 0 and 8 moving forward and backward. it will never go to 6 position, i.e., 6 backward from initial position 0.
I am requesting You my friend just understand the question.
i am sorry my friend but this is rectified answer, the diagrams in the previous one were incorrect by mistake
suppose R be robot, FD forward ditch, BD backward ditch
my input was 8 8 2 10 6
Initial position of R : 0
FD : 10
BD : 6 (negative as it is backward)
Step 1: Intial diagram before any movement
BD(6)_______R(0)______________FD(10)
Step 2: Robot moves 8m forward as first move will always be forward
BD(6)_______(0)____>>____R(8)__FD(10)
Step 3: Now Robot will move 8m backward,
BD(6)_______R(0)___<>_____R(8)__FD(10)
Step 5: Now Robot will move 8m backward,
BD(6)_______R(0)___<<_____(8)__FD(10)
hence the robot will continously move between 0 and 8 moving forward and backward. it will never go to 6 position, i.e., 6 backward from initial position 0.
the reply section has an error, its not giving my full answer. See its misses step 4 which was for forward that the robot moves 8m forward, but i hope u understood my point.