Halve the search space every step — on sorted arrays, and on answers, ranges, and allocations.
Halve a sorted interval until the target appears.
O(log n)naïve O(n)When to reach for it
Spot it in the prompt
Look for scenarios where the array is sorted, and you need to find a specific element efficiently.
How to solve it
Binary search for the target; when absent, the final low pointer marks the index where it should be inserted.
Open full solutionBinary search the answer, not the array.
O(n log(range))When to reach for it
Spot it in the prompt
Look for scenarios where you need to optimize a value within a given range, such as maximizing or minimizing a certain condition.
How to solve it
Animation: Range Search techniqueBinary search the largest allowed subarray sum; greedily count required splits to test feasibility at each guess.
Open full solutionSearch the smallest max that a greedy split allows.
O(n log(range))When to reach for it
Spot it in the prompt
Look for scenarios where resources need to be distributed among entities with certain constraints, and the goal is to optimize a certain criterion, such as minimizing the maximum or maximizing the minimum.
How to solve it
Animation: Allocation Problems techniqueBinary search the eating speed; for each candidate rate sum the hours needed and shrink toward the smallest rate that still finishes in time.
Open full solutionFind boundaries with binary search, then subtract indices.
O(log n)naïve O(n)When to reach for it
Spot it in the prompt
Look for scenarios where you need to count occurrences or determine the frequency of certain elements/values in a sorted array or range.
How to solve it
Animation: Counting Occurrences techniqueRun two binary searches for the leftmost and rightmost target indices; their difference gives the occurrence range.
Open full solutionFollow the slope to the peak in log time.
O(log n)naïve O(n)When to reach for it
Spot it in the prompt
Look for scenarios where the array exhibits a bitonic behavior, and you need to find an element within it efficiently.
How to solve it
Animation: Bitonic Array Search techniqueBinary search toward the higher neighbor since that side is guaranteed to contain a peak element.
Open full solutionFDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI