Amazon Interview Question for Software Engineer / Developers


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.

- Richard September 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- buckCherry December 09, 2012 | Flag
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

- chandershivdasani September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- buckCherry December 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

v*L/(2*V)

- Kunal Bansal September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- san September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dude, dimensionally answer is not a distance...

- Anonymous September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- abc September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

Stop posting same question multiple times

- chandershivdasani September 04, 2012 | Flag Reply
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

- jayram singh September 04, 2012 | Flag Reply
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

- saraf2206 September 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Lucy September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- dev September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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'

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

dev's answer is the only correct answer.

- Anonymous September 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Lucy September 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

(Lv/2V) % L

- gnahzy September 04, 2012 | Flag Reply
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 )

hence the answer is -
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

- Rahul Balakavi September 05, 2012 | Flag Reply
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.

- Shishir September 05, 2012 | Flag Reply
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.

- Shishir September 05, 2012 | Flag Reply
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.

- Vijay September 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- ZhouZhou September 07, 2012 | Flag
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;
     }

- alchemist September 12, 2012 | Flag Reply
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;
     }

- alchemist September 12, 2012 | Flag Reply
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.

- Alok September 17, 2012 | Flag Reply
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.

- Alok September 17, 2012 | Flag Reply
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

- Kratos February 20, 2013 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More