I started with just a hashmap for counts which was fine, but I fumbled on the 'most recent single-hit' part for a bit.
Clarify the requirements and constraints, then propose a data structure that supports O(1) record and O(1) query operations. Use a doubly linked list to maintain the order of unique customers and a hash map to track each customer's visit count and node reference, ensuring efficient updates and retrieval.
Pro tip: Discuss how your solution handles edge cases like multiple visits from the same customer and the removal of customers who are no longer unique. Also, mention how you would extend the design to support additional queries, such as the most recent customer with exactly K visits, to demonstrate scalability.
Ask about the expected scale, whether operations need to be thread-safe, and if the stream is unbounded. Confirm that 'most recently arrived' means the latest event in the stream.
Propose a doubly linked list to maintain the order of unique customers, with the most recent at the tail. Use a hash map to store each customer's visit count and a pointer to their node in the list (if they are unique).
For record(customerID): update the visit count. If count becomes 1, add a new node at the tail. If count becomes 2, remove the node from the list. For query(): return the tail node's customer ID if it exists, else null.
Explain that both operations run in O(1) time. Discuss edge cases: customer visits multiple times, no unique customers, and memory management for removed nodes.
Mention potential extensions like supporting K visits or handling concurrency. Discuss trade-offs between using a linked list versus other structures like a queue with lazy deletion.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.