374. Guess Number Higher or Lower
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find a secret number pick within a given range [1, n]. We have access to an API guess(num) which tells us if our guess is too high, too low, or correct. This is a classic search problem where the search space can be efficiently reduced. The feedback from the guess API (higher/lower) strongly suggests that a binary search approach is the most efficient way to solve this.
Solution
Guess Number Higher or Lower in Python
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
# 1 if num is lower than the picked number
# otherwise return 0
# def guess(num: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
low = 1
high = n
while low <= high:
# Use integer division //
# Python integers have arbitrary precision, but this formula is still good practice.
mid = low + (high - low) // 2
result = guess(mid)
if result == 0:
# Found the number
return mid
elif result == -1:
# The picked number is lower, so our guess was too high.
# Search in the lower half.
high = mid - 1
else: # result == 1
# The picked number is higher, so our guess was too low.
# Search in the upper half.
low = mid + 1
# This part is unreachable as a solution is guaranteed.
return -1
Guess Number Higher or Lower in Java
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/
public class Solution extends GuessGame {
public int guessNumber(int n) {
int low = 1;
int high = n;
while (low <= high) {
// Calculate mid to avoid potential overflow
int mid = low + (high - low) / 2;
int result = guess(mid);
if (result == 0) {
// Found the number
return mid;
} else if (result < 0) { // result == -1
// The picked number is lower, so our guess was too high.
// Search in the lower half.
high = mid - 1;
} else { // result == 1
// The picked number is higher, so our guess was too low.
// Search in the upper half.
low = mid + 1;
}
}
// This part should not be reached because a solution is guaranteed to exist.
return -1;
}
}
Guess Number Higher or Lower in C++
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* int guess(int num);
*/
class Solution {
public:
int guessNumber(int n) {
int low = 1;
int high = n;
while (low <= high) {
// Calculate mid to avoid potential overflow
int mid = low + (high - low) / 2;
int result = guess(mid);
if (result == 0) {
// Found the number
return mid;
} else if (result == -1) {
// The picked number is lower, so our guess was too high.
// Search in the lower half.
high = mid - 1;
} else { // result == 1
// The picked number is higher, so our guess was too low.
// Search in the upper half.
low = mid + 1;
}
}
// This part should not be reached because a solution is guaranteed to exist.
return -1;
}
};
Guess Number Higher or Lower in TypeScript
/**
* Forward declaration of guess API.
* @param {number} num your guess
* @return -1 if num is higher than the picked number
* 1 if num is lower than the picked number
* otherwise return 0
* var guess = function(num) {}
*/
function guessNumber(n: number): number {
let low = 1;
let high = n;
while (low <= high) {
// Calculate mid using floor division to get an integer
const mid = Math.floor(low + (high - low) / 2);
const result = guess(mid);
if (result === 0) {
// Found the number
return mid;
} else if (result === -1) {
// The picked number is lower, so our guess was too high.
// Search in the lower half.
high = mid - 1;
} else { // result === 1
// The picked number is higher, so our guess was too low.
// Search in the upper half.
low = mid + 1;
}
}
// This part is unreachable as a solution is guaranteed.
// We return -1 as a fallback for type safety.
return -1;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.