Top Event Users

Start Timer

0:00:00

Upvote
10
Downvote
Save question
View comments

Let’s say at TikTok we have a list of valid Kafka events. Write a function top_event_users that returns the three users with the most events, from most to fewest.

Each event is a dict with event_id, timestamp, user_id and payload. Return a list of user_id values.

Rules - Count every record in the list, even if an event_id repeats. - Break ties by returning the smaller user_id first. - Return all users if there are fewer than three.

Example:

Input:

events = [
    {'event_id': 1, 'timestamp': '2026-02-02T10:00:00', 'user_id': 7, 'payload': {}},
    {'event_id': 2, 'timestamp': '2026-02-02T10:05:00', 'user_id': 7, 'payload': {}},
    {'event_id': 3, 'timestamp': '2026-02-02T10:10:00', 'user_id': 3, 'payload': {}},
    {'event_id': 4, 'timestamp': '2026-02-02T10:12:00', 'user_id': 9, 'payload': {}},
    {'event_id': 5, 'timestamp': '2026-02-02T10:15:00', 'user_id': 9, 'payload': {}},
    {'event_id': 6, 'timestamp': '2026-02-02T10:20:00', 'user_id': 3, 'payload': {}},
    {'event_id': 7, 'timestamp': '2026-02-02T10:25:00', 'user_id': 7, 'payload': {}},
]

Output:

top_event_users(events) -> [7, 3, 9]
.
.
.
.
.


Comments

Loading comments