## Amazon Interview Question for Software Engineer / Developers

• 0

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
5
of 5 vote

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...

So, 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.

Comment hidden because of low score. Click to expand.
0

Richard - I think u got it right. One vote up to you.

Comment hidden because of low score. Click to expand.
3
of 5 vote

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

Comment hidden because of low score. Click to expand.
0

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.

Comment hidden because of low score. Click to expand.
1
of 1 vote

v*L/(2*V)

Comment hidden because of low score. Click to expand.
0

it will at v/2V from point of entrance of particle into tunnel

Comment hidden because of low score. Click to expand.
0

dude, dimensionally answer is not a distance...

Comment hidden because of low score. Click to expand.
0

relative velocity of particle with any of the train will be zero....so the particle will be at l/2

Comment hidden because of low score. Click to expand.
1
of 3 vote

Stop posting same question multiple times

Comment hidden because of low score. Click to expand.
0
of 0 vote

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

Comment hidden because of low score. Click to expand.
0
of 0 vote

(v*L/2V) % L..from point of entrance of particle into tunnel

Comment hidden because of low score. Click to expand.
0

@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!!!

Comment hidden because of low score. Click to expand.
0

u is diatance of particle form one end
u=((v*L)/(2*v))%2*L;
if(u>L)
2L-u;
else
u;

Comment hidden because of low score. Click to expand.
0

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'

Comment hidden because of low score. Click to expand.
0

Comment hidden because of low score. Click to expand.
0

@dev..can u please explain why %2L

Comment hidden because of low score. Click to expand.
0
of 0 vote

(Lv/2V) % L

Comment hidden because of low score. Click to expand.
0
of 0 vote

Lv/2V = nS+x ( where n is the number of times the tunnel has been traversed )

if n%2 == 1 then x is the distance
if n%2 == 0 then S-x is the distance

from the tunnel where it first started the journey

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Comment hidden because of low score. Click to expand.
0

It depends on whether the particle is vibrating between trains or tunnel.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

Comment hidden because of low score. Click to expand.
0
of 0 vote

In One Line
((1-floor(v/2V)%2)-(v/2V - floor(v/2V)))L

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.