FastPrepUniformly Sample a Node from a Large Tree
Problems/Uniformly Sample a Node from a Large Tree

Uniformly Sample a Node from a Large Tree

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The report asks for picking a node from a large tree with equal chance, without flattening it or counting subtree sizes. It gives no tree format or random function. Our version is about 75% the same as the reported question.

You are given a rooted tree whose nodes are numbered from 0 to n - 1. Node 0 is the root, and children[u] lists the children of node u in traversal order.

Visit the nodes in depth-first preorder without first copying all nodes into a flat list or computing subtree sizes. At the i-th visited node (using zero-based i), draws[i] is a recorded uniformly chosen integer from 0 through i, inclusive. If the draw is 0, replace the one-node sample with the current node; otherwise retain the earlier sample.

Return the sampled node number after visiting every node. The recorded draws fix the result of one run. With truly uniform, independent draws, the one-pass rule above chooses each node with probability 1/n. Use an iterative traversal so a very deep tree does not depend on the call stack.

Function

sampleUniformTreeNode(children: int[][], draws: int[]) → int

Examples

Example 1

children = [[1,2],[3],[],[]]draws = [0,1,0,2]return = 3

The preorder is 0, 1, 3, 2. The draw at visit index 2 is 0, so node 3 replaces the earlier sample. The last draw is not 0.

Example 2

children = [[1,2,3],[],[],[]]draws = [0,0,1,0]return = 3

Nodes are visited as 0, 1, 2, 3. Draws of 0 select nodes 0, 1, and finally 3.

Example 3

children = [[]]draws = [0]return = 0

The only node is the root, and its only valid draw is 0.

Constraints

  • 1 <= n = children.length <= 100000.
  • children defines a valid rooted tree: every node other than 0 occurs in exactly one child list, node 0 occurs in none, and all nodes are reachable from 0.
  • Child lists contain no duplicate node numbers, and the sum of their lengths is n - 1.
  • draws.length == n, and 0 <= draws[i] <= i for every i.

More Meta Onsite Interview questions

  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
  • Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
  • Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
  • Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
  • Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
  • Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Evaluate a Parsed Expression for Many Values of xGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Prime Values in SubtreesVisa · OAVisa interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Path Between Two Nodes of a Fibonacci TreeDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
  • Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
  • Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
  • Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public int sampleUniformTreeNode(int[][] children, int[] draws) {
        // Return one sampled node using iterative preorder and the recorded draws.
    }
}
Checking account…