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.
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.
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.