Solve Quickselect, Tree Distance, and Prefix Substrings
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This multi-part question evaluates algorithmic problem-solving across selection algorithms, graph/tree traversal, and string processing, testing competencies such as Quickselect behavior and pivot reasoning, computation of node distances in a tree, and prefix-based substring counting.
Part 1: Implement Quickselect
Constraints
- 0 <= len(nums) <= 2 * 10^5
- -10^9 <= nums[i] <= 10^9
- k may be invalid in hidden tests; return -1 in that case
- Duplicates may appear
Examples
Input: ([7, 10, 4, 3, 20, 15], 3)
Expected Output: 7
Explanation: The sorted order is [3, 4, 7, 10, 15, 20], so the 3rd smallest element is 7.
Input: ([5, 3, 1, 2, 2, 4], 4)
Expected Output: 3
Explanation: The sorted order is [1, 2, 2, 3, 4, 5], so the 4th smallest element is 3.
Hints
- Partition the array around a pivot like Quicksort, but only continue into the side that can contain the answer.
- A 3-way partition is especially helpful when many elements are equal to the pivot.
Part 2: Find the Distance Between Two Nodes in a Tree
Constraints
- 0 <= len(edges) <= 2 * 10^5
- Each edge is a pair of distinct integers
- Node labels are integers
- Assume the input is a valid tree unless otherwise stated
Examples
Input: ([[0, 1], [1, 2], [1, 3]], 2, 3)
Expected Output: 2
Explanation: The unique path is 2 -> 1 -> 3, which uses 2 edges.
Input: ([[1, 2], [2, 3]], 2, 2)
Expected Output: 0
Explanation: The two targets are the same existing node, so the distance is 0.
Hints
- Convert the edge list into an adjacency list so you can traverse the tree efficiently.
- Because all edges have equal weight, a BFS from u gives the shortest edge distance to every reachable node.
Part 3: Count Substrings With a Given Prefix
Constraints
- 0 <= len(s) <= 2 * 10^5
- 1 <= len(target) <= 2 * 10^5
- s and target consist of comparable characters
- The answer fits in a signed 64-bit integer
Examples
Input: ('ababa', 'aba')
Expected Output: 4
Explanation: Target appears at starts 0 and 2. These contribute 3 and 1 valid substrings, for a total of 4.
Input: ('aaaa', 'aa')
Expected Output: 6
Explanation: Target appears at starts 0, 1, and 2. These contribute 3, 2, and 1 substrings.
Hints
- If target occurs starting at index i, then every substring starting at i and ending at any position from i + len(target) - 1 to the end is valid.
- Use a linear-time string matching algorithm such as KMP or Z-algorithm to find all occurrences efficiently.
Part 4: Follow-up - Return the Length of the Shortest Valid Substring
Constraints
- 0 <= len(s) <= 2 * 10^5
- 1 <= len(target) <= 2 * 10^5
- s and target consist of comparable characters
Examples
Input: ('ababa', 'aba')
Expected Output: 3
Explanation: Since target appears in s, the substring 'aba' itself is valid and has length 3.
Input: ('aaaa', 'aa')
Expected Output: 2
Explanation: The shortest valid substring is exactly 'aa'.
Hints
- What is the minimum possible length of any substring whose prefix is target?
- After that observation, the problem becomes checking whether target appears anywhere in s.