← Google Interview Insights

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

SeniorPrefer not to say
Apr 2026New York City

Summary

Onsite coding round at Google's NYC office, just one problem but it had enough depth to keep me busy for the full slot. The question looked deceptively simple at first glance.

Questions Asked (1)

Q1

Design a class that supports inserting numbers (including duplicates) and finding the (k+1)-th largest value in the collection, where findLargest(0) returns the max. Optimize for query time over insert time.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

My first instinct was a max-heap and I said it out loud before realizing that only works if k is fixed ahead of time.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the requirements: insertions can include duplicates, and findLargest(k) returns the (k+1)-th largest element (0-indexed). Since queries are more frequent, use a data structure that maintains sorted order with O(log n) insertion and O(1) query, such as a balanced BST with subtree sizes or two heaps with lazy deletion. Discuss trade-offs and handle edge cases like k out of bounds.

Pro tip: Mention that you would use a Fenwick tree over compressed values for O(log n) insert and O(log n) query, but if queries are truly O(1) needed, a balanced BST with subtree sizes is optimal. Also, clarify if the data is static or dynamic, as that changes the approach.

1. Clarify requirements and constraints

Ask about expected number of insertions vs queries, memory constraints, and whether k is guaranteed valid. Confirm that duplicates are allowed and that findLargest(0) returns the maximum.

2. Propose a data structure

Suggest a balanced BST (e.g., AVL or Red-Black tree) augmented with subtree sizes, or a Fenwick tree over compressed values. Explain how it supports O(log n) insertion and O(log n) query for the k-th largest.

3. Analyze time and space complexity

Compare with alternatives like sorting on insert (O(n) insert, O(1) query) or maintaining a sorted list. Justify why the chosen structure optimizes query time while keeping insertion efficient.

4. Handle edge cases and duplicates

Discuss how duplicates are stored (e.g., counts in nodes) and how to handle k out of bounds (return null or throw exception). Also consider concurrency if needed.

5. Discuss potential optimizations and trade-offs

Mention that if queries are extremely frequent, a two-heap approach with lazy deletion can give O(1) query for the median, but for arbitrary k, a BST is better. Also, consider if the data is static, then sorting once and using binary search is optimal.

Key Points to Mention

  • Balanced BST with subtree sizes for O(log n) insert and O(log n) find k-th largest
  • Fenwick tree (Binary Indexed Tree) over compressed values for O(log n) operations
  • Two heaps with lazy deletion for O(1) query if only median is needed, but not for arbitrary k
  • Handling duplicates by storing counts in nodes or using a multiset
  • Trade-off between insert and query time: sorting on insert gives O(1) query but O(n) insert
  • Edge cases: k out of bounds, empty collection, and concurrency considerations

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