← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Google SWE coding round focused on a top-K frequency problem using log/stream data. The interviewer pushed pretty hard on knowing all three approaches and had real follow-ups about scale, so it wasn't just 'write the code and leave'.

Questions Asked (3)

Q1

Given a log or chat stream where each line associates a key (user ID, IP, or word) with an event, return the K keys with the highest occurrence counts.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew this problem family but fumbled when they asked me to walk through all three approaches back to back.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints: stream size, memory limits, and whether the stream is static or continuous. Then propose a two-phase approach: count occurrences using a hash map, then select the top K using a min-heap of size K for O(n log K) time. Discuss trade-offs between exact and approximate solutions (e.g., Count-Min Sketch) for large-scale streams.

Pro tip: Mention that for very large streams, a distributed approach like MapReduce can be used: map to (key, 1) pairs, reduce by key to sum counts, then select top K per partition and merge. This shows you think beyond a single machine.

1. Clarify Requirements and Constraints

Ask about stream size, memory limits, whether the stream is static or continuous, and if exact counts are required. This determines the appropriate algorithm and data structures.

2. Count Occurrences

Use a hash map to count occurrences of each key. For large streams that don't fit in memory, consider approximate counting techniques like Count-Min Sketch or distributed counting.

3. Select Top K

Use a min-heap of size K to efficiently find the K keys with highest counts. Iterate through the counts, maintaining the heap to keep the top K.

4. Analyze Complexity and Trade-offs

Discuss time and space complexity: O(n) for counting, O(n log K) for heap selection. Compare with alternatives like sorting all counts (O(n log n)) or using a max-heap (O(n + K log n)).

5. Handle Edge Cases and Scalability

Address edge cases: K larger than unique keys, ties, and memory constraints. For scalability, mention distributed processing (MapReduce) or streaming algorithms.

Key Points to Mention

  • Hash map for counting occurrences
  • Min-heap of size K for efficient top-K selection
  • Time complexity: O(n log K) vs O(n log n) for sorting
  • Space complexity: O(m) where m is number of unique keys
  • Approximate algorithms (Count-Min Sketch) for memory-constrained streams
  • Distributed processing (MapReduce) for large-scale data

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

Q2

How would you handle the top-K frequency problem if the log file is too large to fit in memory?

Algorithms & Data StructuresSystem Design
Author's notes

Went with external sort by key, linear scan to accumulate counts, then top-K on the result.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the constraints (file size, memory limit, K, and whether exact or approximate counts are acceptable). Then propose an external memory approach: stream the file in chunks, count frequencies per chunk, and merge counts using a hash-based partition or a min-heap for top-K. Finally, discuss trade-offs between exact and approximate methods, and how to handle skew.

Pro tip: Mention that you would first check if the file can be processed in a streaming fashion with a hash map if the number of distinct keys is small enough to fit in memory; if not, use external sorting or partitioning. This shows you optimize for the common case before jumping to complex solutions.

1. Clarify requirements and constraints

Ask about memory limit, file size, number of distinct keys, K, and whether exact counts are required. This determines the feasible approach.

2. Choose a strategy: in-memory vs. external

If distinct keys fit in memory, use a hash map to count frequencies in one pass, then a min-heap of size K to find top-K. Otherwise, use external memory techniques.

3. Design external memory algorithm

Partition the file into chunks that fit in memory, count frequencies per chunk, write intermediate counts to disk, then merge counts across chunks (e.g., using external sort or hash partitioning).

4. Extract top-K efficiently

After merging, use a min-heap of size K to track the top-K frequencies, or if data is sorted by key, do a second pass to aggregate and maintain top-K.

5. Discuss optimizations and trade-offs

Mention approximate algorithms (e.g., Count-Min Sketch, Lossy Counting) for memory-constrained scenarios, and how to handle skew (e.g., hot keys) with combiners or sampling.

Key Points to Mention

  • External sorting or hash partitioning to handle data larger than memory
  • Using a min-heap of size K to efficiently track top-K frequencies
  • Two-pass approach: first count frequencies, then find top-K
  • Approximate algorithms like Count-Min Sketch or Lossy Counting for memory efficiency
  • Handling data skew and hot keys with combiners or sampling
  • Trade-offs between exact and approximate results, and between time and memory

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

Q3

How would you scale the top-K frequency solution across a distributed system with multiple machines?

System DesignTechnical Trade-offs
Author's notes

This is where I actually felt okay.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (data size, distribution, latency, accuracy) and then outline a distributed architecture that partitions the data, computes local top-K on each machine, and merges results efficiently. Discuss trade-offs between exact and approximate solutions, and how to handle skewed data and fault tolerance.

Pro tip: Emphasize that the merge step is the bottleneck; using a hierarchical merge or a priority queue can reduce network overhead. Also, mention that for very large-scale systems, approximate algorithms like Count-Min Sketch with a heap are often preferred over exact counting.

1. Clarify Requirements and Constraints

Ask about data volume, velocity, distribution, required accuracy, latency, and whether the system is batch or streaming. This determines the choice of exact vs. approximate algorithms.

2. Data Partitioning Strategy

Decide how to partition the data across machines. Common approaches: hash partitioning by key to ensure all occurrences of a key go to the same machine, or range partitioning. Discuss the impact on load balancing and skew.

3. Local Top-K Computation

Each machine computes its local top-K frequencies using a hash map and a min-heap (or similar). This reduces the data that needs to be sent to the reducer.

4. Global Merge and Aggregation

Collect local top-K results from all machines and merge them to compute the global top-K. Use a priority queue or sort to efficiently find the top-K among the candidates. Consider hierarchical merging to reduce network traffic.

5. Handle Skew, Fault Tolerance, and Scalability

Address data skew (e.g., hot keys) by using techniques like salting or two-phase aggregation. Discuss fault tolerance (replication, checkpointing) and how the system scales with more machines.

Key Points to Mention

  • Partitioning strategies (hash, range) and their impact on load balancing and skew
  • Local aggregation using hash maps and heaps to reduce data shuffling
  • Merge strategies: single reducer vs. hierarchical merge, and use of priority queues
  • Trade-offs between exact and approximate algorithms (e.g., Count-Min Sketch) for scalability
  • Handling hot keys via salting or two-phase aggregation
  • Fault tolerance and scalability considerations (replication, checkpointing, dynamic scaling)

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