Minimum Height
Reported by candidates from Snowflake's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Snowflake reported this one in July 2026, and the setup is odd enough to throw you. A database is a rooted tree at Table 1, and you get max_operations cuts. Each cut detaches a node with its whole subtree and hangs it directly off the root. You want the smallest possible height. If you have the OA in a day or two, this is a binary search on the answer plus a greedy cut count, not a tree DP puzzle. StealthCoder sits invisibly on your screen as a safety net if you blank on the check function mid-assessment.
The problem
A database is represented as a rooted tree with tree_nodes tables, where Table 1 is the root. Up to max_operations operations are allowed. In one operation: Select a child table u of a parent table v and remove the edge between u and v. Move u and its subtree to become a direct child of the root, reducing its depth. Determine the minimum possible height of the tree. Note: The height of the tree is defined as the maximum depth of any table. The depth of a table is the number of edges from the root to that table. A subtree in a rooted tree is a subgraph of the tree consisting of a vertex, the root of that subtree, and its descendants, along with all edges incident to these descendants. Function getMinimumHeight(tree_nodes: int, tree_from: int[], tree_to: int[], max_operations: int) → int Complete the function getMinimumHeight in the editor with the following parameters: int tree_nodes: the number of nodes in the tree int tree_from[tree_nodes - 1]: the starting node of the edge int tree_to[tree_nodes - 1]: the ending node of the edge int max_operations: the maximum number of operations Returns int: the minimum possible height of the tree after as many as max_operations operations Examples Example 1 tree_nodes = 4 tree_from = [3, 1, 2] tree_to = [2, 3, 4] max_operations = 1 return = 2 1324 One possible operation can be: Remove the edge (2, 4) and add an edge (1, 4). 4132 So, the height of the tree is 2.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: binary search the target height H. For each H, count the minimum cuts needed so no node sits deeper than H. Do a DFS that returns each node's subtree height. If a node's height from below reaches H while its parent isn't the root, cut that node. That moves its subtree to the root and resets its contribution to 0. Cutting the deepest possible nodes first is the greedy part, because one cut clears the most depth. If cuts needed is at most max_operations, H works. The common pitfall is cutting from the top, which wastes operations. Another is forgetting that nodes already adjacent to the root can't be improved. Use iterative DFS or a BFS order processed in reverse, since the tree can be deep. If the greedy check slips under pressure, StealthCoder is the hedge during the live OA.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Minimum Height cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Snowflake's OA.
Snowflake reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Height FAQ
How hard is the Snowflake Minimum Height problem really?+
Medium to hard. The tree part is easy. The hard part is seeing that it's a binary search on height with a greedy feasibility check. Once you see that, the code is short, around 30 lines.
What's the core trick for Minimum Height?+
Binary search the final height H. For each H, run a post-order pass computing subtree heights. When a non-root-child node's height reaches H, cut it and return 0 upward. Count cuts and compare to max_operations.
Why cut the deepest nodes first?+
One cut removes a whole subtree from its parent's depth chain. Cutting the node whose height hits H from below fixes the most violations per operation. Cutting near the root moves little and wastes your budget.
Do I need recursion, and is stack depth a risk?+
A skewed tree with many nodes can overflow recursion. Build the adjacency list from the edges, get a BFS or DFS order from node 1, then process it in reverse to compute heights iteratively. That avoids the stack issue.
How do I prepare for this in 48 hours?+
Practice binary search on answer problems with a greedy check, plus post-order tree height computation. Then trace the sample by hand: 4 nodes, 1 operation, answer 2. Edge cases: max_operations 0 returns the original height, and a star tree returns 1.