Quickselect, distance between tree nodes, and substrings with a prefix
I was asked this
No reports yet. Sign in to say where you were asked it.
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-1ifnumsis empty orkis 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—0ifu == vand the node exists;-1if 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-caseO(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 heapO(n log k), quickselect expectedO(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
targetat indexiaddslen(s) - i - len(target) + 1substrings. Find occurrences with KMP or the Z-algorithm inO(len(s) + len(target)). The follow-up's answer islen(target)whenever it occurs at all. - Tests for each part: empty input,
kat both ends,u == v, target longer thans.
Follow-ups
- Part 1: the
ksmallest elements, not just thek-th. - Part 2: many distance queries on the same tree.
- Part 3: count substrings that contain
targetanywhere.
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
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
