PracHub
QuestionsLearningGuidesInterview Prep
|Home/Software Engineering Fundamentals/Snowflake

Clarify and Prune an N-ary Tree to a Depth Limit

Last updated: Jul 28, 2026

Quick Overview

Clarify an ambiguous N-ary tree task involving maximum depth and minimum deletion under a depth limit. Compare distinct deletion-cost contracts, preserve a consistent depth convention, handle empty and root-only trees, and justify correctness and complexity without assuming one interpretation.

  • hard
  • Snowflake
  • Software Engineering Fundamentals
  • Software Engineer

Clarify and Prune an N-ary Tree to a Depth Limit

Company: Snowflake

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Technical Screen

You are given a rooted N-ary tree and a nonnegative integer `k`. First compute the tree's maximum depth. Then discuss how to delete a minimum set of nodes so the resulting tree has maximum depth at most `k`. The phrase “delete a node” is intentionally ambiguous. Before proposing an algorithm, clarify the depth convention, whether the root may be deleted, what happens to a deleted node's descendants, what “minimum” counts, and whether nodes already within the allowed prefix must be preserved. Do not assume that two different deletion contracts have the same optimum. ### Constraints & Assumptions - The input is a valid rooted tree with no cycles or shared children. - The tree may be empty unless the interviewer rules that out. - An iterative traversal should be available for trees too deep for recursion. - The interviewer requests non-DP reasoning, so exploit the structure implied by each clarified contract. ### Clarifying Questions to Ask - Is root depth measured as zero or one? - Does deleting a node remove its entire subtree, or are its children promoted to its parent? - Is cost the number of removed original nodes or the number of explicit delete operations? - Must every node at depth at most `k` be retained? - May the root be deleted, and what should be returned when `k` is below the root's depth? ### Part 1 - Compute Maximum Depth Give an iterative or recursive algorithm that returns the maximum depth under a stated convention. Explain the empty-tree result and the complexity. #### What This Part Should Cover - States whether root depth is zero or one, visits every node once, and avoids hidden assumptions about a binary-tree shape. ### Part 2 - Compare Deletion Interpretations Analyze at least these two reasonable contracts: 1. **Minimum removed nodes with prefix preservation:** deleting a selected node removes its whole subtree, the root remains, and every original node at depth at most `k` must remain. 2. **Minimum subtree-cut operations without prefix preservation:** deleting a selected non-root node removes its whole subtree, each selected subtree root costs one operation, and it is legal to sacrifice nodes whose depths were already within the limit. For each contract, characterize what an optimal result looks like. Also explain why allowing root deletion or promoting children would materially change the problem. #### What This Part Should Cover - Separates removed-node cost from operation cost, identifies degenerate cases, and does not present one interpretation as the reported interview's confirmed rule. ### Part 3 - Give Non-DP Algorithms For both contracts above, provide a traversal-based algorithm, an optimality argument, output format, and time and space complexity. The output may include both the selected cut roots and the total number of original nodes removed so the two cost definitions remain visible. #### What This Part Should Cover - Uses depth or subtree-height information directly, explains why dynamic programming is unnecessary under the stated contracts, and handles `k`, empty-tree, and root-only edge cases. ### What a Strong Answer Covers - Clarifies semantics before coding and maintains one depth convention throughout. - Computes N-ary tree depth in linear time. - Gives separate, correct optima for at least two deletion-cost models. - Proves necessity and sufficiency instead of relying on an unexplained greedy rule. - Calls out when a changed deletion model would require a different algorithm. ### Follow-up Questions 1. How would the answer change if deleting a node promoted its children to the deleted node's parent? 2. How would weighted deletion costs affect the non-DP conclusions? 3. How could maximum depth be maintained while leaves are inserted and removed online?

Quick Answer: Clarify an ambiguous N-ary tree task involving maximum depth and minimum deletion under a depth limit. Compare distinct deletion-cost contracts, preserve a consistent depth convention, handle empty and root-only trees, and justify correctness and complexity without assuming one interpretation.

Related Interview Questions

  • Design a Thread-Safe Multi-Rule Rate Limiter - Snowflake (hard)
  • Explain React classes, lifecycle, and hooks - Snowflake (medium)
|Home/Software Engineering Fundamentals/Snowflake

Clarify and Prune an N-ary Tree to a Depth Limit

Snowflake logo
Snowflake
Jun 20, 2026, 12:00 AM
hardSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

You are given a rooted N-ary tree and a nonnegative integer k. First compute the tree's maximum depth. Then discuss how to delete a minimum set of nodes so the resulting tree has maximum depth at most k.

The phrase “delete a node” is intentionally ambiguous. Before proposing an algorithm, clarify the depth convention, whether the root may be deleted, what happens to a deleted node's descendants, what “minimum” counts, and whether nodes already within the allowed prefix must be preserved. Do not assume that two different deletion contracts have the same optimum.

Constraints & Assumptions

  • The input is a valid rooted tree with no cycles or shared children.
  • The tree may be empty unless the interviewer rules that out.
  • An iterative traversal should be available for trees too deep for recursion.
  • The interviewer requests non-DP reasoning, so exploit the structure implied by each clarified contract.

Clarifying Questions to Ask Guidance

  • Is root depth measured as zero or one?
  • Does deleting a node remove its entire subtree, or are its children promoted to its parent?
  • Is cost the number of removed original nodes or the number of explicit delete operations?
  • Must every node at depth at most k be retained?
  • May the root be deleted, and what should be returned when k is below the root's depth?

Part 1 - Compute Maximum Depth

Give an iterative or recursive algorithm that returns the maximum depth under a stated convention. Explain the empty-tree result and the complexity.

What This Part Should Cover Guidance

  • States whether root depth is zero or one, visits every node once, and avoids hidden assumptions about a binary-tree shape.

Part 2 - Compare Deletion Interpretations

Analyze at least these two reasonable contracts:

  1. Minimum removed nodes with prefix preservation: deleting a selected node removes its whole subtree, the root remains, and every original node at depth at most k must remain.
  2. Minimum subtree-cut operations without prefix preservation: deleting a selected non-root node removes its whole subtree, each selected subtree root costs one operation, and it is legal to sacrifice nodes whose depths were already within the limit.

For each contract, characterize what an optimal result looks like. Also explain why allowing root deletion or promoting children would materially change the problem.

What This Part Should Cover Guidance

  • Separates removed-node cost from operation cost, identifies degenerate cases, and does not present one interpretation as the reported interview's confirmed rule.

Part 3 - Give Non-DP Algorithms

For both contracts above, provide a traversal-based algorithm, an optimality argument, output format, and time and space complexity. The output may include both the selected cut roots and the total number of original nodes removed so the two cost definitions remain visible.

What This Part Should Cover Guidance

  • Uses depth or subtree-height information directly, explains why dynamic programming is unnecessary under the stated contracts, and handles k , empty-tree, and root-only edge cases.

What a Strong Answer Covers Guidance

  • Clarifies semantics before coding and maintains one depth convention throughout.
  • Computes N-ary tree depth in linear time.
  • Gives separate, correct optima for at least two deletion-cost models.
  • Proves necessity and sufficiency instead of relying on an unexplained greedy rule.
  • Calls out when a changed deletion model would require a different algorithm.

Follow-up Questions Guidance

  1. How would the answer change if deleting a node promoted its children to the deleted node's parent?
  2. How would weighted deletion costs affect the non-DP conclusions?
  3. How could maximum depth be maintained while leaves are inserted and removed online?
Loading comments...

Browse More Questions

More Software Engineering Fundamentals•More Snowflake•More Software Engineer•Snowflake Software Engineer•Snowflake Software Engineering Fundamentals•Software Engineer Software Engineering Fundamentals

Write your answer

Your first approved answer each day earns 20 XP.

Sign in to write your answer.
PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.