Mode of a Ternary Search Tree
Updated
Problem statement
A ternary search tree is encoded by a node table nodes. Row i is [value, leftIndex, middleIndex, rightIndex], row 0 is the root, and -1 means that a child is absent.
- Every node in a left subtree has a smaller value than its parent.
- Every node in a middle subtree has the same value as its parent.
- Every node in a right subtree has a larger value than its parent.
Return the mode: the value that occurs most often. If several values have the same highest frequency, return the smallest one.
Function
ternaryTreeMode(nodes: int[][]) → intExamples
Example 1
nodes = [[5,1,2,3],[3,-1,-1,-1],[5,-1,4,-1],[8,-1,-1,-1],[5,-1,-1,-1]]return = 5The value 5 occurs three times.
Example 2
nodes = [[2,1,2,3],[1,-1,-1,-1],[2,-1,-1,-1],[3,-1,-1,-1]]return = 2The root value occurs twice.
Example 3
nodes = [[7,-1,-1,-1]]return = 7A one-node tree has that node's value as its mode.
Constraints
1 <= nodes.length <= 200000.- Each row has exactly four integers.
- Every nonnegative child index is valid, every node is reachable exactly once from row
0, and the stated ternary-search-tree invariant holds.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Lowest Common Manager for Employee GroupsAmazon · PHONE SCREEN
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Translate Values in a Nested MapZip · PHONE SCREENZip interview questions
- Build a Trace ForestDatadog · PHONE SCREENDatadog interview questions