TRT
Sign in
mediumCorecoding

Find the first and last position of a value in a sorted array

Solve it with two boundary searches in O(log n), and explain the invariant that makes each correct.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstLower and upper boundsPro — members onlyInstead of asking whether a value is present, ask where the first element that is at least x sits. That one question finds first and last occurrences, counts duplicates and gives insertion points, with no special cases.

A strong answer

A strong candidate frames Find the first and last position of a value in a sorted array around the exact memory layout, compiler semantics, and hardware execution model covered in Lower and upper bounds. Solve it with two boundary searches in O(log n), and explain the invariant that makes each correct.

"Is x in the array, and where?" is an awkward question for binary search. When x appears several times, there are several right answers, and which one a search finds depends on the order of its comparisons. When x is missing, the answer is "nowhere", which throws away useful information.

A more precise question has exactly one answer every time: what is the first index whose value is at least x? That index is called the lower bound of x. If x is present, it is the first occurrence. If x is absent, it is where x would be inserted. If x is larger than everything, it is len(a), one past the end.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Implement lower bound as a search for the first index where a condition becomes true.
  • Mastery of core invariant: Find the first and last occurrence of a value, and count occurrences, with lower and upper bounds.
  • Mastery of core invariant: Use insertion points to keep a sorted list sorted.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How do you count occurrences of x in O(log n)?
  2. 02How would you find the last element strictly less than x?
  3. 03Why doesn't finding any match and expanding outward meet the time limit?
  4. 04How do Python's bisect_left and bisect_right relate to your functions?

Not started

Sign in to save your learning progress.

Sign in to save