Guess Number Higher or Lower
easyTime: O(log n)Space: O(1)
Signals to notice
sorted range 1 to nguess higher or lowerminimize guesses
Brute force first
Try every number from 1 to n. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.
The key insight
Binary search the range: guess the midpoint, adjust range based on feedback. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.
What must stay true
Each guess eliminates half the remaining candidates. As long as that statement keeps holding, you can trust the steps built on top of it.
Easy way to go wrong
Integer overflow when calculating mid — use left + (right - left) / 2 instead of (left + right) / 2. The fix is usually to return to the meaning of each move, not just the steps themselves.