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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

USA + USSR = PEACE ; P + E + A + C + E = ?

1172


What number should be added to or subtracted from each term of the ratio 17 : 24 so that it becomes equal to 1 : 2.

706


In the month of march a basketball team played 60 games and won 30% of its game played. After some time the team raised it to 50%. How many games did the team had won to attain this average.

720


A lorry starts from Banglore to Mysore at 6.00 A.M,7.00am.8.00 am.....10 pm. Similarly one another starts from Mysore to Banglore at 6.00 am,7.00 am, 8.00 am.....10.00pm. A lorry takes 9 hours to travel from Banglore to Mysore and vice versa. A lorry which has started at 6.00 am will cross how many lorries.

1548


Express 16 m/s into Km/hr

755






2 oranges,3 bananas and 4 apples cost Rs.15 . 3 ornages 2 bananas 1 apple costs Rs 10. what is the cost of 3 oranges, 3 bananas and 3 apples

810


A beats B by 10 meters and B beats C by 15 metres the A beats C by

709


if 8x+4y=6 and 4x+5y=7, x+y equlals

719


One passage was on playing cards. know how many diamonds and etc.

681


if the sum of five consecutive nos. 35? how many prime numbers are there :

739


Find odd one cat,dog,rabbit,hamster,elk.

897


A persons age is similar to the year he was born(last two digits is counted as age).

977


58,27,12,x,2,1. Find x.

890


What is the max possible 3 digit prime number?

783


How many 3-digit numbers will be there which are divisible by 19?

661