Minimum Height
Updated
Problem statement
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
uof a parent tablevand remove the edge betweenuandv. - Move
uand 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 treeint tree_from[tree_nodes - 1]: the starting node of the edgeint tree_to[tree_nodes - 1]: the ending node of the edgeint 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 = 4tree_from = [3, 1, 2]tree_to = [2, 3, 4]max_operations = 1return = 21324
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.
More Snowflake Online Assessment questions
- Safest Shortest Path Through a GridOA · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- LFU CacheOA · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Optimal TransferOA · Seen Oct 2026
- Generating Login CodesOA · Seen Sep 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Sep 2026
- Minimum Clicks Between Wiki PagesOA · PHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Minimum-Sum Tree Fill with Unit StepsVisa · OAVisa interview questions
- Find Minimum Possible DiameterGoogle · OAGoogle interview questions
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Server Upgrade PlanningWells Fargo · OAWells Fargo interview questions
- Find Minimum TimeAlphaSense · OA
- Find Minimum TimeBlackRock · OABlackRock interview questions
- Maximize Pipeline Throughput Under a Scaling BudgetMicrosoft · OAMicrosoft interview questions
- Tree Pythagorean TriplesIBM · OAIBM interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Robot and Human Fulfillment TimeAmazon · OA