← Questions

Quickselect, distance between tree nodes, and substrings with a prefix

0
GoogleCodingLast asked Apr 2026

A coding interview question asked in Google's onsite coding round, last asked Apr 2026. It tests quickselect, trees and strings.

Three independent problems in one round.

Part 1: quickselect

Return the k-th smallest element of an unsorted array (k starts at 1; duplicates count separately). Discuss the possible approaches and their costs, and how to choose a pivot, then implement one.

Unless you agree on a different interface with the interviewer, assume:

  • kth_smallest(nums: list[int], k: int) -> int — return -1 if nums is empty or k is out of range.
kth_smallest([7, 10, 4, 3, 20, 15], 3)  ->  7
kth_smallest([5, 3, 1, 2, 2, 4], 4)     ->  3     # sorted: 1 2 2 3 4 5

Part 2: distance between two nodes in a tree

An undirected tree is given as a list of edges. Return the number of edges on the path between nodes u and v.

  • tree_distance(edges: list[tuple[int, int]], u: int, v: int) -> int — 0 if u == v and the node exists; -1 if either node is not in the tree.
tree_distance([(0,1), (1,2), (1,3)], 2, 3)  ->  2     # 2 - 1 - 3
tree_distance([(1,2), (2,3)], 2, 2)         ->  0
tree_distance([(1,2)], 1, 9)                ->  -1

Be ready to discuss duplicate edges and input that is not a valid tree.

Part 3: substrings that start with a target

Count the substrings of s whose first len(target) characters equal target.

  • count_prefixed(s: str, target: str) -> int
count_prefixed("ababa", "aba")  ->  4     # target at 0 (3 substrings) and at 2 (1 substring)
count_prefixed("aaaa", "aa")    ->  6     # starts 0, 1, 2 give 3 + 2 + 1

Follow-up: return the length of the shortest such substring, or -1 if there is none.

Constraints

  • Up to 2 * 10^5 elements, edges or characters in each part. Part 3's answer fits in 64 bits.

Clarifying questions

  • Part 1: may I modify the input? Is expected O(n) acceptable, or is worst-case O(n) required?
  • Part 2: how should duplicate edges, cycles or a disconnected edge list be handled?
  • Part 3: are overlapping occurrences counted? (Yes: each start position counts.)

What a strong answer covers

  • Part 1: sorting O(n log n), a heap O(n log k), quickselect expected O(n); a random pivot against adversarial input, three-way partitioning for many duplicates, median of medians for a worst-case bound.
  • Part 2: an adjacency list and BFS from u, O(n); for many queries, LCA with binary lifting.
  • Part 3: every occurrence of target at index i adds len(s) - i - len(target) + 1 substrings. Find occurrences with KMP or the Z-algorithm in O(len(s) + len(target)). The follow-up's answer is len(target) whenever it occurs at all.
  • Tests for each part: empty input, k at both ends, u == v, target longer than s.

Follow-ups

  • Part 1: the k smallest elements, not just the k-th.
  • Part 2: many distance queries on the same tree.
  • Part 3: count substrings that contain target anywhere.

Where it's asked

  • Google · Onsite coding · Last asked Apr 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

quickselect · trees · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding