Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Loop by index; return `i` the first time `nums[i] == target`. If the loop finishes, return -1.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you handle the not-found case and know when structure beats a linear scan.
Where candidates slip
Returning the value, or omitting the -1 case.
How to stand out
Mention binary search (sorted) and hash lookup (repeated queries) as faster alternatives.
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
Sequential scan
1Hint 1Explore hintHide hint
No ordering is given — what's the only guaranteed way to find the value?
2Hint 2Explore hintHide hint
Return the index on the first match to get the first occurrence.
3Hint 3Explore hintHide hint
If the loop ends with no match, return -1.
A · Core idea
With no assumptions about order, the only way to find a value is to look at elements one at a time and return the index of the first match. Falling off the end means it's absent, so return -1.
Why return on first match. The problem asks for the first occurrence, so returning immediately gives the smallest index. Continuing would either waste work or (if you overwrite) return the last index instead.
Why -1 for 'not found'. A sentinel out of the valid index range (0..n−1) cleanly signals absence; the caller checks result == -1. Returning 0 or throwing would be ambiguous or heavy.
When you can do better. Linear search is the fallback for unstructured data. If the array were sorted, binary search drops this to O(log n); if you query repeatedly, a hash set/map gives O(1) lookups after O(n) setup. Naming those alternatives shows you reach for structure when it exists.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [4, 2, 7, 1], target = 7i=0
4 == 7? no
continue2C · Code with teaching comments
D · Why it works and complexityO(n) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
If the array were sorted you'd use binary search (O(log n)) — calling that out shows range awareness.
ComplexityO(n) time, O(1) space — in the worst case (absent or last) you scan all n elements; best case is the first element.
E · Edge cases and pitfalls5nums = [4, 2, 7], target = 9 — Absent → loop ends → -1.Explore edge casesHide edge cases
Edge cases handled
nums = [4, 2, 7], target = 9Absent → loop ends → -1.nums = [5], target = 5Found at index 0.nums = [], target = 1Empty → -1 immediately.
Pitfalls
- Returning the element value instead of its index.
- Forgetting the -1 'not found' return.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int search(int[] nums, int target) { for (int i = 0; i < nums.length; i++) { if (nums[i] == target) return i; } return -1;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Returning the element instead of its index.
- Forgetting the -1 'not found' case.
Revise
Carry the pattern into the interview
Pattern to remember
Sequential scan
Code pattern to memorize
for i,x in nums: if x==target return i; return -1Rules to remember
- 1Return index on first match.
- 2Return -1 if never found.
- 3Use binary search when sorted.
Use this pattern when
- Unsorted, one-off lookup
Choose another pattern when
- Sorted data → binary search
- Many lookups → hash set
Interview takeaway
Linear search is the fallback when data has no structure.
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: Sequential scan. Master it on small inputs and it transfers to harder problems.