Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

Data Structures & Algorithms (DSA) Module 6

Binary search and searching interview questions

Interview questions on binary search and searching: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

8 questions: 2 warm-ups and 6 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    bisect_left returns 4 for a value that is not in a sorted list. What does that 4 tell you, and what can you do with it?

    Answered in the lesson Lower bound, upper bound and first-true search

  2. Fresher to mid

    In a rotated sorted array of distinct values, how do a[lo], a[mid] and a[hi] tell you which half is sorted?

    Answered in the lesson Binary search on rotated, peaked and 2-D data

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    You search 2,000 commits for the first one that broke a test. Broken commits always fail, but the test also fails on 5 % of runs of good commits, at random. What does a plain first-true search return, and how do you make the answer reliable at a known cost?

    What it tests
    Seeing that a flaky test breaks the monotone assumption, estimating the error of the search, and choosing a retry rule that bounds it with numbers.
    First step
    Count the checks first: about ⌈log₂ 2,001⌉ = 11 builds, roughly half of them on good commits. Each good commit looks broken with probability 0.05, and one false failure sends the search to an earlier commit. Work out the chance that none of those good runs fails.
    Follow-ups

    From the lesson Lower bound, upper bound and first-true search

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  2. Fresher to mid

    A sorted list holds a day's 10⁵ train departure times in minutes after midnight. For 10⁶ queries, return the first departure at or after time t and the last one strictly before t. Write the searches, handle times before the first and after the last train, and give the total cost.

    What it tests
    Turning "at or after" and "strictly before" into the right bound, handling both ends of the list, and estimating the cost of a million queries.
    First step
    Translate the words into bounds: "at or after t" is the lower bound of t, the first time that is at least t, and "strictly before t" is the position just before it. Then list the two edge cases: the lower bound equals the length of the list, or it is 0.
    Follow-ups

    From the lesson Lower bound, upper bound and first-true search

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A ring buffer keeps 10⁶ events in time order from a moving start, and its timestamps have one-second resolution, so long runs of equal times are common. Your rotated search for a timestamp sometimes reads every entry. Explain why, show that no algorithm can always avoid it, and say what you would change.

    What it tests
    Seeing why ties break the sorted-half test, proving a linear lower bound with an adversary, and fixing the data or the access instead of the search.
    First step
    Find the comparison that stops working: with a[lo] == a[mid] == a[hi], neither half is known to be sorted. Then build a family of valid inputs, a long run of one time with a single different time placed anywhere, and ask what an algorithm learns from each read.
    Follow-ups

    From the lesson Binary search on rotated, peaked and 2-D data

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  4. Fresher to mid

    Hourly prices of a product over 10⁵ hours are in an array. A teammate reports the highest price with the uphill binary search for a peak. When is the answer right, when is it wrong, and what does each correct approach cost?

    What it tests
    Telling a peak from the maximum, knowing which shapes of data guarantee a single peak, and costing a scan against a search.
    First step
    Write down what the uphill rule guarantees: a position at least as large as its neighbours, nothing more. Then draw a price series with two hills of different heights and trace the search: which hill does it climb, and why?
    Follow-ups

    From the lesson Binary search on rotated, peaked and 2-D data

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Mid to senior

    A table of two-dimensional prefix sums of non-negative counts has 50 rows and 2 × 10⁶ columns, so it is sorted along every row and every column. Compare the staircase, a binary search in every row, and copying the table into one sorted array for 10⁴ queries "is p in the table?", by reads and memory, and choose.

    What it tests
    Costing three matrix searches from the shape, seeing that the staircase is linear in the long side, and weighing a one-off copy and sort against per-query work.
    First step
    Write each cost from the shape before choosing: the staircase reads up to r + c − 1 cells; a binary search in each of the r rows reads about r × (⌊log₂ c⌋ + 2); one sorted array of all r × c values must be built and sorted first. Put numbers on all three.
    Follow-ups

    From the lesson Binary search on rotated, peaked and 2-D data

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All Data Structures & Algorithms (DSA) interview questions · The Data Structures & Algorithms (DSA) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.