← Snowflake Interview Insights

Snowflake·Software Engineer·Technical Phone Screen·Senior

Senior
Jun 2026

Summary

Snowflake SWE technical phone screen with a tree problem that looked manageable until the deleted-node splicing requirement showed up. Single coding question, pretty focused session.

Questions Asked (1)

Q1

Given an n-ary tree where each node has a boolean 'deleted' flag, compute the maximum height of the effective tree after splicing out all deleted nodes. A deleted node's children get re-attached to its nearest non-deleted ancestor. The root itself may be deleted, and chains of consecutive deleted nodes should collapse entirely. Solve this in a single DFS.

Algorithms & Data Structures
Author's notes

My first instinct was to build the effective tree explicitly and then measure height separately.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a single post-order DFS that returns the height of the effective subtree rooted at each node, considering deletions. When a node is deleted, return the maximum height among its children's effective subtrees (without adding 1), effectively splicing them to the nearest non-deleted ancestor. For non-deleted nodes, return 1 plus the maximum height from children.

Pro tip: Clarify that the height of a tree with no nodes is -1 (or 0, depending on convention) and handle the case where the root is deleted and all nodes are deleted. Also, mention that the DFS should be iterative to avoid stack overflow for deep trees, but recursive is acceptable if the interviewer allows.

1. Understand the problem and define height

Confirm the definition of height (number of edges or nodes) and the splicing behavior. Clarify that deleted nodes are removed and their children attach to the nearest non-deleted ancestor.

2. Design the DFS function

Define a recursive function that returns the effective height of the subtree rooted at the current node. For a deleted node, return the maximum height among its children's results (no +1). For a non-deleted node, return 1 + max(children's results).

3. Handle base cases and edge cases

If a node has no children, return 0 if non-deleted, or -1 if deleted (to indicate no contribution). Handle the case where the root is deleted and all nodes are deleted, returning -1 or 0 as appropriate.

4. Implement and test with examples

Write the code, then walk through a small example with a deleted chain to verify correctness. Consider iterative DFS if recursion depth is a concern.

5. Analyze complexity and discuss optimizations

State that the time complexity is O(n) and space is O(h) for recursion stack. Mention that no extra data structures are needed beyond the recursion.

Key Points to Mention

  • Post-order traversal to compute heights bottom-up.
  • Deleted nodes do not add 1 to height; they pass up the max child height.
  • Non-deleted nodes add 1 to the max child height.
  • Base case: leaf node returns 0 if not deleted, -1 if deleted.
  • Root deletion is handled naturally by the same logic.
  • Time complexity O(n), space O(h) for recursion stack.

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