FastPrepMode of a Ternary Search Tree
Problems/Mode of a Ternary Search Tree

Mode of a Ternary Search Tree

Google logoGoogle● EasyNEW GRADPHONE SCREEN

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The firsthand Google new-grad report defines a ternary tree with smaller values on the left, equal values in the middle, and larger values on the right, then asks for the tree's mode. The smallest-value tie rule is an explicit practice assumption; the undefined 'broken tree' follow-up is excluded.

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[][]) → int

Examples

Example 1

nodes = [[5,1,2,3],[3,-1,-1,-1],[5,-1,4,-1],[8,-1,-1,-1],[5,-1,-1,-1]]return = 5

The 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 = 2

The root value occurs twice.

Example 3

nodes = [[7,-1,-1,-1]]return = 7

A 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

Interview reports that mention this question

  • Google New Grad R1, Ternary Tree Mode: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int ternaryTreeMode(int[][] nodes) {
        // Write your code here.
    }
}
Checking account…