← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Snowflake SWE interview with a tree problem that had a decent follow-up. The core question was manageable but the follow-up pushed into greedy/DP territory which is where things got more interesting.

Questions Asked (2)

Q1

You're given a rooted tree where some nodes are marked as deleted. A deleted node and everything below it that's now disconnected from the root is removed. Compute the height of the resulting tree, where height is the max number of nodes on any root-to-leaf path. If the root itself is deleted, return 0.

Algorithms & Data Structures
Author's notes

Pretty standard DFS once you frame it right.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the height of the valid subtree rooted at each node, treating deleted nodes as contributing height 0 and pruning their descendants. At each node, compute 1 + max(height of valid children), but if the node itself is deleted, return 0 immediately. The answer is the height returned for the root, or 0 if the root is deleted.

Pro tip: Clarify the definition of 'deleted' and whether the tree is given as an adjacency list or parent array; then explicitly state that you prune at deleted nodes to avoid traversing disconnected subtrees, which keeps the solution O(n).

1. Clarify the problem and input format

Confirm that a deleted node removes its entire subtree, height counts nodes on the longest root-to-leaf path, and the root deletion returns 0. Ask how the tree is represented (e.g., adjacency list, parent array) and whether deleted nodes are marked in a boolean array.

2. Choose the right traversal

Select a post-order DFS because a node's height depends on the heights of its valid children. This naturally handles pruning: when a node is deleted, return 0 without recursing into its children.

3. Define the recursive function

Write a function height(node) that returns 0 if node is deleted; otherwise, compute 1 + max(height(child) for each child). If a child is deleted, its height is 0 and it contributes nothing to the path.

4. Handle edge cases and base cases

If the root is deleted, return 0 immediately. For a leaf node that is not deleted, height is 1. Ensure that deleted nodes do not propagate their descendants' heights upward.

5. Analyze complexity and test

State that the algorithm visits each node at most once, so time is O(n) and space is O(h) for recursion (or O(n) worst case). Walk through a small example with a deleted internal node to verify pruning.

Key Points to Mention

  • Post-order DFS is ideal because height is computed bottom-up from children.
  • Deleted nodes act as barriers: return 0 and do not recurse into their children.
  • Height is defined as number of nodes, so a single node has height 1.
  • If the root is deleted, the entire tree is removed, so return 0.
  • Time complexity is O(n) because each node is processed once; space is O(h) for recursion stack.
  • Edge cases: empty tree, all nodes deleted, only root deleted, deep skewed tree.

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

Q2

Follow-up: given an integer K, what is the minimum number of additional nodes you need to delete so that the height of the effective tree is at most K? You can delete any nodes you want.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I fumbled a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding a set of nodes to delete so that the remaining tree has height ≤ K, minimizing deletions. Use dynamic programming on the tree: for each node, compute the minimum deletions in its subtree for each possible height, then combine children by keeping at most K levels. The answer is the minimum deletions for the root with height ≤ K.

Pro tip: Clarify that 'effective tree' means the tree after deletions, and note that deleting a node also removes its entire subtree. This shows you understand the problem's constraints and avoids misinterpretation.

1. Clarify the problem

Confirm that deleting a node removes its entire subtree, and that the height is measured in edges or nodes. Ask if K is given as a non-negative integer.

2. Define DP state

For each node u, let dp[u][h] be the minimum number of deletions in the subtree rooted at u such that the resulting subtree has height at most h. h ranges from 0 to K.

3. Derive recurrence

For a leaf, dp[u][h] = 0 for all h ≥ 0. For an internal node, either delete u (cost = size of subtree) or keep u and combine children: for each child v, we need dp[v][h-1] if we keep the edge, but we can also delete the child (cost = size of v's subtree). The recurrence is dp[u][h] = min( size(u), sum over children of min( dp[v][h-1], size(v) ) ).

4. Compute bottom-up

Perform a post-order traversal to compute subtree sizes and dp values for all nodes and heights from 0 to K. Use memoization or iterative DP.

5. Return answer

The minimum number of additional deletions needed is dp[root][K]. If the original height is already ≤ K, the answer is 0.

Key Points to Mention

  • Dynamic programming on trees with state (node, height)
  • Subtree sizes precomputed to handle deletion cost
  • Time complexity O(N*K) where N is number of nodes
  • Space complexity O(N*K) or optimized to O(N) with careful iteration
  • Handling of base cases: leaves and K=0
  • Comparison with greedy approaches and why DP is necessary

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