DSA

Binary Search Interview Questions

Explain Binary Search and when you should not use it — spoken sample answer for Indian interviews.

  • 5Questions with answers
  • 3Difficulty levels

Questions (5)

Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.

Question 1
Interview Beginner
Question

Explain Binary Search and when you should not use it.

Answer:

Find a target in a sorted array by repeatedly halving the search range.

Keep lo and hi pointers. Compute mid as lo + (hi - lo) / 2 to avoid overflow. Compare nums[mid] with target and shrink left or right half. Complexity: Time O(log n) , Space O(1) iterative

Sorted arrays, or binary search on the answer when a monotonic predicate exists (e.g. minimum ship capacity, first failing version).

First/last occurrence of duplicates requires two binary searches with different shrink directions.

Infinite loops from wrong lo/hi updates; off-by-one on first/last duplicate occurrence.

Question 2
Interview Intermediate
Question

How do you find the first and last occurrence of a number in a sorted array with duplicates?

Answer:

I run binary search twice. For the first occurrence, when nums[mid] equals target I still go left — hi equals mid minus one — and I record mid. For the last occurrence I go right. Each search is O(log n) . A single pass that returns any match is not enough. The off-by-one I watch is whether I use mid minus one or mid when I shrink. I test with [1, 2, 2, 2, 3] target 2. First index 1, last index 3. If they want count, last minus first plus one.

Question 3
Interview Advanced
Question

How do you search in a rotated sorted array?

Answer:

One half of [lo, hi] is always sorted. I compute mid. If nums[lo] is less than or equal to nums[mid], the left half is sorted. If the target sits in that range I search left, else right. The other branch mirrors it. Still O(log n) . I have to be careful when there are duplicates — [2, 2, 2, 3, 2] can make both halves look unsorted, and then I may have to shrink lo by one. I would mention duplicates as the hard follow-up so they know I have seen it.

Question 4
Interview Intermediate
Question

What is binary search on the answer? Give an example.

Answer:

Sometimes the array is not sorted, but the yes/no question is monotonic. I binary search the answer space. Example: Koko eating bananas — can she finish by hour H at speed k? If yes, try slower. If no, try faster. The predicate is monotonic so binary search works. Capacity to ship packages in D days is the same shape. Time is O(n log M) where M is the max answer. I would not call this 'binary search on an array'. I would call it binary search on a predicate.

Question 5
Interview Beginner
Question

Why do you compute mid as lo + (hi - lo) / 2 instead of (lo + hi) / 2?

Answer:

In languages with 32-bit ints, lo plus hi can overflow. lo plus (hi minus lo) divided by 2 stays in range. In JavaScript numbers are floats so it is less of a landmine, but I still write it that way because interviewers know the Java/C++ bug. I also say I prefer the iterative version in interviews so I do not blow a recursion discussion. Overflow plus infinite loops from wrong lo/hi updates are the two bugs I mention unprompted.

Practice with AI mock interviews

Run DSA mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.

Free to start · No credit card required