DSA
    advanced

    Advanced Binary Search Patterns

    Software Engineer path · lesson 48 of 99
    time O(n log M)
    space O(1)

    Pile sizes 1–40. Koko eats up to k bananas from one pile per hour.

    Try:
    Hours available (h)

    Fewer hours force a faster minimum speed. h is raised to the number of piles if it is lower.

    piles36711
    speed kcandidates1234567891011
    Trying kFast enoughToo slowRuled out

    Search speeds 1..11. "Finishes in 8h?" flips from no to yes exactly once, so binary search works on the answer.

    Code (running line highlighted)
    1. 1lo, hi = 1, max(piles)
    2. 2while lo < hi:
    3. 3 k = (lo + hi) // 2
    4. 4 hours = sum(ceil(p / k) for p in piles)
    5. 5 if hours <= h: hi = k # k works; try slower
    6. 6 else: lo = k + 1 # too slow
    7. 7return lo
    Speeds tried
    0 / 4
    Invariant: The slowest speed that finishes in 8h is in [1..11].

    Tap any element in the diagram to inspect its state at this step.

    1 / 10

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    Binary search on the answer

    Sequence

    Order the steps for Koko’s minimum eating speed.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    When should you use Binary Search on Answer?

    Write the code

    Runs your solution

    Koko Eating Bananas

    Koko loves to eat bananas. There are n piles of bananas with piles[i] bananas. She can eat at speed k bananas/hour (one pile at a time). Return the minimum k such that she can eat all bananas within h hours.

    Example Cases

    Input:[[3,6,7,11], 8]
    Output:4
    Input:[[30,11,23,4,20], 5]
    Output:30
    Input:[[30,11,23,4,20], 6]
    Output:23
    javascript
    function minEatingSpeed(piles, h) {
      // Your code here
    }

    The short version

    Imagine finding the perfect meeting time for everyone. You don't check every minute - you guess a time, ask 'Is this too early or too late?', then adjust. Binary Search on Answer finds optimal values by repeatedly testing 'Is X achievable?'

    In more depth

    Binary Search on Answer Space

    Instead of searching an array, you search a range of possible answers [lo, hi] to find the optimal value.

    The Pattern:

    1.Define search space: What are the min/max possible answers?
    2.Design feasibility check: Can we achieve result ≤ mid? (O(n) typically)
    3.Binary search: If feasible, try smaller. If not, try larger.

    Common Variants:

    Minimize Maximum: Ship packages, split array largest sum
    Maximize Minimum: Aggressive cows, magnetic force between balls
    Rotated Array: Find minimum, search target in rotated sorted array

    Key Gotchas:

    Off-by-one errors at boundaries (use lo < hi vs lo <= hi carefully)
    Ensure search space bounds are correct (not too narrow)
    Feasibility function must return consistent monotonic results

    When to reach for this

    • Problem asks for 'minimum maximum' or 'maximum minimum'
    • Can verify if answer X is achievable in O(n)
    • Answer space is bounded and monotonic
    • Keywords: 'at least', 'at most', 'minimize the maximum'

    Interview Questions

    Google
    Facebook
    Amazon
    Amazon
    Google
    Google
    Amazon
    Facebook
    Google
    Amazon
    Google
    Microsoft

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required