Amazon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Two trains enter from opposite directions with speed V, hence their total speed is 2V.
Time it takes for both the trains to meet: L/2V ( time = distance/speed)
Now, we need to find the distance travelled by the particle in this time. This distance = time*speed.
Distance = v*L/2V
Being polite I could say this answer is half way to the correct one.
For example fly v= 90, Train V=30, Tunnel L=16 leaves the fly's position 90x16/2x30 = 24 which is wrong since the particle is vibrating within 16 unit tunnel.
See Richard's solution in this post and I think he got it right for us.
One vote down from me to this answer.
As both the trains are running with same speed..... they will meet in the tunnel only after they reach half of the tunnel... and as the length of tunnel is L the distance covered will be ( L/2 ) for each train. the time taken will be ((L/2) / V)... that means L/2V..... now as the particle is running with speed v it has a position at (L/2V)*v...
so the answer is ----> Lv / 2V
@saraf ..%L is done becoz it may happen the distance travelled will be more than L..So it retraces its path again..Correct me If I m wrong!!!
actually you just cant do modulo L. Because say in the 2nd or 3rd vibration of the particle the distance between the trains will not be 'L'
Lets make problem very simple.
Time taken to meet the trains= L/2V {Total relative distance/Relative speed}
In the same time distance traveled by particle = v * L/2V {speed*time}
Hence position of particle in the tunnel can be determined as: after completing a total of 'n' one way rounds, it will be at distance d from any end of the tunnel, where d can be described as:
d = v * L/2V - n*L , where n is the largest integer among {0,1,2,3,4,....} for which:
v * L/2V >= n*L
And, if n is even integer(including 0), then d will be a position from the end of the tunnel from which particle was first entered, else if n is odd integer, the d is the position from the other end of the tunnel.
In other words,
If greatest integer n{0,1,2,3,4..} for which d (where d = v * L/2V - n*L ) is positive, then position D from the end of the tunnel from which particle entered is defined as,
D = d if n is even, else
L-d if n is odd.
Agreed Rahul.
Lets make problem very simple. Explaining in few more words.
Time taken to meet the trains= L/2V {Total relative distance/Relative speed}
In the same time distance traveled by particle = v * L/2V {speed*time}
Hence position of particle in the tunnel can be determined as: after completing a total of 'n' one way rounds, it will be at distance d from any end of the tunnel, where d can be described as:
d = v * L/2V - n*L , where n is the largest integer among {0,1,2,3,4,....} for which:
v * L/2V >= n*L
And, if n is even integer(including 0), then d will be a position from the end of the tunnel from which particle was first entered, else if n is odd integer, the d is the position from the other end of the tunnel.
In other words,
If greatest integer n{0,1,2,3,4..} for which d (where d = v * L/2V - n*L ) is positive, then position D from the end of the tunnel from which particle entered is defined as,
D = d if n is even, else
L-d if n is odd.
First I also thought like others. But then I I realized that v* L/2V gives the total distance travelled by the particle. But we need to find the position of the particle from one of the ends of the tunnel.
I don't know if my reasoning is correct or not. But after thinking for a while I felt this.
If both the trains are coming with speed V from opposite directions, they will meet at the center of the tunnel i.e after L/2 distance. If the particle is vibrating to and fro between the trains, the particle should at L/2 at the end. Right? Please comment my approach.
The two trains meet at L/2 distance, So the time at which they meet, will be:
time_train_meet = L/2V [V = Speed of train]
Now distance traveled by particle in time 'time_train_meet' will be :
distance_covered_by_particle = vL/2V
Now,
if (distance_covered_by_particle > L){
// Find out how much time has the particle vibrated to and fro
n = distance_covered_by_particle % L
if(n == 0){
//Particle is at either ends of the tunnel
n = distance_covered_by_particle / L;
if( n is odd){
// particle is at the start of the tunnel; return;
PARTICLE_IS_AT = 0;
return;
}else{
// particle is at the end of tunnel
PARTICLE_IS_AT = L;
return;
}
}else{
n = (int) distance_covered_by_particle / L
// particle is at distance_covered_by_particle - (n*L)
PARTICLE_IS_AT = distance_covered_by_particle - (n*L);
return;
}
}else{
//distance traveled by particle is same as 'distance_covered_by_particle'
PARTICLE_IS_AT = distance_covered_by_particle;
return;
}
The two trains meet at L/2 distance, So the time at which they meet, will be:
time_train_meet = L/2V [V = Speed of train]
Now distance traveled by particle in time 'time_train_meet' will be :
distance_covered_by_particle = vL/2V
Now,
if (distance_covered_by_particle > L){
// Find out how much time has the particle vibrated to and fro
n = distance_covered_by_particle % L
if(n == 0){
//Particle is at either ends of the tunnel
n = distance_covered_by_particle / L;
if( n is odd){
// particle is at the start of the tunnel; return;
PARTICLE_IS_AT = 0;
return;
}else{
// particle is at the end of tunnel
PARTICLE_IS_AT = L;
return;
}
}else{
n = (int) distance_covered_by_particle / L
// particle is at distance_covered_by_particle - (n*L)
PARTICLE_IS_AT = distance_covered_by_particle - (n*L);
return;
}
}else{
//distance traveled by particle is same as 'distance_covered_by_particle'
PARTICLE_IS_AT = distance_covered_by_particle;
return;
}
I think answer to the given question is quite easy, nothing complicated is asked here.
It just asks the position of the particle when trains meet.
and as trains are travelling at same speed V in opposite directions, they meet at L/2. so the particle also will be at the same position that is L/2.
and if question was to calculate the distance travelled by the particle then also its pretty simple, as everybody has explained here.
But in my opinion this was asked to know if you are paying attention to the question. As this is pretty popular question.
The question says very clearly position, not distance.
If anyone's opinion differs, then comment please.
I think answer to the given question is quite easy, nothing complicated is asked here.
It just asks the position of the particle when trains meet.
and as trains are travelling at same speed V in opposite directions, they meet at L/2. so the particle also will be at the same position that is L/2.
and if question was to calculate the distance travelled by the particle then also its pretty simple, as everybody has explained here.
But in my opinion this was asked to know if you are paying attention to the question. As this is pretty popular question.
The question says very clearly position, not distance.
If anyone's opinion differs, then comment please.
Suppose one end of the tunnel is at 0 point, and the other end is at L point. Suppose the particle come from 0 to L. Then obviously the total distance of the particle is s = Lv/2V. Since s may be larger than L, this means the particle may reach the L point and go back towards 0, and then reach 0 point and go to L again...
- Richard September 05, 2012So, if s/L (integer division) is odd, it means the particle is going from L to 0 when the two trains meet. Therefore, the position of the particle should be L - s % L. Otherwise, the position is s % L.
Please correct me if I'm wrong.