DSA
    advanced

    Advanced Binary Search Patterns

    Advanced Binary Search Patterns

    Visualize Binary Search on Answer Space through the Koko Eating Bananas (minimize-maximum) pattern. Watch how a feasibility predicate monotonically flips as the candidate answer grows, allowing binary search to locate the optimal value. The Learn tab covers maximize-minimum, rotated arrays, and other advanced variants.

    ⏱ O(n log M)
    💾 O(1)
    Ready
    Banana Piles (size)
    3
    6
    7
    11
    Answer Space [1..11] — searching for minimum feasible speed
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ← too slow (not feasible)too fast (feasible) →
    lo=1hi=11

    Problem: Koko eats piles [3,6,7,11] in 8h. Find minimum speed k. Search space: [1..11]. "Is k=X feasible?" monotonically flips false→true.

    50%
    Live Variables
    piles3,6,7,11
    hours8
    lo1
    hi11

    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

    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