Gamified challenge · Binary search
A prize is hidden on one floor of a tower with floors 1..n. After each guess the lift operator replies higher, lower or here. The tower's rule: every guess must be a middle floor of the floors still possible (when an even number of floors is left, either of the two middle ones is fine).
The operator can't talk to your code, so you are told the prize floor secret instead. Write guess(n, secret) that returns the list of floors you would guess, in order, ending with the guess that hits secret. The checker replays the operator's replies and rejects any guess that isn't a middle floor of what is left.
Example: guess(10, 7) returns [5, 8, 6, 7]: 5 → higher (6..10 left), 8 → lower (6..7 left), 6 → higher, 7 → here. [6, 9, 8, 7] (always the upper middle) or [5, 8, 7] would also be accepted.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google