interview-tips
search-algorithms
problem-solving
technical-interviews
coding-interview

Tricky Interview question on searching

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Interview questions about searching are rarely about memorizing linear search or binary search. The real test is whether you can identify what property makes search possible, state the invariant clearly, and handle the edge cases without guessing.

The hidden trick in most search questions

Many "tricky" questions are really asking whether you can search over a monotonic condition. Instead of asking "where is the value," ask "for which positions does a condition become true?"

That reframing turns many problems into the same pattern:

  • find the first index where a predicate becomes true
  • find the last index where a predicate stays true
  • verify the answer after the loop

This is why strong candidates talk about invariants before they talk about code.

Classic example: first occurrence in a sorted array

Suppose an interviewer asks for the first occurrence of 7 in a sorted array that may contain duplicates. A standard binary search is not enough, because finding any 7 does not prove it is the first one.

A clean Python solution looks like this:

python
1def first_occurrence(nums, target):
2    left = 0
3    right = len(nums)
4
5    while left < right:
6        mid = (left + right) // 2
7        if nums[mid] >= target:
8            right = mid
9        else:
10            left = mid + 1
11
12    if left < len(nums) and nums[left] == target:
13        return left
14    return -1
15
16print(first_occurrence([1, 3, 7, 7, 7, 9], 7))

Notice the invariant: the first valid answer is always in the half-open interval from left to right.

Searching on an answer, not just in an array

A harder interview twist is binary search on a numeric answer space. For example: "What is the smallest processing speed that finishes all jobs within h hours?"

You are no longer searching a sorted list. You are searching a range of possible answers where the predicate "this speed is sufficient" is monotonic.

python
1def min_speed(jobs, hours):
2    def can_finish(speed):
3        total = 0
4        for job in jobs:
5            total += (job + speed - 1) // speed
6        return total <= hours
7
8    left = 1
9    right = max(jobs)
10
11    while left < right:
12        mid = (left + right) // 2
13        if can_finish(mid):
14            right = mid
15        else:
16            left = mid + 1
17
18    return left
19
20print(min_speed([3, 6, 7, 11], 8))

This is still binary search, but the searchable structure is the predicate, not an array index.

What interviewers are actually watching

During a search problem, interviewers usually care about these points:

  • did you ask whether the data is sorted or monotonic
  • did you define the loop invariant
  • did you choose inclusive or half-open bounds consistently
  • did you prove termination
  • did you validate the final candidate

If you can explain those points, even a small syntax slip is usually recoverable. If you cannot explain them, a correct-looking solution often collapses under follow-up questions.

A strong way to talk through the problem

When solving at the board or in a shared editor, narrate the structure:

  1. State what property makes binary search legal.
  2. Define what left and right mean.
  3. Explain which side is discarded after each comparison.
  4. Describe what must be checked after the loop.

That style shows control. It also prevents the common mistake of writing code first and reasoning later.

Common Pitfalls

The most common mistake is applying binary search to data that is not actually sorted or monotonic. If that prerequisite is missing, the entire method is invalid.

Another bug is mixing boundary conventions. Using inclusive bounds at the start and half-open bounds in the update logic produces off-by-one errors that are hard to spot.

People also forget to check the final index. The loop may end at the insertion position rather than at a guaranteed match.

Finally, do not treat every search question as array lookup. Many of the harder interview problems are really "search the answer space" problems in disguise.

Summary

  • The hardest part of a search interview problem is usually identifying the searchable monotonic property.
  • Binary search variants are best expressed with clear invariants and consistent bounds.
  • First-occurrence and lower-bound problems are standard interview patterns.
  • Many advanced questions search an answer space rather than an array.
  • Explaining why the algorithm is valid matters as much as writing the code.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.