← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026Remote

Summary

Snowflake SWE interview with two tree problems back to back, both built around the same unusual deletion rule where you promote children up instead of just removing the node. The warm-up felt manageable but the main problem had enough moving parts that I was scrambling by the end.

Questions Asked (2)

Q1

You have a rooted binary tree with unique node IDs and a list of nodes to delete. When you delete a node, its children get promoted to fill its spot under the deleted node's parent, preserving left/right positions where possible. After all deletions, how many levels does the resulting tree have? Also handle the case where the root itself gets deleted, which produces a forest, and return the max height across all resulting trees. What's the time and space complexity?

Algorithms & Data Structures
Author's notes

The promotion rule is what makes this feel different from a standard delete question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the deletion semantics and root-deletion behavior, then propose a post-order traversal that computes the height of each subtree after deletions, handling promoted children and forest cases. Discuss time and space complexity, aiming for O(n) time and O(h) space.

Pro tip: Mention that deletions can be processed in a single post-order traversal without physically modifying the tree, by treating deleted nodes as transparent and promoting their children. This shows you can optimize both time and space.

1. Clarify the problem

Ask questions to confirm: Are node IDs unique? What does 'preserving left/right positions' mean exactly? If a deleted node has two children, do they both get promoted, and how are they ordered? What if the root is deleted—does each child become a root of a new tree?

2. Define the recursive approach

Design a post-order traversal that returns the height of the subtree rooted at the current node after deletions. If the current node is deleted, combine the heights of its children appropriately (e.g., if both children exist, the promoted subtree's height is max(leftHeight, rightHeight) + 1? Actually, need to think: when a node is deleted, its children are promoted to its parent. So the height contribution from that subtree is the maximum of the heights of its children, but if both children exist, they become siblings under the parent, so the height is max(leftHeight, rightHeight) + 1? Wait, careful: The height of a tree is the number of nodes on the longest path from root to leaf. If a node is deleted, its children take its place. So if the node had two children, they both become children of the parent, so the height of the subtree rooted at the parent will be 1 + max(height of left child's subtree, height of right child's subtree) but note that the children are now directly attached to the parent, so the edge from parent to child counts. So if the deleted node had children, the height contributed is max(height(left), height(right)) + 1? Actually, if the deleted node is at depth d from the parent, and it had children, those children are now at depth d+1 from the parent? Let's think: parent -> deleted node -> child. After deletion, parent -> child. So the child moves up one level. So the height of the subtree rooted at the parent is 1 + max(height of child subtrees). But if the deleted node had no children, it contributes 0. So the recursive function should return the height of the subtree rooted at the current node after deletions, considering that if the current node is deleted, we need to merge its children's heights. However, if the current node is deleted, we cannot simply return max(left, right) because the children become siblings under the parent, so the height from the parent's perspective is 1 + max(left, right) if at least one child exists, else 0. But the function returns the height of the subtree rooted at the current node. If the current node is deleted, the subtree rooted at the current node is effectively replaced by its children, so the height of that 'subtree' (now a forest of up to two trees) is max(left, right). But when attached to the parent, the parent will add 1. So the function should return the height of the forest resulting from the current node's subtree if the current node is deleted. That is, if deleted, return max(height(left), height(right)) (or 0 if no children). If not deleted, return 1 + max(height(left), height(right)). This works because if not deleted, the node itself adds 1 to the height. If deleted, the node is removed, so the height is just the max of the children's heights. But careful: if the node is deleted and has two children, they both become children of the parent, so the height from the parent's perspective is 1 + max(left, right). But the function returns the height of the subtree rooted at the current node after deletion, which is max(left, right) because the current node is gone. Then the parent will add 1 if the parent is not deleted. So this is consistent.

3. Handle root deletion and forest

If the root is deleted, the result is a forest of up to two trees (the root's children). The overall height is the maximum height among these trees. So after processing the root, if it is deleted, return max(height(left), height(right)) as the final answer; otherwise return 1 + max(height(left), height(right)).

4. Analyze complexity

Time complexity is O(n) because each node is visited once. Space complexity is O(h) for the recursion stack, where h is the height of the original tree (worst case O(n) for skewed tree).

5. Discuss edge cases

Consider empty tree, deleting all nodes, deleting a leaf, deleting a node with one child, and deleting the root. Also consider if the tree is a single node and it is deleted—resulting forest is empty, so height 0.

Key Points to Mention

  • Post-order traversal to compute heights bottom-up.
  • Handling deleted nodes by promoting children and adjusting height calculation.
  • Root deletion produces a forest; take max height of resulting trees.
  • Time complexity O(n), space complexity O(h) due to recursion.
  • Edge cases: empty tree, deleting all nodes, deleting root with one child.
  • Clarify assumptions about promotion order when both children exist.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Using the same child-promotion deletion rule, given a rooted binary tree, a max number of deletions n, and a target height k, return all unique sets of node IDs whose deletion results in a tree of exactly height k using at most n deletions. Each set should be returned as a sorted list, with no duplicate sets in the output. Walk through your search and pruning strategy, argue correctness, and analyze complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got hard.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the child-promotion deletion rule and confirm assumptions about the tree structure and deletion semantics. Then, design a recursive search that explores all valid deletion sets, using pruning based on current height and remaining deletions, and deduplicate results by canonicalizing sets. Finally, analyze correctness and complexity, discussing trade-offs between exhaustive search and optimizations.

Pro tip: Explicitly state your assumptions about the deletion rule and tree properties upfront; this shows you can handle ambiguity and sets the stage for a focused solution. Also, mention that you would test with small trees to validate the rule before scaling up.

1. Clarify the problem and constraints

Restate the child-promotion deletion rule, confirm tree properties (e.g., node IDs unique, binary tree), and define what 'height' means (e.g., number of edges on longest root-to-leaf path). Ask clarifying questions if needed.

2. Design a recursive search with pruning

Use DFS to explore deleting or keeping each node, tracking current height and deletions used. Prune branches where height already exceeds k or deletions exceed n, and where remaining nodes cannot achieve height k.

3. Ensure uniqueness and sorted output

Store each valid deletion set in a hash set after sorting the node IDs to avoid duplicates. At the end, convert the set to a list of sorted lists.

4. Argue correctness

Explain that the search exhaustively considers all subsets of nodes up to size n, and pruning only removes branches that cannot lead to a valid solution. Thus, every valid set is found exactly once.

5. Analyze complexity and discuss trade-offs

State worst-case time complexity O(2^m * m) where m is number of nodes, but pruning reduces practical runtime. Space complexity O(2^m) for storing results. Mention potential optimizations like memoization on subtree states.

Key Points to Mention

  • Definition of child-promotion deletion: when a node is deleted, its children are promoted to its parent, preserving binary tree structure.
  • Height definition: typically number of edges on longest path from root to leaf; clarify if nodes count instead.
  • Pruning conditions: current height > k, deletions > n, or even with all remaining nodes deleted height cannot be reduced to k.
  • Deduplication: use a set of sorted tuples to ensure unique sets.
  • Complexity: exponential in worst case due to subset enumeration, but pruning and small n can make it feasible.
  • Trade-offs: exhaustive search vs. heuristic or DP approaches; discuss when each is appropriate.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.