Quick Overview

This question evaluates frequency-counting and log-parsing skills along with algorithmic reasoning about time and space complexity for identifying the most frequent element in a dataset.

Find most frequent log user

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

##### Question You are given a list of log entries in string format, where each entry contains a username and an access timestamp separated by a delimiter (e.g., "alice 2025-07-27T12:30:00Z"). Design an algorithm that processes the logs and returns the username that appears most frequently (i.e., the user with the highest number of accesses). If multiple users tie for the highest frequency, you may return any one of them. Discuss the time- and space-complexity of your solution.

Overview: This question evaluates frequency-counting and log-parsing skills along with algorithmic reasoning about time and space complexity for identifying the most frequent element in a dataset.

You are given a list of log entry strings. Each entry is formatted as 'username timestamp' where username is the substring before the first whitespace and timestamp follows after at least one space (e.g., 'alice 2025-07-27T12:30:00Z'). Return the username that appears most frequently across all entries. If multiple users tie for the highest frequency, return any one of them. Usernames are case-sensitive. The input list is non-empty and every entry contains at least two whitespace-separated tokens.

Constraints

  • 1 <= len(logs) <= 200000
  • Each entry contains at least two tokens separated by whitespace
  • Username has no whitespace
  • Timestamps may be any strings without whitespace; their format is irrelevant
  • Usernames are case-sensitive
  • Return any one username if multiple have the same maximum frequency

Examples

Input:

Expected Output: alice

Input:

Expected Output: charlie

Hints

  1. Split each log entry by whitespace and use the first token as the username.
  2. Use a hash map (dictionary) to count the frequency of each username.
  3. After counting, select the username with the maximum count.

Most Frequent User in Logs

mediumAmazonSoftware EngineerHash Table

You are given a list of log entry strings. Each entry is formatted as 'username timestamp' where username is the substring before the first whitespace and timestamp follows after at least one space (e.g., 'alice 2025-07-27T12:30:00Z'). Return the username that appears most frequently across all entries. If multiple users tie for the highest frequency, return any one of them. Usernames are case-sensitive. The input list is non-empty and every entry contains at least two whitespace-separated tokens.

Examples

Example 1
Input
logs = ["alice 2025-07-27T12:30:00Z", "bob 2025-07-27T13:00:00Z", "alice 2025-07-27T14:10:00Z"]
Output
alice
Notes
alice appears 2 times, bob appears 1 time.
Example 2
Input
logs = ["alice 2025-01-01T00:00:00Z", "bob 2025-01-01T01:00:00Z", "bob 2025-01-01T02:00:00Z", "alice 2025-01-01T03:00:00Z"]
Output
alice
Notes
alice and bob both appear 2 times; returning any one of them is acceptable.

Constraints

  • •1 <= len(logs) <= 200000
  • •Each entry contains at least two tokens separated by whitespace
  • •Username has no whitespace
  • •Timestamps may be any strings without whitespace; their format is irrelevant
  • •Usernames are case-sensitive
  • •Return any one username if multiple have the same maximum frequency

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Sample input
["alice 2025-07-27T12:30:00Z","bob 2025-07-27T13:00:00Z","alice 2025-07-27T14:10:00Z"]
Language
Case 1
Input
[
  "alice 2025-07-27T12:30:00Z",
  "bob 2025-07-27T13:00:00Z",
  "alice 2025-07-27T14:10:00Z"
]
Expected
alice
Case 2
Input
[
  "charlie 2024-01-01T00:00:00Z",
  "dave 2024-01-01T01:00:00Z",
  "charlie 2024-01-01T02:00:00Z",
  "charlie 2024-01-01T03:00:00Z"
]
Expected
charlie

Browse More Questions

Show the approach

Approach

Count occurrences of each username using a dictionary where keys are usernames and values are counts. Extract the username as the first whitespace-separated token from each log entry. Finally, return the username with the maximum count; in case of ties, any one of the tied usernames may be returned.

Time complexity:
O(n)
Space complexity:
O(k)