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
two trains are 2 kms apart. Speed of one train is 20m/s and the other train is running at 30 m/s. Lengths of the trains are 200 and 300m. In how much time do the trains cross each other?
Which is the biggest perfect square amongst the following 15129, 12348, 23716, 20736
A family I know 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?
Draw venn diagram relating rhombus, quadrilateral & polygon
Find the reminder when 333666777888999 divided by 3 or 9 or 11 ?
complete the series: x y z u v w r s t __
Find the value of ( 0.75 * 0.75 * 0.75 - 0.001 ) / ( 0.75 * 0.75 - 0.075 + 0.01)?
A can have a piece of work done in 8 days, B can work three times faster than the A, C can work five times faster than A. How many days will they take to do the work together ?
Cost of an item is Rs 12.60 ,profit is 10% over selling price.
two workers can type two pages in two minuets then how many persons can type 18 pages in 6 minuets
Find the product of the prime numbers between 1-20
Find the longest palendrom in a string? Example Input: abfgerccdedccfgfer Output: ccdedcc i want a solution in C/C++ language
Hansie made the following amounts in seven games of cricket in India: Rs.10, Rs.15, Rs.21, Rs.12, Rs.18, and Rs.17(all figures in crores of course). Find his average earnings.
A boy has Rs 2. He wins or loses Re 1 at a time If he wins he gets Re 1 and if he loses the game he loses Re 1. He can loose only 5 times. He is out of the game if he earns Rs 5. Find the number of ways in which this is possible?
A sales person multiplied a number and get the answer is 3, instead of that number divided by 3. what is the answer he actually has to get ? (1/3). 7. A ship started from port and moving with I mph and another ship started from L and moving with H mph. At which place these two ships meet ?