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

Car is traveling

A car is traveling at a uniform speed.The driver sees a milestone showing a 2-digit number. After traveling for an hour the driver sees another milestone with the same digits in reverse order.After another hour the driver sees another milestone containing the same two digits. What is the average speed of the driver?

Ans

45 kmph

22 comments:

  1. say those two numbers are a and b

    so 1st and 2nd milstones displays ab and ba

    Few things deduced from it is

    1. ba > ab
    2. As the 3rd milestone can not have only these two numbers as it has to be different than ab or ba, we will have a three digit number for 3rd milestone which will contain both these numbers.

    for any a and b, ba-ab cant be >100 so the distance in two hours will be 2*(ba-ab) and will be some 1xx

    so one number is 1 and it has to be the a

    now if you say the average speed/hr is x

    (b*10+a) - (a*10+b) = x

    a=1 so

    9b-9 = x
    9b-x =9

    now

    fotr 2nd and 3rd milestone (assume the three digits are a,b and k) and

    b*10+a+x = a*100 + (either b*10 + k or k*10 +b)

    I tried different values for b here and found following valid

    16
    61
    106

    diff 45 (speed)

    ReplyDelete
  2. Can you pls tell me how did you get
    (b*10+a) - (a*10+b) = x

    I'm sorry if this qn sounds stupid.

    ReplyDelete
  3. reason is that you are in decimal system

    ReplyDelete
  4. please can have the ans in clarity pls

    ReplyDelete
  5. this eqn is correct

    ReplyDelete
  6. I don't understand why 1 has to 'a'.. It surely can't be b, but it can be the third digit..

    ReplyDelete
  7. 66kmph works or does the last milestone have to be 3? In fact any 11x s.t. 11x < 100 should work?

    ReplyDelete
  8. @Parijit, there is one flaw in your reasoning:

    ab to ba and ba to 1XX

    you are automatically assuming a = 1 instead of considering the scenario 1ab, in which case:

    Let the 2 digits be ab and the speed of the car be x kmph.

    Since the car is travelling at uniform speed,
    distance from ab to ba = x km

    Using above logic, ba > ab

    also, after another 1 hr, the driver saw the same digits in another milestone, so the new number has to be 1ab.

    so, the distance from ba to 1ab = x

    Finally, ab to ba = x
    ba to 1ab = x;
    distance between ab and 1ab = 100km

    2x = 100
    x=50;

    ReplyDelete
  9. @parijat
    when u travel in a car the value appearing on milestone should decrease, but u have assummed just opposite. please explain

    ReplyDelete
  10. ab=a*10 + b

    because , for example 41=4*10+1
    121=1*100+2*10+1

    ReplyDelete
  11. After moving from ba - ab = x.

    When finding the difference we know that b, of the second milestone, will be a multiple of 10. The same goes for a in the first milestone.

    ReplyDelete
  12. @thomas:
    number, 123 can be represented as,
    1 * 100 + 2 * 10 + 3 * 1,
    hence number with digits a, b can be represented as,
    a * 10 + b
    and the reverse of the number ab is ba which is,
    b *10 + a

    hope you understood!

    ReplyDelete
  13. Yeah, from where did u get the number 10 when you say,

    (b*10+a) - (a*10+b) = x

    I wonder if it's a part of the question(that might have got missed out) or I am not good enough in observing it in the question. :)

    ReplyDelete
  14. eg :expand 245

    2*100+4*10+5
    200
    40
    5

    ReplyDelete
  15. the distance in two hours will be 2*(ba-ab) and will be some 1xx.....
    i didnt get it.take that two mile stone 16 and 61.then 61-16=45.2*45=90...how it will be 1xx???

    ReplyDelete
  16. the milestones can be
    21
    12

    23
    32

    34
    43

    45
    54

    56
    65

    67
    76

    78
    87

    89
    98

    so as the car travels at uniform speed so the average speed will be the uniform speed only.

    Now the difference between the first two milestones will be 9kms and the car travels for 1 hour so 9 kms in 1 hour so uniform speed is 9kmph

    ReplyDelete
    Replies
    1. why can't the first two numbers be 24/42, 25/52, 26/62,....etc. or 35/53,36/63....etc or 46/64 etc. and so on

      Delete
  17. Parijat has answered it with a very good explanation 1 Yr before.... :)

    ReplyDelete
    Replies
    1. before start consider milestone showing 90
      after an hour it shows the reverse ie 09
      again after an hour it shows only the same digits ie either 99 or 00
      since 99 cant b it is 00
      ans avg speed= distance travld in 2hrs/2
      = 90/2
      = 45

      Delete
  18. How could this parajit creature ass-ume a=1!! 1ab could also be possible!!

    ReplyDelete
  19. Asume that uniform speed is X, A and B are digits. So BA>AB and BA-AB=x .
    @3th millstone he/she saw a 3 digit number. the number should be a number like this: 100+AB(the 100+BA is not acceptable, it's become very large than its speed) 100+AB= 1AB.
    So we have following equations :

    BA-AB=X
    (100+AB)-BA=X

    so by solving above equations(sum two equation and find X) we conclude that X=50.
    So simple :D.

    ReplyDelete