Find mode in a trinary search tree
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's understanding of tree data structures and frequency analysis, focusing on handling a trinary search tree variant and determining mode values across possibly large datasets.
Part 1: Find Mode(s) in a Valid Trinary Search Tree
Constraints
- 0 <= n <= 100000, where n == len(values)
- len(left) == len(middle) == len(right) == n
- Each child index is either -1 or an integer in [0, n - 1]
- root == -1 if n == 0; otherwise 0 <= root < n
- The nodes reachable from root form a valid tree with no cycles
- The tree satisfies the trinary search ordering rules recursively
- Each value is a 32-bit signed integer
Examples
Input: ([], [], [], [], -1)
Expected Output: []
Explanation: The tree is empty, so there are no modes.
Input: ([42], [-1], [-1], [-1], 0)
Expected Output: [42]
Explanation: A single-node tree has that node's value as the only mode.
Input: ([0, -2147483648, 2147483647], [1, -1, -1], [-1, -1, -1], [2, -1, -1], 0)
Expected Output: [-2147483648, 0, 2147483647]
Explanation: All three values occur once, so they all tie as modes.
Input: ([5, 3, 5, 7, 5], [1, -1, -1, -1, -1], [2, -1, 4, -1, -1], [3, -1, -1, -1, -1], 0)
Expected Output: [5]
Explanation: Value 5 occurs three times, while 3 and 7 occur once.
Input: ([10, 4, 10, 12, 4], [1, -1, -1, -1, -1], [2, 4, -1, -1, -1], [3, -1, -1, -1, -1], 0)
Expected Output: [4, 10]
Explanation: Values 4 and 10 each occur twice, tying for highest frequency.
Hints
- In a valid trinary search tree, an inorder traversal using left, node, middle, right visits values in nondecreasing order.
- Because equal values appear consecutively during that traversal, you can track only the current run length and the best run length so far.
Part 2: Find Mode(s) in a Broken Trinary Tree
Constraints
- 0 <= n <= 100000, where n == len(values)
- len(left) == len(middle) == len(right) == n
- Each child index is either -1 or an integer in [0, n - 1]
- root == -1 if n == 0; otherwise 0 <= root < n
- The nodes reachable from root form a tree with no cycles
- No ordering rule is guaranteed between a node and its children
- Each value is a 32-bit signed integer
Examples
Input: ([], [], [], [], -1)
Expected Output: []
Explanation: The tree is empty, so there are no modes.
Input: ([-2147483648], [-1], [-1], [-1], 0)
Expected Output: [-2147483648]
Explanation: A single-node tree has that node's value as the only mode.
Input: ([5, 8, 1, 8, 5, 8], [1, -1, 4, -1, -1, -1], [2, -1, -1, -1, -1, -1], [3, -1, -1, -1, 5, -1], 0)
Expected Output: [8]
Explanation: The tree violates trinary search ordering, but counting all nodes shows that 8 occurs three times.
Input: ([2, 1, 3, 1, 3, 4], [1, -1, 4, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [2, 3, 5, -1, -1, -1], 0)
Expected Output: [1, 3]
Explanation: Values 1 and 3 each occur twice, tying for highest frequency.
Input: ([0, 2147483647, -2147483648], [1, -1, -1], [-1, -1, -1], [2, -1, -1], 0)
Expected Output: [-2147483648, 0, 2147483647]
Explanation: All values occur once, even though the child ordering is broken.
Hints
- Without the ordering property, equal values are not guaranteed to be adjacent in any traversal.
- Use a frequency table while traversing the entire tree.