← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Snowflake coding round, one tree problem the whole time. Pretty focused session, nothing crazy on the surface but the edge cases sneak up on you.

Questions Asked (1)

Q1

Given a tree with a target node X, return the height of the tree after removing X and its entire subtree. Height is defined as the number of edges on the longest root-to-leaf path.

Algorithms & Data Structures
Author's notes

I jumped straight to DFS and felt pretty good about it, but then realized mid-explanation I wasn't handling the case where X is the root cleanly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the tree is rooted and that removing X means detaching the entire subtree rooted at X. Then compute the height of the remaining tree by finding the maximum depth of all nodes except those in X's subtree, which can be done with a single DFS/BFS while skipping X's subtree.

Pro tip: Mention that if X is the root, the remaining tree is empty and height is typically defined as -1 or 0; state your assumption explicitly and ask the interviewer to confirm.

1. Clarify definitions and edge cases

Confirm that the tree is rooted, that removing X includes its entire subtree, and how height is defined for an empty tree. Discuss edge cases like X being the root or a leaf.

2. Choose traversal strategy

Decide between DFS (recursive or iterative) and BFS. DFS is natural for computing heights, but BFS can also work by tracking levels.

3. Compute height while skipping X's subtree

Traverse the tree from the root, but when you encounter X, do not recurse into it. Track the maximum depth reached among all other nodes.

4. Handle special cases

If X is the root, return the defined height for an empty tree (e.g., -1 or 0). If X is not found, the height is the original tree height.

5. Analyze complexity and test

State that the time complexity is O(n) and space is O(h) for DFS or O(n) for BFS. Walk through a small example to verify correctness.

Key Points to Mention

  • Tree traversal (DFS/BFS) and recursion/stack usage
  • Height definition and how it changes when a subtree is removed
  • Edge cases: X is root, X is leaf, X not present
  • Time and space complexity analysis
  • Handling of empty tree after removal
  • Potential for iterative vs recursive solutions

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