First Non-Repeating Tag in a Live Stream of Log Events
Last updated: November 29, 2026
Quick Overview
Design a class that receives tags one at a time via add(tag) and can return, at any moment, the earliest tag that has appeared exactly once so far (or None).
0
0
0 solved
Design a class that receives tags one at a time via add(tag) and can return, at any moment, the earliest tag that has appeared exactly once so far (or None).
A Google phone screen question that starts as the familiar first unique character problem and immediately shifts to a streaming constraint. The interviewer is checking whether you pick a data structure that makes both operations O(1) instead of rescanning history on every query.
What the Interviewer Expects
- Recognizes that rescanning on each query is O(n) per call
- Combines a count map with an insertion-ordered structure
- Removes tags from the candidate set as soon as they repeat
- Gives O(1) amortized add and first
- Mentions memory growth for unbounded tag vocabularies
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
- What if you only care about the last W events (a sliding window)?
- How would you shard this across machines if tags arrive at millions per second?
- Return the first k unique tags instead of one.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
add(tag): record one occurrence.first(): return the earliest-added tag whose count is exactly 1, else None.- Both operations should be O(1) amortized; the stream is long and queries are freq...
High-Level Design
Keep two structures. A seen set records every tag ever observed. An OrderedDict named unique holds tags seen exactly once, in first-arrival order. On add: if the tag is new, put it in both; if i...