FastPrepColored Rooted Tree
Problems/Colored Rooted Tree

Colored Rooted Tree

Rubrik logoRubrik● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This is the second round of a Rubrik interview loop, listed under multithreading and distributed systems. You build a rooted tree in which every node except the root has one parent. Nodes begin with no color, a node with no color of its own takes its parent's color, and a color given to a node wins over the one it would take. A follow-up asks how to handle a tree of 10 million nodes.

The report gives no input format, operation names, limits or examples. We chose the parent array, the numbered set and get operations, 0 for no color, recoloring, the limits and all three examples. The round label mentions multithreading, but the task itself does not ask for thread safety, so this version has none. The 10 million node follow-up is covered in the editorial.

Our version is about 85% the same as the reported question.

Implement a colored rooted tree: run a list of operations that set and read node colors, and return the answers of the get operations in order.

The tree has n nodes numbered from 0 to n - 1. Every node has exactly one parent, except the root. parent[i] is the parent of node i, and parent[i] = -1 marks the root. The root can be any node, and a parent can have a larger number than its child.

The color rules are:

  • At the start, no node has a color.
  • If a node has no color assigned to it, it inherits its parent's effective color.
  • A color assigned to a node overrides any color it would inherit.

So the effective color of a node is the color of the nearest node with an assigned color on the path from that node up to the root, the node itself included. If no node on that path has a color, the effective color is 0.

Each operation is a list of integers, and its first number gives its type:

  • [1, node, color] is a set: it assigns color to node. If node already has an assigned color, the new color replaces it. No other node's assigned color changes.
  • [2, node] is a get: it asks for the effective color of node.

Function

colorTree(parent: int[], operations: int[][]) → int[]

Examples

Example 1

parent = [-1,0,0,1,1,2]operations = [[2,3],[1,1,5],[2,3],[2,2],[1,0,2],[1,4,7],[2,4],[2,3],[2,5],[1,1,9],[2,3]]return = [0,5,0,7,5,2,9]

Node 0 is the root with children 1 and 2. Node 1 has children 3 and 4, and node 2 has child 5.

  • [2, 3]: no node has a color yet, so the answer is 0.
  • [1, 1, 5], then [2, 3]: node 3 has no color of its own, and its parent 1 has 5.
  • [2, 2]: neither 2 nor the root 0 has a color, so 0.
  • [1, 0, 2] and [1, 4, 7], then [2, 4]: node 4's own color, 7.
  • [2, 3]: the nearest colored node above 3 is 1, so 5, not the root's 2.
  • [2, 5]: 5 and 2 have no color, so 5 gets the root's 2.
  • [1, 1, 9] replaces node 1's color, so [2, 3] now gives 9.

Example 2

parent = [1,-1,0]operations = [[1,1,4],[2,2],[1,0,8],[2,2],[1,1,3],[2,1],[2,2]]return = [4,8,3,8]

The root is node 1. Node 0's parent is 1, and node 2's parent is 0. After [1, 1, 4], node 2 inherits 4 through node 0. After [1, 0, 8], node 2 inherits 8 from node 0. [1, 1, 3] recolors the root to 3, but node 0 keeps its own 8, so node 2 still gives 8.

Example 3

parent = [-1]operations = [[2,0]]return = [0]

The tree is a single node with no color, so the answer is 0.

Constraints

  • n == parent.length
  • 1 <= n <= 10^5
  • parent[i] == -1 for exactly one node, the root. Every other parent[i] is a node from 0 to n - 1, and following parents from any node reaches the root.
  • 1 <= operations.length <= 5000
  • Each operations[i] is [1, node, color] or [2, node].
  • 0 <= node < n
  • 1 <= color <= 10^9
  • At least one operation is a get, [2, node].

More Rubrik interview questions

  • All Balanced Strings from Minimum Parenthesis InsertionsONSITE INTERVIEW · Seen Nov 2026
  • Insert into a Left-Complete Binary TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Request Counts by AttributeONSITE INTERVIEW · Seen Oct 2026
  • Delete and Freeze GameOA · Seen Nov 2026
  • Delete K Subsets for Maximum ScoreOA · Seen Nov 2026
  • Distribute Elements Into Two Arrays IIOA · Seen Nov 2026
  • Minimum Replacements to Sort the ArrayOA · Seen Nov 2026
  • Minimum Swaps To Binary PalindromeOA · Seen Apr 2026
  • Get Minimum OperationsOA · Seen Nov 2024
  • Horizontal Pod AutoscalerOA · Seen Nov 2024

Similar questions at other companies

  • Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Serialize and Deserialize a Binary TreeMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Serialize and Deserialize an N-Ary TreeUber · ONSITE INTERVIEWUber interview questions
  • Nested Todo ListNotion · PHONE SCREEN · ONSITE INTERVIEWNotion interview questions
  • Evaluate a Parsed Expression for Many Values of xGoogle · ONSITE INTERVIEWGoogle interview questions
  • Subtree Access OverridesPinterest · PHONE SCREENPinterest interview questions
  • Throne Inheritance Without an Initial KingSnowflake · PHONE SCREENSnowflake interview questions
  • Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
All Rubrik interview questionsSee Rubrik hiring insights
Saved
class Solution {
    public int[] colorTree(int[] parent, int[][] operations) {
      // Write your code here.
    }

}
Checking account…