Identify a Hidden Number from Comparison Replies

Quick Overview

Identify a fixed hidden integer using an API that reports whether each proposal is too high, too low or correct.

Identify a Hidden Number from Comparison Replies

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Identify a Hidden Number from Comparison Replies A service chooses one fixed integer from `1` through `n`, inclusive. Implement `Solution.guessNumber(n)` to return that integer using only the provided `guess(number)` API. ### Interface The reply describes your proposed number relative to the hidden number: - `-1`: your proposal is too high. - `1`: your proposal is too low. - `0`: your proposal equals the hidden number. Only propose numbers in `1..n`. For this Python practice interface, construct `Solution(guess)` with the supplied callable and invoke `guessNumber(n)`. The candidate may call that API but may not inspect its owner, attributes, closure or source. The hidden number in an example is private setup data, not an argument to `guessNumber`. ### Examples **Example 1:** With `n = 20` and private hidden number `14`, `guessNumber(20)` returns `14`. For illustration, `guess(10)` would return `1`, `guess(17)` would return `-1`, and `guess(14)` would return `0`. **Example 2:** With `n = 9` and private hidden number `9`, `guessNumber(9)` returns `9`. A proposal of `8` is too low and receives `1`. ### Constraints - `1 <= n <= 2**31 - 1`. - The hidden number lies in `1..n` and never changes during a game. - Every reply follows the sign convention above. - No particular numeric API-call cap is part of the contract. ### What a Strong Answer Covers - Interpret both nonzero replies in the correct direction. - Return the exact hidden number, including endpoint and single-number games. - Explain termination and the number of API calls. ### Follow-up Questions - Which tests would reveal an implementation that reverses the two nonzero replies? - What assumptions must hold if the same client is used for several independent games?

Overview: Identify a fixed hidden integer using an API that reports whether each proposal is too high, too low or correct.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Identify a Hidden Number from Comparison Replies

A service chooses one fixed integer from 1 through n, inclusive. Implement Solution.guessNumber(n) to return that integer using only the provided guess(number) API.

Interface

The reply describes your proposed number relative to the hidden number:

  • -1 : your proposal is too high.
  • 1 : your proposal is too low.
  • 0 : your proposal equals the hidden number.

Only propose numbers in 1..n. For this Python practice interface, construct Solution(guess) with the supplied callable and invoke guessNumber(n). The candidate may call that API but may not inspect its owner, attributes, closure or source. The hidden number in an example is private setup data, not an argument to guessNumber.

Examples

Example 1: With n = 20 and private hidden number 14, guessNumber(20) returns 14. For illustration, guess(10) would return 1, guess(17) would return -1, and guess(14) would return 0.

Example 2: With n = 9 and private hidden number 9, guessNumber(9) returns 9. A proposal of 8 is too low and receives 1.

Constraints

  • 1 <= n <= 2**31 - 1 .
  • The hidden number lies in 1..n and never changes during a game.
  • Every reply follows the sign convention above.
  • No particular numeric API-call cap is part of the contract.

What a Strong Answer Covers Guidance

  • Interpret both nonzero replies in the correct direction.
  • Return the exact hidden number, including endpoint and single-number games.
  • Explain termination and the number of API calls.

Follow-up Questions Guidance

  • Which tests would reveal an implementation that reverses the two nonzero replies?
  • What assumptions must hold if the same client is used for several independent games?