Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
PROBLEM-SOLVING TECHNIQUES

Binary Search on Answer

Binary search over possible answers when a yes/no check flips only once.

Advanced8 problems3 interactive subtopicsO(n log m) time · O(1) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (3)Open in full playground →
STEP 1/5The answer is between 1 and 11
try speeds 1 to 11
speed
012345678910
1
2
3
4
5
6
7
8
9
10
11
lo
hi
  • inside the window
  • result
  • ruled out
  • removed / popped
Space Play←→ StepR Replay
1 / 5
PYTHONpiles = [3, 6, 7, 11], h = 8
1def min_eating_speed(piles, h):
▶2 lo, hi = 1, max(piles)
3 while lo < hi:
4 mid = (lo + hi) // 2
5 hours = sum((p + mid - 1) // mid for p in piles)
6 if hours <= h:
7 hi = mid
8 else:
9 lo = mid + 1
10 return lo
STEP 1 OF 5Setup

The answer is between 1 and 11

Speed 11 finishes any pile in an hour, so it always works.

VARIABLES

lo
1
hi
11
mid
-
hours
-
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

Instead of searching an array, search the range of candidate answers. A feasibility check says whether an answer works - and if it does, every larger (or smaller) one does too.

Why it matters

“Find the smallest X that works” becomes O(log range) checks, each usually O(n).

Think of it like this

Picking the smallest suitcase: if size 50 fits everything, so does 60 - so try the middle size and halve.

Use it when

  • “Minimize the maximum” or “maximize the minimum”
  • A monotonic yes/no check is easy to write
  • A huge answer range, up to 10⁹

Avoid it when

  • The check isn’t monotonic
  • A formula or a greedy pass gives the answer directly

Complexity cheatsheet

OperationTimeSpace
Binary search over a range of size mO(n log m)O(1)
Try every answerO(n × m)O(1)
One feasibility checkO(n)O(1)

Often confused with: plain binary search - it looks for a value in sorted data; this searches the space of answers.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

minimum speedcapacitywithin D daysminimize the maximumat least / at most
  • The answer is often bounded by max(nums) or sum(nums)
  • An O(n) check gives O(n log range) overall
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Using lo <= hi together with hi = mid - an infinite loop
  • Starting lo at 0 - the check divides by zero
  • Testing feasibility in the wrong direction

Edge cases to test

  • h equals the number of piles - the answer is max(piles)
  • A single pile
  • Very large values - keep the check in integer maths
Interview tip: Say the monotonic property out loud: “if speed k works, every faster speed works too.”
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

What must be true for binary search on the answer to work?

06

Practice

Apply what you just watched on real interview problems.

Generate Binary Search problem in AI DSA Practice →
  • BeginnerSqrt(x)LeetCode 69 ↗ (opens in a new tab)
  • IntermediateKoko Eating BananasLeetCode 875 ↗ (opens in a new tab)
  • AdvancedSplit Array Largest SumLeetCode 410 ↗ (opens in a new tab)
Fast & Slow PointersMerge Intervals