← Questions

Frequent user events within a 24-hour window

0
AmazonCoding

A coding interview question asked at Amazon. It tests sliding window, hash map and sorting.

You are given a list of activity log entries. Each entry has a user_id (string), a timestamp (integer seconds since epoch) and an event_type (string). The list is not sorted.

Implement:

def frequent_events(logs: list[LogEntry], k: int) -> list[str]: ...

Return, sorted alphabetically, every event type that some single user performed at least k times within a 24-hour window. A group of events fits in a 24-hour window when the time between its first and last event is strictly less than 86,400 seconds.

  • Counts are per user. Two users doing the same event never add together.
  • Entries with the same timestamp are separate events.
  • 1 <= k, and there are up to 10^5 log entries.

Example 1 (k = 3, times in hours for readability)

user time event
ana 1h upload
ana 9h upload
ana 20h upload
ben 0h share
ben 12h share
cara 5h share

Output: ["upload"]. Ana uploads 3 times within 19 hours. share happens 3 times in total, but ben does it only twice.

Example 2 (k = 2): one user logs export at 0h and again at exactly 24h. Output: [], because events exactly 24 hours apart are not inside the same window.

Be ready to discuss a streaming version where entries arrive one at a time.

Clarifying questions

  • Is k = 1 simply every event type that appears?
  • Can timestamps be negative or out of order within a user (the list is unsorted: yes)?
  • Should the output list each event type once even if many users qualify?

What a strong answer covers

  • Groups timestamps by (user, event type), sorts each group: O(n log n) overall.
  • Slides over each sorted group checking t[i + k - 1] - t[i] < 86400; any hit adds the event type.
  • Gets the strict boundary right: two events exactly 86,400 seconds apart do not share a window (Example 2).
  • Counts per user only, keeps same-timestamp events separate, and returns the types sorted.
  • Tests the examples, k = 1, many identical timestamps, and a group that qualifies only in its middle.

Follow-ups

  • Streaming version: entries arrive one at a time in time order; keep a deque per (user, event) and evict entries older than 24 hours.
  • Report which users triggered each event type, and when.
  • Handle entries that arrive slightly out of order, or a dataset too large for one machine.

Where it's asked

What it tests

sliding window · hash map · sorting

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding