First Unique Character in a Stream
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reportedly served this one in September 2026, and it's a string problem wearing a stream costume. After every character arrives, you output the earliest character that has appeared exactly once so far, or # if none. The input is up to 100000 characters, so rescanning the prefix each time is a trap. It reduces to a frequency count plus a queue of candidates. If your head goes blank mid-OA, StealthCoder is the invisible safety net that reads the problem and hands you the solution while the proctor sees nothing.
The problem
Characters arrive in the order of the string stream. After each arrival, append the earliest character seen so far whose frequency in the processed prefix is exactly one. If the current prefix has no unique character, append #. Return the concatenation of all per-arrival answers. Comparisons are case-sensitive. Sibling variants: same core, different skin These four problems are sibling variants of the same first-unique pattern. Their story wrappers and query timing differ, while the core rule keeps the earliest value whose frequency is exactly one. Longest-Waiting Unique Task Dynamic Longest-Waiting Unique Task Queries First User to Log In Exactly Once First Unique Character in a Stream (current) Function firstUniqueAfterEach(stream: String) → String Examples Example 1 stream = "aabc" return = "a#bb" The first a is unique, then none is unique, and b remains first after c arrives. Example 2 stream = "zz" return = "z#" The only character stops being unique after its second occurrence. Example 3 stream = "abc" return = "aaa" The earliest a remains unique throughout all three prefixes. Constraints 1 ≤ stream.length ≤ 100000. stream contains only ASCII letters and digits. The sentinel # does not occur in stream.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: keep a count array (62 possible ASCII letters and digits) and a queue of characters in arrival order. On each arrival, increment the count and push the character. Then pop from the front while the front's count is greater than one. The front that remains is your answer, or # if the queue is empty. Each character enters and leaves the queue once, so the whole thing is O(n). The common pitfall is rescanning the prefix per step, which goes O(n^2) and dies at 100000. Another miss is forgetting case sensitivity, so 'a' and 'A' are different. Also don't remove a character from the queue the moment it repeats unless it's at the front. Lazy deletion is simpler. Check your output against "aabc" giving "a#bb". If you freeze on the lazy-pop idea during the live OA, StealthCoder is the hedge that gives you the working code.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill First Unique Character in a Stream cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as first unique character in a string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
First Unique Character in a Stream FAQ
What's the trick to First Unique Character in a Stream?+
Track frequencies and keep a queue of characters in arrival order. After each new character, pop the front while its count exceeds one. Whatever sits at the front is the first unique, or # when the queue is empty. Every character is pushed and popped at most once, so it's linear.
How hard is this Amazon OA question really?+
Easy to medium. The idea is short once you see the queue plus counts. The difficulty is spotting that per-prefix rescans time out at 100000 characters. If you've seen first unique character in a string, this is the same core with a running answer.
Why not just rescan the prefix each time?+
That's O(n^2). With a stream up to 100000 characters, that's billions of operations in the worst case. The queue with lazy popping amortizes everything to O(n), because each character is added once and removed at most once.
Do I need a hash map or is an array fine?+
Either works. The input is only ASCII letters and digits, so a fixed array of 128 counts is simple and fast. A hash map is fine too. Just remember the comparison is case-sensitive, so 'a' and 'A' count separately.
How do I prepare for this in 48 hours?+
Write it once from scratch with a queue and a count array. Test on "aabc" (a#bb), "zz" (z#), and "abc" (aaa). Then do the sibling variants, like first user to log in exactly once, since they share the same core rule. Skip the rest of the topic list.