← Snowflake Interview Insights

Snowflake·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Two coding problems back to back for a Snowflake SWE round. One graph problem, one tree traversal. Nothing too exotic but the boundary tree one had enough edge cases to slow me down.

Questions Asked (2)

Q1

Given n courses and a list of prerequisite pairs, return any valid ordering of all courses. If a cycle exists, return an empty list.

Algorithms & Data Structures
Author's notes

Classic topological sort.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph, then perform a topological sort using either Kahn's algorithm (BFS with in-degree tracking) or DFS with cycle detection. If a cycle is detected, return an empty list; otherwise, return the topological ordering.

Pro tip: Mention that Kahn's algorithm naturally detects cycles when the number of processed nodes is less than n, and that it avoids recursion depth issues. Also, clarify that any valid topological order is acceptable, so you don't need to worry about a specific ordering.

1. Clarify and Model the Problem

Confirm that the input is a list of prerequisite pairs where [a, b] means b must be taken before a. Represent the courses as nodes and prerequisites as directed edges from b to a.

2. Choose an Algorithm

Decide between Kahn's algorithm (BFS with in-degree) or DFS with cycle detection. Both are O(V+E) time and space; choose the one you are most comfortable implementing.

3. Implement Topological Sort

For Kahn's: compute in-degrees, enqueue nodes with in-degree 0, then repeatedly dequeue and reduce in-degrees of neighbors. For DFS: perform DFS and add nodes to the result in post-order, while tracking visited and recursion stack to detect cycles.

4. Detect Cycles and Return Result

If using Kahn's, check if the result size equals n; if not, a cycle exists, so return []. If using DFS, if a back edge is found, return []. Otherwise, return the topological order.

5. Analyze Complexity and Edge Cases

State that the time complexity is O(V+E) and space is O(V+E). Discuss edge cases: no prerequisites, disconnected graph, self-loop, and multiple valid orderings.

Key Points to Mention

  • Graph representation: adjacency list for efficiency.
  • Kahn's algorithm: in-degree array, queue, and cycle detection via count.
  • DFS approach: visited set, recursion stack for cycle detection, and post-order insertion.
  • Time and space complexity: O(V+E) where V is number of courses and E is number of prerequisites.
  • Handling cycles: return empty list if cycle detected.
  • Any valid topological order is acceptable; no need to sort lexicographically unless specified.

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

Q2

Given the root of a binary tree, return the boundary nodes in anti-clockwise order: root, left boundary (no leaves), all leaves left to right, then right boundary (no leaves) bottom-up.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one took me longer than I wanted.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Break the problem into three distinct traversals: left boundary (excluding leaves), all leaves left-to-right, and right boundary (excluding leaves) collected bottom-up. Implement each traversal with careful condition checks to avoid duplicates and ensure correct ordering, then concatenate the results.

Pro tip: Clarify edge cases upfront, such as a single-node tree or skewed trees, and mention that the root is included only once even if it's also a leaf. This shows attention to detail and prevents off-by-one errors.

1. Clarify and define boundaries

Confirm the exact definition of boundary nodes, especially edge cases like single-node trees and skewed trees. State that the root is always included, and leaves are only included in the leaves traversal.

2. Collect left boundary

Traverse from root's left child down the left side, adding nodes that are not leaves. Use a top-down approach, moving to left child if it exists, otherwise right child.

3. Collect all leaves left-to-right

Perform a DFS (preorder) traversal, adding nodes that have no children. This ensures leaves are collected in left-to-right order.

4. Collect right boundary bottom-up

Traverse from root's right child down the right side, adding non-leaf nodes, but store them in a stack or reverse the list to get bottom-up order.

5. Combine and handle edge cases

Concatenate the three lists, ensuring no duplicates (e.g., root not added twice). Test with edge cases like empty tree, single node, and skewed trees.

Key Points to Mention

  • Time complexity O(n) and space complexity O(n) due to recursion stack and output storage.
  • Avoiding duplicates: root is added once; leaves are not included in left/right boundary traversals.
  • Handling edge cases: empty tree, single node, left-skewed or right-skewed trees.
  • Using iterative vs recursive approaches and trade-offs (e.g., recursion depth for skewed trees).
  • Order of traversal: left boundary top-down, leaves left-to-right, right boundary bottom-up.
  • Clarifying that leaves are only those nodes with no children, and the root may be a leaf if it's the only node.

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