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



A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. There is..

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

Post New Answer

More General Aptitude Interview Questions

The major component of CNG is a. Ethane b. Propane c. Butane d. Methane

7 Answers   SSC,


How many bridges over yamuna river in Delhi?

1 Answers  


Jack & Jill went to the hill....In going to hill (upword Direction)...Jack went to the hill first nd waited for Jill for "x" hours.. Jack's speed was then twice of Jill.. In the down word direction Jill can go 3km in a hour, while Jack can go 1km in 1 hour.. Jill reach the ground 1st nd waited for Jack for "x" hours.. Total journey they traveled is 6 km.. Find the speed of Jack...

3 Answers   CTS,


A person runs 6 ft in 1/4 sec. how much it will run in 10 sec.

0 Answers   Accenture,


One Train Travels 200m From A To B With 70 Km/Ph And Returns To A With 80kmph, What Is The Average Of Their Speed?

0 Answers   TCS,






A train speeds past a pole in 15 seconds and a platform 100 m long in 25 seconds. Its length is

7 Answers   TCS,


In a certain code, ?BELIEF? is written as ?AFKKDI?. How is ?SELDOM? written in that code ? (A) RDKCNL (B) TFKENP (C) RFKENM (D) RFKFNP (E) None of these

3 Answers  


You have 5 jars of pills. Each pill weighs 10 gram, except for contaminated pills contained in one jar, where each pill weighs 9 gm. Given a scale, how could you tell which jar had the contaminated pills in just one measurement?

7 Answers   Alcatel, Alcatel-Lucent, Lazada, Microsoft, Qubercomm,


pulizter prize : related to which country ?

1 Answers   ONGC,


What will you do after 5 years?

3 Answers  


A building with height D shadow upto G. What is the height of a neighbouring building with a shadow of C

0 Answers   TCS,


7) Jane works at a fashion design company, and is having problems getting dressed for work. She refuses to wear any color combination that does not go well together as many of her clients may look down upon this. She has two pairs of skirts, brown and blue; three blouses, white, sky blue, and gray; four pairs of stockings, red, black, brown, and blue; and two pairs of shoes, black and brown. The blue skirt cannot be worn with red or brown stockings. Gray does not go well with brown. Black does not go well with brown. If Jane wears black shoes she will not wear: a) red stockings. b) a blue skirt. c) a white blouse. d) blue stockings. e) a sky blue blouse. 8) Jane works at a fashion design company, and is having problems getting dressed for work. She refuses to wear any color combination that does not go well together as many of her clients may look down upon this. She has two pairs of skirts, brown and blue; three blouses, white, sky blue, and gray; four pairs of stockings, red, black, brown, and blue; and two pairs of shoes, black and brown. The blue skirt cannot be worn with red or brown stockings. Gray does not go well with brown. Black does not go well with brown. If Jane is color blind and is unable to determine what outfits went well together, how many possible clothing combinations could she have? a) 24 b) 32 c) 36 d) 44 e) 48 9) Jane works at a fashion design company, and is having problems getting dressed for work. She refuses to wear any color combination that does not go well together as many of her clients may look down upon this. She has two pairs of skirts, brown and blue; three blouses, white, sky blue, and gray; four pairs of stockings, red, black, brown, and blue; and two pairs of shoes, black and brown. The blue skirt cannot be worn with red or brown stockings. Gray does not go well with brown. Black does not go well with brown. If Jane wears a brown skirt and a white blouse, she could: a) not wear blue stockings. b) not wear brown shoes. c) not wear black shoes. d) wear blue stockings. e) wear red stockings. 10) Jane works at a fashion design company, and is having problems getting dressed for work. She refuses to wear any color combination that does not go well together as many of her clients may look down upon this. She has two pairs of skirts, brown and blue; three blouses, white, sky blue, and gray; four pairs of stockings, red, black, brown, and blue; and two pairs of shoes, black and brown. The blue skirt cannot be worn with red or brown stockings. Gray does not go well with brown. Black does not go well with brown. Jane buys a gray scarf. If she wears the new scarf, then she could: a) not wear blue stockings. b) not wear brown stockings. c) not wear black shoes. d) wear a white blouse. e) wear black stockings. 11) Jane works at a fashion design company, and is having problems getting dressed for work. She refuses to wear any color combination that does not go well together as many of her clients may look down upon this. She has two pairs of skirts, brown and blue; three blouses, white, sky blue, and gray; four pairs of stockings, red, black, brown, and blue; and two pairs of shoes, black and brown. The blue skirt cannot be worn with red or brown stockings. Gray does not go well with brown. Black does not go well with brown. Jane will never wear: a) blue and red together. b) white and red together. c) gray and blue together. d) white and black together. e) white and red together. 12) Two men, Barry and David, and two women Ann and Cathy are doctors. One is a surgeon, one a dentist, one an optometrist, and one is a general practitioner. They are seated around a square table, with one person on each side. 1) Barry is across from the dentist. 2) David is not across from the surgeon. 3) The optometrist is on Ann's immediate left. 4) Cathy is the general practitioner. 5) The surgeon and general practitioner are married to each other. 6) The general practitioner is not on Cathy's immediate left. 7) The general practitioner is across from the optometrist. Which of the following must be true? a) Barry is the dentist. b) The surgeon and general practitioner are women. c) The dentist is across from the surgeon. d) David is the surgeon. e) Cathy is across from Ann. 13) Two men, Barry and David, and two women Ann and Cathy are doctors. One is a surgeon, one a dentist, one an optometrist, and one is a general practitioner. They are seated around a square table, with one person on each side. 1) Barry is across from the dentist. 2) David is not across from the surgeon. 3) The optometrist is on Ann's immediate left. 4) Cathy is the general practitioner. 5) The surgeon and general practitioner are married to each other. 6) The general practitioner is not on Cathy's immediate left. 7) The general practitioner is across from the optometrist. If both women leave the table, the a) optometrist and dentist remain. b) surgeon and optometrist remain. c) surgeon and general practitioner remain. d) general practitioner and dentist remain. e) general practitioner and optometrist remain.

0 Answers  


Categories