Frequent user events within a 24-hour window

0
Sign in

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.

Reconstructed from the reported question; details may differ from what interviewers ask.

AmazonCodingMedium

Sign in to see the worked answer

The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.