A helicopter drops two trains, each on a parachute, onto a
straight infinite railway line. There is an undefined
distance between the two trains. Each faces the same
direction, and upon landing, the parachute attached to each
train falls to the ground next to the train and detaches.
Each train has a microchip that controls its motion. The
chips are identical. There is no way for the trains to know
where they are. You need to write the code in the chip to
make the trains bump into each other. Each line of code
takes a single clock cycle to execute.
You can use the following commands (and only these);
MF - moves the train forward
MB - moves the train backward
IF (P) - conditional that's satisfied if the train is next
to a parachute. There is no "then" to this IF statement.
GOTO
Answers were Sorted based on User's Feedback
Answer / rshadow
After I was asked this question (two weeks after my comment
#3) in a job interview and was guided to the solution - not
before I tried to give the oscillating solution from comment
#2 above, which can't be coded within the limitation of the
4 available commands - I give here the correct and final answer:
start:
MF
MF
MB
if (p) GOTO found
GOTO start
found:
MF
GOTO found
Explanation:
Both trains start moving in the same direction at the same
rate. After one train meets the parachute of the other
train, it starts going at a faster rate and thus will catch
up with the other train and bump into it.
It's a simple solution, but not that easy to come up with.
Seems to be a popular question in job interviews for
programmers in high-tech companies in Israel (me and a
friend were each asked this riddle at a job interview, in
two different companies).
| Is This Answer Correct ? | 26 Yes | 2 No |
Answer / vinayak
MF
IF (P)
GOTO B
GOTO A
-----
B: MF
GOTO B
Explanation: The first line simply gets them off the
parachutes. You
need to get the trains off their parachutes so the back
train can find
the front train's parachute, creating a special condition
that will
allow
it to break out of the code they both have to follow
initially. They
both loop through A: until the back train finds the front
train's
parachute, at which point it goes to B: and gets stuck in
that loop.
The front
train still hasn't found a parachute, so it keeps in the A loop.
Because each line of code takes a "clock cycle" to execute,
it takes
longer
to execute the A loop than the B loop, therefore the back train
(running
in the B loop) will catch up to the front train.
| Is This Answer Correct ? | 12 Yes | 2 No |
Answer / raghav
In answer #4 , I think there is no need of these 3 lines
MF
MF
MB
Instead we can use only 1 line
MF
Both produces the same result but 1 line command is more efficient i think.
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / jubalau
had this too. i was allowed to number the lines
1 mf
2 if p go to 4
3 go to 1
4 mf
5 go to 4
you could also add more mf lines between lines 4 and 5 for better results i guess...
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / rshadow
Rajasekar's answer is almost correct, but:
1. i has to be increased by 1 at each iteration of the while
loop.
2. since the 2 trains are facing the same direction at the
start, using this solution will not work - the 2 trains will
always go at the same direction and will never bump into
each other. To overcome this pitfall we need to use a
statement such as 'if (P) STOP', (or: if (P) GOTO END).
notice that above I assume that the condition if (P) is only
satisfied when the train is next to the OTHER train's
parachute. Otherwise, using a counter we can check if the
train is next to its own parachute or not.
| Is This Answer Correct ? | 5 Yes | 6 No |
Answer / rajasekar b
D answer sanket may be wrong. U gotta program both the chips.
and u cant say "MB(T1)" to train T2 and viceversa, neither
"IF(T1 at P1)". There is no way for the trains to know where
they are.
My answer is:
MF;
i=1;
do
{
for(j=1;j<=(i*2)+1;j++)
MB;
for(j=1;j<=(i*2)+2;j++)
MF;
}While(1);
This causes both the trains to oscillate about their
respective parachutes,moving a unit length longer in forward
and backward direction for each oscillation. sure, they
gotta BUMP into each other.
My assumption:
Usual programming language constructs can be used.
Given Commands are just for controlling the train.
| Is This Answer Correct ? | 4 Yes | 11 No |
Answer / sanket
Assumptions - the track goes North-South
Both trains either face or North or South
Two trains are T1 and T2 and their respective parachutes
are P1 and P2
Algorithm/code-
MF(T1) //Moves T1 in forward direction
MF(T2) //Moves T2 in forward direction
Label:If(T1 at P2) MB(T2)
If(T2 at P1) MB(T1)
GOTO Label
Description - Since both trains are facing same direction
(either N or S) if both start moving forward (say toward N)
at some point of time one train will cross other train's
parachute, at which point we reverse the other train. The
trains are now on a collision course.
| Is This Answer Correct ? | 4 Yes | 17 No |
You have 8 balls. One of them is defective and weighs less than others. You have a balance to measure balls against each other. In 2 weighings how do you find the defective one?
A Lorry starts from Banglore to Mysore At 6.00 a.m, 7.00 a.m, 8.00 a.m.....10 p.m. Similarly another Lorry on another side starts from Mysore to Banglore at 6.00 a.m, 7.00 a.m, 8.00 a.m.....10.00 p.m. A Lorry takes 9 hours to travel from Banglore to Mysore and vice versa.?
There are 5 red shoes, 4 green shoes. If one drasw randomly a shoe . what is the probability of getting redshoe
A certain street has 500 buildings. A sign-maker is contracted to number the houses from 1 to 500. How many zeroes will he need?
Three men A, B, C can complete a work separately in some specified days (May be 8, 7, 6).If they do the work together by alternate days. Then how many days need to complete the work?
if A = D, B=E, C=F, VWDUW = ?
A man brought a house for Rs. 5lakhs and rents it. he puts 12 1/2% of each month's rent aside for repairs, pays Rs.1660 as annual taxes and realizes 10% on his investment thereafter. the monthly rent of the house is : a) Rs.2460 b)Rs.2500 c)Rs.4920 d)Rs.5000
6. A teacher wants to change the seating arrangement in the class in order to improve the no. of comments given by the students in the class. She 1st decides to change the seating arrangement of the students in the class. The no. of comments made by 8 students during a period are given below: Serials A.A R.F G.H D.C A.F C.M S.S C.G No. of comments 0 5 2 22 2 3 4 1 What should be the method adopted by her to get the accurate results.
Sum of 5 consecutive numbers is 35. How many of the numers are prime:
There are coins of Rs.5, 2, 1,50p, 25p, 10p, 5p. each one has got a weight. Rs 5 coin weighs 20gms.find the minimum number of coins to get a total of 196.5gms.
the plane question was asked.it took off from 7 degree 53' 6.1'' and landed at 8 degree 6' 43.5''.it takes 12 hours to travel between these points.then what will be local time at destination ?
pick the odd one out bend shave chop whittle shear