← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Three coding problems back to back for a Google SWE round. Nothing too outrageous but the tree distance one had some edge case discussion that I wasn't fully prepared for.

Questions Asked (3)

Q1

Implement Quickselect to find the k-th smallest element in an unsorted integer array (1-indexed, duplicates allowed). Walk through different approaches and their complexities, explain how you'd choose a pivot, then code a working solution.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew Quickselect going in but fumbled the pivot discussion more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by comparing brute force, sorting, and heap approaches, then focus on Quickselect as the optimal average-case O(n) solution. Explain pivot selection strategies (random, median-of-medians) and their impact on worst-case complexity, then implement with careful partitioning and handling of duplicates.

Pro tip: Mention that random pivot selection gives expected O(n) time and that median-of-medians guarantees O(n) worst-case, but is rarely used in practice due to high constants. Also, clarify how duplicates are handled in partitioning to avoid infinite loops.

1. Clarify problem and constraints

Confirm that k is 1-indexed, duplicates are allowed, and the array can be modified. Discuss edge cases like k out of bounds or empty array.

2. Compare approaches

Mention sorting (O(n log n)), min-heap of size k (O(n log k)), and Quickselect (average O(n), worst O(n^2)). Highlight Quickselect as optimal for average case.

3. Explain Quickselect and pivot selection

Describe the algorithm: partition around a pivot, recurse only on the side containing the k-th element. Discuss pivot strategies: random (expected O(n)), median-of-medians (worst-case O(n)), and first/last (bad for sorted input).

4. Implement partition and Quickselect

Code a partition function (e.g., Lomuto or Hoare) that handles duplicates. Implement Quickselect iteratively or recursively, adjusting k based on pivot index.

5. Analyze complexity and test

State time complexity: average O(n), worst O(n^2) with random pivot, O(n) with median-of-medians. Space: O(1) iterative, O(log n) recursive. Walk through an example and edge cases.

Key Points to Mention

  • Time complexity: average O(n), worst O(n^2) with random pivot, O(n) with median-of-medians.
  • Space complexity: O(1) iterative, O(log n) recursive due to call stack.
  • Pivot selection: random pivot gives expected linear time; median-of-medians guarantees linear worst-case but has high constant factors.
  • Partitioning scheme: Lomuto vs Hoare, and how to handle duplicates (e.g., three-way partitioning or careful pointer movement).
  • Comparison with other approaches: sorting O(n log n), heap O(n log k).
  • Edge cases: k=1 (minimum), k=n (maximum), all elements equal, k out of bounds.

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

Q2

Given an undirected tree as an edge list and two nodes u and v, return the number of edges on the path between them. Return 0 if they're the same node, -1 if either node doesn't exist. Be ready to discuss how you'd handle invalid input like duplicate edges.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The edge case discussion is what got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then propose an efficient solution using BFS/DFS from one node to find the other, or preprocess the tree for LCA if multiple queries are expected. Discuss how to validate input and handle invalid cases like duplicate edges, missing nodes, or cycles.

Pro tip: Mention that you'd first build an adjacency list and validate the tree structure (e.g., check for cycles or duplicate edges) before answering queries, showing you think about robustness and real-world data issues.

1. Clarify requirements and edge cases

Ask about input format, whether the tree is guaranteed valid, and if multiple queries will be made. Confirm return values for same node, missing nodes, and invalid edges.

2. Choose an algorithm

For a single query, BFS/DFS from u to v is O(N). For multiple queries, preprocess with LCA (binary lifting or Euler tour + RMQ) for O(log N) per query.

3. Handle invalid input

Validate that u and v exist in the node set. Detect duplicate edges by checking if an edge already exists in the adjacency list; if duplicates are present, decide whether to ignore or treat as invalid.

4. Implement and test

Write clean code with helper functions. Test with cases: same node, adjacent nodes, distant nodes, non-existent nodes, duplicate edges, and disconnected components (if tree not guaranteed).

5. Analyze complexity and trade-offs

Discuss time/space complexity of chosen approach. Compare BFS/DFS vs LCA preprocessing, and explain when each is preferable based on query frequency.

Key Points to Mention

  • Tree properties: acyclic, connected, N nodes and N-1 edges.
  • BFS/DFS for path finding: track distance from source, stop when target found.
  • LCA preprocessing: binary lifting or Euler tour + RMQ for multiple queries.
  • Input validation: check node existence, duplicate edges, and tree validity.
  • Handling duplicate edges: use a set or check adjacency list before adding.
  • Complexity: O(N) per query for BFS/DFS, O(N log N) preprocessing + O(log N) per query for LCA.

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

Q3

Count how many substrings of a string s start with a given non-empty prefix target. Return the count as a 64-bit integer. Follow-up: find the length of the shortest such substring, or return -1 if none exist.

Algorithms & Data Structures
Author's notes

Easiest of the three.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, verify that the string s starts with the target prefix; if not, return 0 (and -1 for the follow-up). If it does, the count of substrings starting with target is simply the number of possible ending positions, which is n - m + 1, where n is the length of s and m is the length of target. For the shortest substring, the answer is m itself (the prefix itself), so return m if the prefix matches, else -1.

Pro tip: Clarify whether overlapping substrings are counted (they are) and mention that the count can be large, so use a 64-bit integer to avoid overflow. Also, explicitly state that the shortest substring is always the prefix itself if it exists, demonstrating you understand the problem's simplicity.

1. Clarify the problem

Confirm that substrings are contiguous and that we count all occurrences, including overlapping ones. Ask if the prefix must match exactly at the start of the substring.

2. Check prefix match

Compare the first m characters of s with target. If they don't match, return 0 for the count and -1 for the shortest length.

3. Compute count

If the prefix matches, the number of substrings starting with target is n - m + 1. Use a 64-bit integer to store the result.

4. Find shortest substring

The shortest substring starting with target is the prefix itself, so its length is m. Return m if the prefix matches, else -1.

5. Analyze complexity

The solution runs in O(m) time for the prefix check and O(1) additional time for the count and shortest length, with O(1) space.

Key Points to Mention

  • Substrings are contiguous and overlapping occurrences are counted.
  • The count formula: n - m + 1 when the prefix matches, where n = len(s), m = len(target).
  • Use 64-bit integer (e.g., long long in C++ or long in Java) to prevent overflow.
  • The shortest substring is exactly the prefix itself, so its length is m.
  • Time complexity: O(m) for prefix comparison, O(1) for the rest.
  • Edge cases: empty target (though problem says non-empty), target longer than s, or no match.

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