Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Track `first = -1` and `last = -1`. On each match, set `first` only if it's still -1, and always update `last`. O(n). (Sorted → two binary searches, O(log n).)
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you capture first/last in one pass and handle the absent case.
Where candidates slip
Overwriting first, or wrong absent sentinel.
How to stand out
Give the O(log n) two-binary-search solution for sorted input (LC34).
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Single-pass first/last
1Hint 1Explore hintHide hint
Can one pass capture both the earliest and latest match?
2Hint 2Explore hintHide hint
Set first only once; always update last.
3Hint 3Explore hintHide hint
What do you return when the target is absent?
A · Core idea
Return the first and last indices of a target. A single pass suffices: record first only the first time you match, and overwrite last on every match.
Why guard `first` but not `last`. The earliest index should be set once and never changed (guard it with the first == -1 check). The latest index is simply whatever the final match was, so you update it unconditionally on each hit.
Why both default to -1. If the target never appears, both stay -1, which is the agreed 'absent' signal. Returning [0, 0] for an absent target is the classic bug.
The sorted upgrade. On a sorted array, the matches form a contiguous block, so two binary searches (leftmost and rightmost index) find both ends in O(log n) — the standard follow-up (LeetCode 34).
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 2, 2, 3, 2], target = 2i=1
match, first=-1→1
first=1,last=1[1, 4]C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
On a sorted array, two binary searches (leftmost and rightmost) give O(log n) — the classic follow-up.
ComplexityO(n) time, O(1) space — one linear pass with two index variables; O(log n) via two binary searches if sorted.
E · Edge cases and pitfalls4nums = [1, 2, 3], target = 5 — Absent → [-1, -1].Explore edge casesHide edge cases
Edge cases handled
nums = [1, 2, 3], target = 5Absent → [-1, -1].nums = [4], target = 4Single hit → [0, 0].
Pitfalls
- Overwriting `first` on every match (loses the earliest).
- Returning [0, 0] instead of [-1, -1] when absent.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int[] firstLast(int[] nums, int target) { int first = -1, last = -1; for (int i = 0; i < nums.length; i++) { if (nums[i] == target) { if (first == -1) first = i;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Overwriting `first` on every match (loses the earliest).
- Returning [0, 0] instead of [-1, -1] when absent.
Revise
Carry the pattern into the interview
Pattern to remember
Single-pass first/last
Code pattern to memorize
first=-1,last=-1; on match: if first<0 first=i; last=iRules to remember
- 1Guard first with -1.
- 2Always update last.
- 3Both -1 if absent.
Use this pattern when
- Range of a value's positions
Choose another pattern when
- Sorted — binary-search both ends
Interview takeaway
One scan can capture both the first and last occurrence.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Single-pass first/last. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Find First and Last Position (sorted)Coming soonTwo binary searches on a sorted array.
- Count Occurrenceslast − first + 1 gives the count on sorted data.