← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026

Summary

Google SWE interview focused almost entirely on word frequency problems, starting from a basic array version and escalating into stream processing and then a tricky non-repeating words variant. The problem kept mutating in ways I didn't fully anticipate.

Questions Asked (3)

Q1

Given an array of words and an integer k, return both the k most frequent and k least frequent distinct words. How do you handle ties?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The tie-breaking part is what tripped me up a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: define 'distinct words', confirm whether k can exceed the number of distinct words, and explicitly state your tie-breaking rule (e.g., lexicographical order). Then outline an efficient algorithm using a frequency map and a heap or sorting, and discuss time/space complexity and trade-offs.

Pro tip: Explicitly state your tie-breaking rule upfront and justify it; interviewers value candidates who proactively handle ambiguity and consider edge cases like k larger than the number of distinct words.

1. Clarify requirements and edge cases

Ask clarifying questions: Are words case-sensitive? What if k exceeds the number of distinct words? How should ties be broken? Confirm the expected output format.

2. Choose data structures and algorithm

Build a frequency map (hash map) of word counts. For top k frequent, use a min-heap of size k; for least k frequent, use a max-heap of size k. Alternatively, sort the distinct words by frequency and then by tie-breaker.

3. Define tie-breaking rule

Decide on a deterministic tie-breaker, such as lexicographical order. Apply it consistently when frequencies are equal, both in heap comparisons and final sorting.

4. Implement and test

Write clean code, handle edge cases (empty array, k=0, k > distinct count), and walk through a small example to verify correctness.

5. Analyze complexity and trade-offs

State time and space complexity (e.g., O(n + m log k) where m is distinct words). Discuss alternative approaches (e.g., full sort O(m log m)) and when each is preferable.

Key Points to Mention

  • Frequency map construction using a hash map for O(n) time.
  • Heap-based selection for O(m log k) time, where m is number of distinct words.
  • Tie-breaking strategy: lexicographical order for determinism.
  • Edge cases: k=0, k > distinct words, empty input, case sensitivity.
  • Space complexity: O(m) for frequency map and O(k) for heaps.
  • Trade-offs: heap vs. sorting; when k is small, heap is better; when k is large, sorting may be simpler.

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

Q2

Design a data structure for an unbounded word stream that supports ingesting words one at a time and querying the top-k or bottom-k most frequent words seen so far. What are the time and space tradeoffs?

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

This is where it got interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements: unbounded stream, top-k and bottom-k queries, and whether exact or approximate results are acceptable. Then propose a hash map for exact frequency counts combined with a min-heap for top-k and a max-heap for bottom-k, discussing the tradeoffs of each component and alternatives like count-min sketch for space efficiency.

Pro tip: Demonstrate awareness that in an unbounded stream, exact top-k with a heap requires O(k) space but O(log k) update time, while approximate methods like count-min sketch with a heap can reduce space at the cost of accuracy. Also mention that bottom-k can be derived from top-k by negating counts or using a separate structure.

1. Clarify Requirements and Constraints

Ask about the expected stream size, memory limits, required accuracy (exact vs approximate), and whether top-k and bottom-k need to be queried simultaneously or separately.

2. Propose a Baseline Exact Solution

Use a hash map to store word frequencies, and maintain a min-heap of size k for top-k and a max-heap of size k for bottom-k. Explain that updating the heap on each word insertion takes O(log k) time.

3. Analyze Time and Space Tradeoffs

Discuss that the hash map uses O(n) space for n distinct words, which is infeasible for unbounded streams. The heaps use O(k) space. Time per word is O(1) for hash map update plus O(log k) for heap update.

4. Introduce Approximate Solutions for Scalability

Mention count-min sketch or lossy counting to reduce space to sublinear, with probabilistic guarantees. Combine with a heap for top-k, but note that bottom-k may require a separate sketch or inverted counts.

5. Summarize and Recommend Based on Use Case

Conclude that for exact results with moderate distinct words, the hash map + heaps is fine; for truly unbounded streams, approximate methods are necessary. Highlight that bottom-k can be obtained by tracking least frequent items with a max-heap or by using a sketch that supports both.

Key Points to Mention

  • Hash map for exact frequency counts, but O(n) space where n is number of distinct words.
  • Min-heap of size k for top-k, max-heap of size k for bottom-k, each O(k) space and O(log k) update time.
  • Count-min sketch for approximate frequencies with sublinear space, but requires a heap for top-k and may not directly support bottom-k.
  • Tradeoff between exactness and space: exact requires O(n) space, approximate uses O(1/ε log(1/δ)) space.
  • Bottom-k can be derived by tracking least frequent items with a max-heap, or by negating counts and using top-k logic.
  • Consider using a balanced binary search tree or order-statistic tree for dynamic top-k queries if k is large, but heap is simpler.

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

Q3

Extend your stream design to also return the first k or last k words that are currently non-repeating, meaning words with a frequency of exactly 1, ordered by when they arrived in the stream.

Algorithms & Data StructuresSystem Design
Author's notes

Did not see this coming.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the stream processing model and constraints, then propose a data structure that maintains insertion order and supports O(1) frequency updates and O(k) retrieval of non-repeating words. Combine a doubly linked list of unique words with a hash map for frequencies and a hash map from word to list node, and handle edge cases like k larger than the number of unique words.

Pro tip: Emphasize that you would discuss trade-offs between time and space, and mention that for very large k or high-throughput streams, you might need to consider approximate or windowed solutions.

1. Clarify requirements and constraints

Ask about the definition of 'non-repeating' (frequency exactly 1), the expected size of k, whether the stream is infinite, and if updates are needed in real-time or batch.

2. Design core data structures

Propose a doubly linked list to maintain the order of unique words, a hash map for word frequencies, and a hash map from word to its node in the list for O(1) access.

3. Define update operations

For each incoming word, update its frequency; if it becomes 1, append to the list; if it becomes 2, remove from the list; if it was 2 and becomes 3, do nothing.

4. Implement retrieval of first/last k

Traverse the list from head or tail to collect up to k words, handling cases where fewer than k unique words exist.

5. Analyze complexity and edge cases

State that updates are O(1) and retrieval is O(k), and discuss edge cases like k=0, k > unique count, and memory constraints.

Key Points to Mention

  • Use of doubly linked list to maintain insertion order of unique words
  • Hash map for O(1) frequency updates
  • Hash map from word to list node for O(1) removal
  • Handling of frequency transitions (1->2, 2->3, etc.)
  • Time complexity: O(1) per update, O(k) per query
  • Space complexity: O(n) where n is number of distinct words

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