The “guess the number” game for arbitrary rational numbers?

后端 未结 8 1059
既然无缘
既然无缘 2020-12-12 09:12

I once got the following as an interview question:

I\'m thinking of a positive integer n. Come up with an algorithm that can guess it in O(lg n) quer

8条回答
  •  暖寄归人
    2020-12-12 10:05

    You can sort rational numbers in a given interval by for example the pair (denominator, numerator). Then to play the game you can

    1. Find the interval [0, N] using the doubling-step approach
    2. Given an interval [a, b] shoot for the rational with smallest denominator in the interval that is the closest to the center of the interval

    this is however probably still O(log(num/den) + den) (not sure and it's too early in the morning here to make me think clearly ;-) )

提交回复
热议问题