Most Active Users in a Live Communication Stream
GoogleSoftware Engineer · OnsiteLast updated
This problem tests your ability to efficiently manage and rank real-time event counts using hash maps and balanced data structures or heaps, focusing on deduplication and stream processing.
Problem Statement
You are building an analytics service for a live communication platform. You are given a stream of events, where each event is a tuple (timestamp, user_id, message_id).
Your task is to implement a class ActivityTracker that processes these events and identifies the 'Most Active Users'. An active user is defined by the number of unique message_ids they have sent. If a message_id appears multiple times for the same user_id, it should only be counted once.
Implement the following methods:
record_event(timestamp, user_id, message_id): Registers a message for a user.get_top_k(k): Returns a list of the topkuser_ids based on their count of uniquemessage_ids, ordered by count descending. If two users have the same count, break ties by returning the smalleruser_idfirst.
Constraints
user_idis an integer.message_idis an integer.- The system must be able to handle millions of events.
kwill be a small positive integer.
Example
tracker = ActivityTracker()
tracker.record_event(1, 101, 1)
tracker.record_event(2, 101, 2)
tracker.record_event(3, 102, 1)
tracker.record_event(4, 101, 1) # Duplicate message_id, ignored
# User 101: 2 unique messages, User 102: 1 unique message
print(tracker.get_top_k(1)) # Output: [101]
Hints
Hint 1
Use a nested hash map or a dictionary of sets to store unique message IDs per user to ensure deduplication.
Hint 2
Maintain a separate data structure that maps counts to sets of users to allow efficient retrieval of top users without full re-sorting.
Hint 3
Consider using a min-heap to keep track of the top K users if the frequency of get_top_k calls is low compared to record_event calls.
Follow-up questions
- How would your solution change if you needed to track activity within a sliding time window (e.g., the last 10 minutes)?
- If the system requires massive horizontal scaling, how would you partition the data across multiple servers?
- How would you handle the case where the number of unique users is extremely large, making it impossible to store all user data in a single machine's RAM?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.