Google Interview experience
1 round over 8 weeks. Accepted offer, good experience.
The interviewer pasted the question in the document, and I asked for some clarifications. They then asked about my previous projects listed on my resume. The questions were fairly standard. At the end, I also had a few questions of my own. Was asked to implement the problem below, which is a variation of the Top K problem.
Question:
Given a continuous stream of integers and a fixed integer n, which remains constant throughout the stream, output the top n most frequent elements from the stream. When a query with the integer 0 is received, output the top n most frequent elements observed so far.
Example:
Input Stream: [3, 1, 2, 3, 1, 2, 3, 4, 2, 0, 5, 6, 2, 0]
Value of n: 3
Output:
After processing up to the first 0 query: [3, 2, 1] (at this point, the element 3 has appeared the most, followed by 2 and 1.)
After processing up to the second 0 query: [2, 3, 4] (by this time, the element 2 has become the most frequent, followed by 3 and 4, as 4 has now appeared frequently enough to be in the top 3.)
Brute Force Solution:
Suggested that we can use a hashmap to store the frequencies of each element as we process the stream. For each new input or query, we would sort the elements by their frequency and then retrieve the top k elements.
Optimal Solution:
Use a hashmap to track the frequency of each element. Maintain a min-heap of size n to keep track of the top n elements based on their frequency. When a new element arrives, update its frequency in the hashmap and adjust the heap to ensure it contains the most frequent elements. When a query with 0 is received, simply retrieve the elements from the heap.
import java.util.*;
class MinHeap {
private final int size;
private final PriorityQueue<HeapNode> heap;
private final Map<Integer, HeapNode> entryFinder;
public MinHeap(int size) {
this.size = size;
this.heap = new PriorityQueue<>(Comparator.comparingInt(a -> a.frequency));
this.entryFinder = new HashMap<>();
}
public void push(int item, int frequency) {
if (entryFinder.containsKey(item)) {
// Remove the old entry
remove(item);
}
HeapNode entry = new HeapNode(frequency, item);
entryFinder.put(item, entry);
if (heap.size() < size) {
heap.add(entry);
} else if (frequency > heap.peek().frequency) {
heap.poll();
heap.add(entry);
}
}
public void remove(int item) {
HeapNode entry = entryFinder.remove(item);
if (entry != null) {
entry.frequency = Integer.MIN_VALUE; // Mark as removed
}
}
public List<Integer> getTopElements() {
List<Integer> topElements = new ArrayList<>();
for (HeapNode node : heap) {
if (node.frequency != Integer.MIN_VALUE) {
topElements.add(node.item);
}
}
return topElements;
}
private static class HeapNode {
int frequency;
int item;
HeapNode(int frequency, int item) {
this.frequency = frequency;
this.item = item;
}
}
}
class TopKFrequent {
private final int k;
private final Map<Integer, Integer> frequencyMap;
private final MinHeap minHeap;
public TopKFrequent(int k) {
this.k = k;
this.frequencyMap = new HashMap<>();
this.minHeap = new MinHeap(k);
}
public void add(int element) {
frequencyMap.put(element, frequencyMap.getOrDefault(element, 0) + 1);
minHeap.push(element, frequencyMap.get(element));
}
public List<Integer> getTopK() {
return minHeap.getTopElements();
}
public static void main(String[] args) {
TopKFrequent topKFrequent = new TopKFrequent(3);
int[] stream = {3, 1, 2, 3, 1, 2, 3, 4, 2};
for (int num : stream) {
topKFrequent.add(num);
}
// Query for top k elements
System.out.println(topKFrequent.getTopK()); // Output: [3, 2, 1]
}
}Time Complexity:
push(int item, int frequency): O(log k)
remove(int item): O(1)
getTopElements(): O(k)
Processing stream of N elements: O(N * log k)
Know someone interviewing at Google? Pass it on.
Same company, different candidates.
Share yours while it’s fresh. It takes five minutes, it’s anonymous, and it will help future candidates.