Skip to content
Decoder

Guess Number Higher or Lower

Easy

Problem

A secret number pick has been chosen from 1 to n. You cannot see it, but you are given a function guess(num) that tells you how your guess compares:

- -1 if your guess is too high (pick < num) - 1 if your guess is too low (pick > num) - 0 if you got it

Write guess_number(n, guess) (TypeScript: guessNumber(n, guess)) that returns pick. The tests count your calls, so use as few as possible.

Example 1

Input
n = 10, pick = 6
Output
6

Example 2

Input
n = 1, pick = 1
Output
1

Example 3

Input
n = 2, pick = 1
Output
1

Constraints

  • 1 <= n <= 2^31 - 1
  • 1 <= pick <= n

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.