Katkat

Most Active Users in a Live Communication Stream

GoogleSoftware Engineer · Onsite

Last 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:

  1. record_event(timestamp, user_id, message_id): Registers a message for a user.
  2. get_top_k(k): Returns a list of the top k user_ids based on their count of unique message_ids, ordered by count descending. If two users have the same count, break ties by returning the smaller user_id first.

Constraints

  • user_id is an integer.
  • message_id is an integer.
  • The system must be able to handle millions of events.
  • k will 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?