Ctrl K

A Safe Budget for Guessing

Medium
A host picks a secret integer from 1 to n. You name numbers one at a time. If you hit the secret, the game is over. If you miss, you pay as many coins as the number you named, and the host tells you whether the secret is higher or lower. Return the smallest number of coins that guarantees you can keep guessing until you win, no matter which number the host picked. You must never run out of money along the way.
Example 1
n=3 → 2

Guess 2. If that is wrong, the host says "higher" or "lower" and only one number is left. The worst case costs 2.

Example 2
n=5 → 6

Start with 2: if the number is lower, it is 1 and the total is 2. If higher, 3..5 remain; guess 4, and the worst case is 2 + 4 = 6.

Example 3
n=1 → 0
Constraints
  • 1 ≤ n ≤ 200
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShared Pool Race to a Goal MediumDuel for the End Cards MediumDuel for the End Heaps MediumThe Growing Grab Limit MediumScoring What Remains Medium
loading Python…
Input
n=3
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 31 tests on the server, hidden ones included.