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
4 men can cross a bridge in 3,7,13, 17 minutes. Only two can cross the bridge at a time. The time taken by the two to cross the bridge is equal to the time taken by the slowest person of the two. Find the minimum time to all of them cross the bridge?
The cost of one pencil, two pens and four erasers is Rs.22 while the cost of five pencils, four pens and two erasers is Rs.32.How much will three pencils, three pens and three erasers cost?
There N stations on a railroad. After adding x stations 46 additional tickets have to be printed. Find N and X.
If the simple interest is 10.5 % annual and compound interest is 10 % annual, compounded annually, find the difference between the interests after 3 years on a sum of Rs. 1,000.
Write a C Program to declare an array for 2 0 floats. Accept the values from the user sort the two arrays in descending order. Merge the two arrays into a new array and display the new array.
In Bangalore, during income tax deduction for a single person, the percentage increased by 3% and for middle house-holders, it decreased by 3%. What can you infer?
How many people are there in the queue if I am 7th in the queue from either end?
govern of irda,sebi and rbi
The house is big and lovely but since nobody uses it it is pretty much a white Elephant What does White Elephant mean?
if area is icc. by 30% then what is the change in the area?
Sum of money at compound interest amounts of thrice itself in 3 years. In how many years will it take 9 times itself.
work can be done by 8 men and 10 women in 25 days, the same work can be done by 10 children and 5 women . in how many days 2 children and 3 men
8 persons A,B,C,D,E,F,G,H are sitting in a round dining table.some arrangement was given and position of H was asked.I don't remember the question exactly.
Argentina had football team of 22 player of which captain is from Brazilian team and goalki from European team. For remaining player they have picked 6 from Argentinian and 14 from European. Now for a team of 11 they must have goalki and captain so out of 9 now they plan to select 3 from Argentinian and 6 from European. Find out no. of methods available for it
there is a 4 inch cube painted on all sides. this is cut into no of 1 inch cubes. what is the no of cubes which have no pointed sides.