Cracking the Coding Interview: Fourth Edition (308 page e-book / PDF)
Delivered instantly as a PDF via email. For Software Engineers & SDETs
  • 150 programming interview questions and answers
  • 5 proven approaches to crack algorithm questions
  • 10 mistakes candidates make, and how to avoid them.
  • How to prepare for technical and behavioral questions without wasting your time!
"The BEST book for acing your interview. It helped me land my Microsoft job, and it was worth every penny!" - Ravi (Accepted at Microsoft, Amazon and Facebook) 30 Day Money Back Guarantee: Don't love the book? We'll give you your money back! More Info

Wednesday, April 28, 2010

Two trains enter at the opposite

Two trains enter at the opposite sides of a tunnel of length L with speeds 'V'. A particle enters the tunnel at the same time with a speed 'v' and it vibrates in the tunnel[i.e. if it reaches the end of the tunnel then it comes back]. What is the position of the particle by the time the 2 trains meet?

Ans

If v<=2V then the position is (v*L)/(2*V) from the starting point else it is 2*L -(v*L)/(2*V) from the starting point.

5 comments:

  1. if v> 2V thrn , 2*L -(v*L)/(2*V) from the starting point.
    this is not valid if v > 4V.

    ReplyDelete
  2. 2*L -(v*L)/(2*V) from the starting point.
    this is not valid if v> 4V.

    ReplyDelete
  3. position is given by :
    [1 - cos((v/2V)*PI)] * (L/2)

    ReplyDelete
  4. total distance traveled by particle is 2*L -(v*L)/(2*V).

    Find distance = Mod (2*L -(v*L)/(2*V), 2*L)
    If distance < L;
    distance from start = distance
    else
    distance from start = 2L - distance

    ReplyDelete
  5. Inputs:
    Length of the tunnel = L
    Speed of each train = V
    Since trains are travelling opposite to meet together, altogether speed: 2V
    So, Time taken to meet the trains = L/2V
    Given the speed of the particle is v. So the total distance the particle travelled = time taken * v = (L/2V)*v = vL/2V....

    Now lets find howmany times particle travelled inside the tunnel from one end to another i.e. total distance devided by length of the tunnel. i.e. floor((vL/2V)/L). If this is an even number the particle has completed one cycle(i.e. travelled to the other end and came back to the starting point) and going away from the starting point. Then answer is vL/2V-floor((vL/2V)/L)*L.

    If the value for total distance/L is odd, particle is coming back from the other end and it has passed vL/2V-floor((vL/2V)/L)*L distance from the other end. So distance from the starting point = L -(vL/2V-floor((vL/2V)/L)*L)

    Final answer can be:
    vL/2V-floor((vL/2V)/L)*L
    or
    L -(vL/2V-floor((vL/2V)/L)*L)

    --Sanish

    ReplyDelete