Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

(1/10)18-(1/10)20=? a.99/1020 b.99/10 c.0.9 d. none of these

6 Answers   Infosys, MAHINDRA, Satyam,


In a club there are certain no. of males and females. If 15 females are absent then no. of males will be twice that of females. If 45 males are absent then female strength will be 5 times that of males. Find no. of males actually present.

0 Answers   Accenture,


Area of triangle=(1/2)*b*h base increased by 4 times & height is divided by 2, the net effect of area?

0 Answers   Wipro,


hash function,hash table,linked list,binary tree traversal

3 Answers   Syncfusion, University Exams,


) Peter and Paul are two friends. The sum of their ages is 42 years. Peter is twice as old as Paul was when Peter was as old as Paul is now. What is the present age of Peter?

9 Answers   TCS,


2. In an examination a student must answer 65% of the questions correctly. But it was found that after answer 7 questions corretly out of the first 14, the student qualified. What is the min no. of questions in the paper. (a) 22 (b) 18 (c) 20 (d) Can't say

2 Answers   CTS,


The price of sugar increases by 20%, by what % house-wife should reduce the consumption of sugar so that expenditure on sugar can be same as before

0 Answers   IIT Delhi,


A man leaves office daily at 7pm A driver with car comes from his home to pick him from office and bring back home One day he gets free at 5:30 and instead of waiting for driver he starts walking towards home. In the way he meets the car and returns home on car He reaches home 20 minutes earlier than usual. In how much time does the man reach home usually??

2 Answers   TCS, Verizon,


Complete the sequence A, E ,I ,M ,Q ,U , _ , _?

0 Answers   Wipro,


A single discount equivalent to three successive discounts of 20%, 25% and 10% is a) 55% b) 50% c)48% d)46% plz specify the method... thanks

21 Answers   ABC, Arihant, Ashok Leyland, CTS, DELL, Hyundai, Mahindra Comviva, PSTET, SSC, TATA,


A fly is there 1 feet below the ceiling right across a wall length is 30m at equal distance from both the ends. There is a spider 1 feet above floor right across the long wall eqidistant from both the ends. If the width of the room is 12m and 12m, what distance is to be travelled by the spider to catch the fly? if it takes the shortest path.

0 Answers   Infosys,


What is the angle of degree suspended when two hands of clock showing the time 2.30.

0 Answers   Hexaware,


Categories