← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google SWE coding round, one question about finding top-k users from chat logs. Pretty standard frequency counting problem but they wanted test cases too, which I almost forgot about.

Questions Asked (1)

Q1

Given a list of chat log entries in the format 'user_id message', find the top k users who sent the most messages. Return their user IDs. Logs and users can be up to 10^5 entries. Also provide test cases.

Algorithms & Data Structures
Author's notes

The core logic wasn't bad.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the input format and constraints, then propose a two-pass solution: first count messages per user using a hash map, then select the top k users using a min-heap of size k for O(n log k) time. Discuss edge cases like ties, fewer than k users, and large input handling.

Pro tip: Mention that a min-heap is optimal for top-k when k is small, but if k is close to n, sorting all users might be simpler; also discuss how to handle ties consistently (e.g., by user_id) and the importance of defining tie-breaking rules upfront.

1. Clarify requirements and constraints

Ask about input format, size limits, definition of 'top' (ties), and expected output order. Confirm that user IDs are comparable and that k is valid.

2. Design the counting phase

Use a hash map to count messages per user by iterating through the log entries once. This handles up to 10^5 entries efficiently.

3. Select top k users

Use a min-heap of size k to track the k users with the highest counts. For each user, push (count, user_id) and pop the smallest if size exceeds k. Alternatively, sort all users by count descending.

4. Handle edge cases and ties

If there are fewer than k users, return all. For ties, define a consistent rule (e.g., smaller user_id first) and apply it in the heap comparator or sorting.

5. Provide test cases

Include tests for normal case, ties, k=0, k > number of users, empty input, and large input to verify performance.

Key Points to Mention

  • Time and space complexity: O(n + m log k) time, O(m) space, where n is number of logs and m is number of unique users.
  • Choice of data structures: hash map for counting, min-heap for top-k selection.
  • Tie-breaking strategy: specify how to order users with equal message counts (e.g., by user_id ascending).
  • Edge cases: empty logs, k=0, k greater than unique users, duplicate user IDs, and large input size.
  • Test cases: provide concrete examples with expected outputs, covering normal, boundary, and performance scenarios.
  • Scalability: discuss how the solution handles 10^5 entries and whether streaming or distributed approaches are needed.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.