Find the First Unique Product Code in an Order Stream
Last updated: November 29, 2026
Quick Overview
Given a list of product codes in the order they were scanned, return the first code that appears exactly once, or None if every code repeats. Then extend it to support codes arriving one at a time with a query for the current first unique code.
Amazon
November 29, 20260
0
0 solved
Given a list of product codes in the order they were scanned, return the first code that appears exactly once, or None if every code repeats. Then extend it to support codes arriving one at a time with a query for the current first unique code.
A common warm-up in Amazon phone screens, framed around scanner events at a fulfillment center. The interviewer checks that you reach for a hash map instead of nested loops and that you can evolve a batch solution into a streaming one without losing the O(1) query.
What the Interviewer Expects
- Clarify whether codes are case sensitive and what to return when none are unique
- Give the two-pass hash map solution first
- Explain why a nested loop is O(n^2) and too slow for millions of scans
- Extend to a streaming version with an ordered structure
- Test with empty input and all-duplicate input
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support removal of a code when a scan is voided?
- What if the stream is too large to fit counts in memory on one host?
- Can you make the streaming query O(1) worst case rather than amortized?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input is a list of strings, possibly empty. Return the earliest code whose total count is 1. Codes are compared exactly (case sensitive). For the streaming version, add(code) and first_unique() sh...
High-Level Design
Batch: count every code with a dictionary, then walk the list again in order and return the first code whose count is 1. Two linear passes.
Stream: keep a count map plus an insertion-ordered dictiona...