Binary Search Coding Problems: 95 Questions with Solutions

95 binary search coding problems — 16 easy · 58 medium · 21 hard — with solutions in 13 languages. Plus a step-by-step walkthrough and a 8-day plan.

  • Problems: 95
  • By difficulty: 16 easy · 58 medium · 21 hard
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
  • Cost: Free on every plan; sign in to run and submit

Binary search halves a sorted range every step, finding a value — or the boundary where a condition flips — in logarithmic time. Beyond searching a sorted array it answers "what is the smallest x for which this is possible?" over any monotone predicate. The problems here practise the search over indices, the search over answers, and the off-by-one care the boundaries demand.

How binary search works, step by step

nums205182123164235386567728lohimidlo = 2, hi = 3, mid = 2nums[2] = 8, the target
Finding a number in a sorted array, with binary search. Example: nums = [2, 5, 8, 12, 16, 23, 38, 56, 72], target = 8
  1. The array is sorted, so if 8 is anywhere it lies between lo and hi, which start at the two ends. Each step looks at the middle and throws away the half that cannot hold it.
  2. mid = (0 + 8) / 2 = 4 and nums[4] = 16 is more than 8. Everything from index 4 on is at least 16, so the target can only be to the left.
  3. hi moves to mid − 1 = 3, so 5 numbers leave the search in one step; 4 numbers remain between lo and hi.
  4. mid = (0 + 3) / 2 = 1, rounded down, and nums[1] = 5 is less than 8. Everything up to index 1 is at most 5, so the target can only be to the right.
  5. lo moves to mid + 1 = 2, discarding 2 numbers at once; 2 numbers remain between lo and hi.
  6. mid = (2 + 3) / 2 = 2, rounded down, and nums[2] = 8: found at index 2 after 3 comparisons. Halving the range each time needs at most 4 looks for 9 numbers: O(log n), where a scan could need 9.

Binary Search study plan

14 of the 95 Binary Search problems (4 easy, 7 medium and 3 hard) over 8 days, about 8 h 10 min in all — the pattern first, then easiest to hardest. After that, the other 81 in the full list below are practice at your own pace. Then move on to Stack.

Day 1

Learn the pattern: read the essentials and step through the walkthrough above, then solve these 3.

Day 2

Medium problems: the same pattern with one twist each. Name the twist before you code.

Day 3

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 4

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 5

More mediums. Before coding each one, write down what state the pattern keeps and when it changes.

Day 6

Hard problems: the pattern combined with a second idea. Give each a full attempt before reading the editorial.

Day 7

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Day 8

Another hard one. If it beats you after a real attempt, read the editorial, then solve it again tomorrow from memory.

Next topic: Stack

Binary Search: the essentials

When to reach for it

Sorted input, or sorted with a twist (rotated, rising then falling); the first or last position of something; and the less obvious case — "the minimum capacity, speed or time such that …", where any guess above the answer also works. Large bounds (answers up to 10⁹) together with a cheap feasibility check are the giveaway.

The pattern

Search for a boundary, not a value: the first x where ok(x) is true, given that ok is false up to some point and true from then on. Keep the invariant that the answer lies in [lo, hi], and discard the half that cannot hold it. The template below assumes ok(hi) is true; start hi at a value known to work.

def first_true(lo, hi, ok):         # smallest x in [lo, hi] with ok(x)
    while lo < hi:
        mid = (lo + hi) // 2
        if ok(mid):
            hi = mid                # mid may be the answer
        else:
            lo = mid + 1            # mid is not
    return lo

Cost

O(log n) iterations. Searching an answer range of size R with an O(n) feasibility check costs O(n log R) — about 30 checks for R = 10⁹.

Common mistakes

  • mid = (lo + hi) / 2 overflowing when lo + hi passes 2³¹ − 1 in Java or C++; write lo + (hi - lo) / 2.
  • An endless loop: lo = mid with a mid rounded down never shrinks a two-element range, so that variant needs mid = (lo + hi + 1) / 2.
  • Mixing closed [lo, hi] and half-open [lo, hi) conventions inside one loop.
  • Binary searching a predicate that is not monotone, where halving throws the answer away.

Start with

All binary search problems

Easy (16)

Medium (58)

Hard (21)

Companies that ask binary search problems

  • Amazon 77 problems on binary search
  • Google 71 problems on binary search
  • Microsoft 15 problems on binary search
  • Meta 13 problems on binary search
  • Adobe 6 problems on binary search
  • Flipkart 6 problems on binary search
  • Apple 4 problems on binary search
  • Infosys 4 problems on binary search
  • TCS 3 problems on binary search
  • Uber 3 problems on binary search
  • Atlassian 2 problems on binary search
  • Bloomberg 2 problems on binary search

Next topic: Stack