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
A train 100 meter long takes 3 second to cross a man walking at the rate of 6km/hr in a direction opposite to that of the train. Find the speed of the train.
There are two balls touching each other circumferentially. The radius of the big ball is 4 times the diameter of the small ball.The outer small ball rotates in anticlockwise direction circumferentially over the bigger one at the rate of 16 rev/sec. The bigger wheel also rotates anticlockwise at N rev/sec. what is 'N' for the horizontal line from the centre of small wheel always is horizontal.
In which of the system, decimal number 194 is equal to 1234?
vernanlar : place :: finger print : ?
If A = x3y2x3y2 and B=xy3xy3, then find the HCF of A, B
If a bat costs Rs.30 in the year 1999 and Rs.250 in the year 2000.What is the percent increase in price?
There are 150 weights .Some are 1 kg weights and some are 2 kg weights. The sum of the weights is 260.What is the number of 1kg weights?
500 men are arranged in an array of 10 rows and 50 columns . ALL tallest among each row aare asked to fall out . And the shortest among THEM is A. Similarly after resuming that to their originaal podsitions that the shorteest among each column are asked to fall out. And the longest among them is B . Now who is taller among A and B ?
wind flows 160 miles in 330 min, for 80 miles how much time required.
How Can A Cake(circular) Be Cut Into 8 Pieces By Making Just 3 Cuts?
a man is 24 years older than his son. In two years, his age will be twice the age of his son. The present age of his son is?
there is a matrix N x N .Its elements consist of either value =1 or value=0. If there is a any zero in the row, then the output matrix should have all zeroes in that row. If there is a single zero in any column then that column should have all zeroes n the output matrix. write the function to perform these operations. i want a solution in c/c++ language
mention your extra-curricular interest. Which do you actively pursue? how do you see these developing in the future?
Bird is flying 120km/hr b/w b to r. Two trains at b to r at 60 kmph the distance traveled by the bird before it is killed.
A family has several children. Each boy in this family has as many sisters as brothers but each girl has twice as many brothers as sisters. How many brothers and sisters are there?