← Google Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Google SWE coding round, two questions back to back. First one felt like a heap problem dressed up with extra sorting rules, second was a binary search question that I almost overcomplicated. Neither was brutal but both had enough edge cases to trip you up if you moved too fast.

Questions Asked (2)

Q1

Given an array of 2D points that may contain duplicates, return the top K distinct points ranked by frequency (descending), then by squared distance to the origin (ascending) as a tiebreaker, then by x and y coordinates ascending if still tied.

Algorithms & Data Structures
Author's notes

My first instinct was a hash map for counts, then sort.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, count the frequency of each distinct point using a hash map. Then, sort the distinct points with a custom comparator that orders by frequency descending, squared distance ascending, and finally x and y ascending. Finally, return the first K points.

Pro tip: Clarify edge cases upfront: what if K exceeds the number of distinct points? Also, discuss trade-offs between sorting and using a heap for large datasets.

1. Clarify requirements and edge cases

Confirm input format, constraints, and expected output. Ask about K's range, duplicate handling, and tie-breaking rules.

2. Count frequencies

Iterate through the array and use a hash map to count occurrences of each distinct point.

3. Sort distinct points

Extract unique points and sort them using a comparator that implements the specified ordering: frequency descending, squared distance ascending, then x and y ascending.

4. Select top K

Take the first K elements from the sorted list. If K is larger than the number of distinct points, return all.

5. Analyze complexity and optimizations

Discuss time and space complexity. Mention alternative approaches like using a heap for better performance when K is small.

Key Points to Mention

  • Hash map for frequency counting
  • Custom comparator with multiple tie-breaking rules
  • Squared distance calculation (avoid floating-point)
  • Time complexity: O(N + D log D) where D is distinct points
  • Space complexity: O(D) for the hash map and sorted list
  • Edge cases: K > distinct points, empty input, negative coordinates

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

Q2

Design a time-based key-value store that supports storing values with timestamps and retrieving the value whose timestamp is closest to a given query timestamp, with ties broken in favor of the smaller timestamp.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Binary search question, pretty clear once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: operations include set(key, value, timestamp) and get(key, timestamp). For each key, store a list of (timestamp, value) pairs sorted by timestamp, and use binary search to find the closest timestamp, handling ties by choosing the smaller timestamp. Discuss trade-offs between different data structures and consider concurrency if needed.

Pro tip: Mention that timestamps are monotonically increasing for set operations, so appending to a list maintains sorted order, enabling O(log n) binary search for get. Also, explicitly handle the tie-breaking rule by checking both neighbors after binary search.

1. Clarify Requirements and Constraints

Ask about expected operations, timestamp uniqueness, tie-breaking rules, and performance requirements. Confirm that set timestamps are strictly increasing per key.

2. Choose Data Structures

Decide to use a hash map from key to a list of (timestamp, value) pairs, leveraging the monotonic timestamps to keep the list sorted. Alternatively, consider a balanced BST or skip list for more general cases.

3. Design set Operation

For set, append the new (timestamp, value) to the list for the key. Since timestamps are increasing, the list remains sorted. This is O(1) amortized.

4. Design get Operation

For get, perform binary search on the list to find the insertion point for the query timestamp. Then compare the timestamp at the insertion point and its predecessor to find the closest, applying tie-breaking (prefer smaller timestamp). Return the value or empty string if none.

5. Analyze Complexity and Edge Cases

Discuss time complexity: O(1) for set, O(log n) for get. Handle edge cases: query timestamp before all, after all, exact match, and multiple keys. Mention potential concurrency considerations.

Key Points to Mention

  • Use a hash map to store per-key sorted lists of (timestamp, value) pairs.
  • Leverage monotonic timestamps to append and maintain sorted order.
  • Binary search to find the closest timestamp efficiently.
  • Tie-breaking: when two timestamps are equidistant, choose the smaller one.
  • Time complexity: O(1) for set, O(log n) for get; space O(n).
  • Edge cases: no previous timestamp, exact match, and query before first timestamp.

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