Clarify requirements first, then propose a two-part solution: a hash map to track per-user login counts and earliest timestamps, and a min-heap (priority queue) keyed by timestamp to efficiently retrieve the user with exactly one login and earliest time. Discuss lazy deletion to handle users who become ineligible after additional logins, and analyze time/space complexity.
Pro tip: Explicitly discuss how to handle duplicate logins and stale heap entries with lazy deletion, and mention that the heap may grow with each event but queries remain O(1) amortized after cleanup—this shows you understand real-world trade-offs.
Ask about stream volume, memory limits, whether timestamps are unique, and if the query must be exact or approximate. Confirm that 'exactly once' means the user has only one login event in the entire stream.
Use a hash map to store per-user login count and earliest timestamp (or list of timestamps). Use a min-heap keyed by timestamp to track candidates who currently have exactly one login.
When a new login arrives, update the user's count. If count becomes 1, push (timestamp, user) to heap; if count becomes 2, mark user as ineligible (lazy deletion). For queries, pop heap entries where user's count != 1.
Add operation: O(log n) for heap push, O(1) for map update. Query: O(k log n) worst-case for lazy deletions, but amortized O(1) if each entry removed once. Space: O(n) for map and heap.
Consider using a balanced BST or sorted set instead of heap for O(log n) query. Handle empty heap, users with multiple logins, and timestamp ties. Mention potential memory growth and periodic cleanup.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.