As reported: Rubrik SWE1 loop, algo coding round — a sample question from Rubrik's preparation material (2026): insert a node into a left-complete binary tree. A linear solution is the starting point; then a more optimal approach, with time and space analysis.
This follows LeetCode 919 — Complete Binary Tree Inserter.
A complete binary tree has every level full except possibly the last, whose nodes are as far left as possible. Implement CBTInserter:
CBTInserter(root)— starts from the given complete tree.insert(val)— adds a node with valuevalso the tree stays complete, and returns the value of the new node's parent.get_root()— returns the root.
Example
["CBTInserter","insert","insert","get_root"]
[[[1,2]],[3],[4],[]]
-> [null,1,2,[1,2,3,4]]
3 becomes the right child of 1; 4 becomes the left child of 2.
Constraints: the tree has 1 to 1000 nodes initially; 0 ≤ Node.val ≤ 5000; root is complete; at most 10^4 calls to insert and get_root.
Code runner format: LeetCode's design format — line 1 the method names, line 2 their arguments (the tree as a level-order array). Implement the CBTInserter class; the harness prints the list of results (get_root as a level-order array).