First Commenter Seen Only Once in a Live Comment Stream
Last updated: November 29, 2026
Quick Overview
Comments arrive as a stream of user ids. Support add(uid) and first_unique(), which returns the earliest-arriving user who has commented exactly once so far, or None.
ByteDance
November 29, 20260
0
0 solved
Comments arrive as a stream of user ids. Support add(uid) and first_unique(), which returns the earliest-arriving user who has commented exactly once so far, or None.
Douyin LIVE hosts sometimes reward the first genuinely new voice in chat rather than repeat commenters. The interviewer wants O(1) operations, which requires pairing a count map with an ordered structure and removing entries the moment a user repeats.
What the Interviewer Expects
- Track counts per user in a hash map
- Keep an insertion-ordered set of users seen exactly once
- Remove a user from the ordered set when their count reaches two
- Make both operations O(1)
- Discuss memory for long streams
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 bound memory over a stream of billions of ids?
- What if uniqueness should be judged only within the last 10 minutes?
- Implement it without OrderedDict using a doubly linked list and a map.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: add(uid) and first_unique(). IDs are hashable. first_unique returns None if no user has exactly one comment. Both should be O(1)....
High-Level Design
Maintain count[uid]. Also maintain once, an OrderedDict used as an insertion-ordered set of users whose count is exactly one. On a user's first comment, append them to once; on the second, del...