← Questions

Most frequent user in access logs

0
AmazonCodingLast asked Jul 2025

A coding interview question asked in Amazon's coding round, last asked Jul 2025. It tests hashing and parsing.

You get a list of access-log lines. Each line holds a username, then whitespace, then an access timestamp, for example "alice 2025-07-27T12:30:00Z". Return the username that appears in the most lines. When several users tie for the most, any one of them is acceptable. Then discuss the cost of your solution and what changes when the log does not fit in memory.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • most_frequent_user(logs: list[str]) -> str — the username is the text before the first whitespace; usernames are case-sensitive.

Example

logs = [
  "alice 2025-07-27T12:30:00Z",
  "bob 2025-07-27T12:31:00Z",
  "alice 2025-07-27T12:35:10Z",
  "carol 2025-07-27T12:40:00Z",
  "Alice 2025-07-27T12:41:00Z",
]
->  "alice"    # 2 lines; "Alice" is a different user

Constraints

  • 1 <= len(logs) <= 200,000
  • Every line has at least two whitespace-separated tokens; the username has no whitespace.
  • The timestamp's format does not matter here.

Clarifying questions

  • Are usernames case-sensitive?
  • On a tie, does any answer do, or is there a rule (first seen, alphabetical)?
  • Can lines be malformed (empty, no timestamp, extra spaces)? Skip them or fail?
  • Does the timestamp matter, for example only accesses in a time range?

What a strong answer covers

  • Split on the first whitespace and count in a hash map: one pass, O(N) time, O(U) space for U distinct users.
  • Tracking the leader while counting, so no second pass is needed.
  • Robust parsing: leading or repeated spaces, tabs, lines to skip.
  • Tests: one line, every line the same user, a tie.

Follow-ups

  • The log is far larger than memory. Stream it, and partition by a hash of the username or sort externally.
  • The log is spread over many machines. Combine partial counts with a map-reduce step.
  • Return the top k users, or the most frequent user in each hour.
  • Approximate the answer in small memory (count-min sketch, a heavy-hitters algorithm) and state the error.

Where it's asked

  • Amazon · Coding · Last asked Jul 202555 min · Usually two rounds; one is often an object-oriented design built in steps · No AI

What it tests

hashing · parsing

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