PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Snowflake

Implement topological sort and tree boundary traversal

Last updated: Mar 29, 2026

Quick Overview

These tasks evaluate core competencies in directed graph algorithms (topological ordering and cycle detection) and binary tree traversals (boundary identification and leaf enumeration), testing understanding of dependency modeling and traversal patterns.

  • medium
  • Snowflake
  • Coding & Algorithms
  • Software Engineer

Implement topological sort and tree boundary traversal

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given two separate coding tasks. ## Problem A — Order courses with prerequisites You have `n` courses labeled `0..n-1` and a list of prerequisite pairs `prerequisites`, where each pair `[a, b]` means **to take course `a`, you must first take course `b`**. **Task:** Return **any valid ordering** of all courses that satisfies prerequisites. If it is impossible (because of a cycle), return an **empty list**. **Input:** - `n` (integer) - `prerequisites` (list of pairs) **Output:** - A list of length `n` representing a valid order, or `[]` if no order exists. **Constraints (typical):** - `1 ≤ n ≤ 10^5` - `0 ≤ len(prerequisites) ≤ 2*10^5` **Notes:** - Implement the algorithm yourself (e.g., topological sort). - Be prepared to write a few basic tests (e.g., cycle case, disconnected graph). --- ## Problem B — Boundary traversal of a (complete/balanced) binary tree You are given the root of a binary tree. The tree is guaranteed to be **complete and balanced** (interview variant constraint), but your solution may work for any binary tree. Define the **boundary** of the tree in **anti-clockwise** order as: 1. The **root** (once). 2. The **left boundary** (excluding leaves): from `root.left` going downward, always taking the next boundary node. 3. All **leaf nodes** from left to right. 4. The **right boundary** (excluding leaves): from `root.right` going downward, collected top-down but output **bottom-up**. **Task:** Return a list of node values in boundary order, with **no duplicates**. **Input:** - `root` of a binary tree **Output:** - List of integers representing the boundary traversal. **Edge cases to consider:** - Single-node tree - Root has only one child - Trees where left/right boundary paths include missing children (even if the interview variant says complete) **Complexity target:** - Time `O(N)`, space `O(H)` (recursion) or `O(N)` worst case depending on implementation.

Quick Answer: These tasks evaluate core competencies in directed graph algorithms (topological ordering and cycle detection) and binary tree traversals (boundary identification and leaf enumeration), testing understanding of dependency modeling and traversal patterns.

Related Interview Questions

  • Solve Array Distance and Wiki Navigation - Snowflake (medium)
  • Implement Document Predicate APIs - Snowflake (medium)
  • Find Shortest Wiki Click Path - Snowflake (medium)
  • Schedule prerequisite classes with retakes - Snowflake (easy)
  • Solve three coding rounds - Snowflake (medium)
Snowflake logo
Snowflake
Feb 12, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
24
0

You are given two separate coding tasks.

Problem A — Order courses with prerequisites

You have n courses labeled 0..n-1 and a list of prerequisite pairs prerequisites, where each pair [a, b] means to take course a, you must first take course b.

Task: Return any valid ordering of all courses that satisfies prerequisites. If it is impossible (because of a cycle), return an empty list.

Input:

  • n (integer)
  • prerequisites (list of pairs)

Output:

  • A list of length n representing a valid order, or [] if no order exists.

Constraints (typical):

  • 1 ≤ n ≤ 10^5
  • 0 ≤ len(prerequisites) ≤ 2*10^5

Notes:

  • Implement the algorithm yourself (e.g., topological sort).
  • Be prepared to write a few basic tests (e.g., cycle case, disconnected graph).

Problem B — Boundary traversal of a (complete/balanced) binary tree

You are given the root of a binary tree. The tree is guaranteed to be complete and balanced (interview variant constraint), but your solution may work for any binary tree.

Define the boundary of the tree in anti-clockwise order as:

  1. The root (once).
  2. The left boundary (excluding leaves): from root.left going downward, always taking the next boundary node.
  3. All leaf nodes from left to right.
  4. The right boundary (excluding leaves): from root.right going downward, collected top-down but output bottom-up .

Task: Return a list of node values in boundary order, with no duplicates.

Input:

  • root of a binary tree

Output:

  • List of integers representing the boundary traversal.

Edge cases to consider:

  • Single-node tree
  • Root has only one child
  • Trees where left/right boundary paths include missing children (even if the interview variant says complete)

Complexity target:

  • Time O(N) , space O(H) (recursion) or O(N) worst case depending on implementation.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Snowflake•More Software Engineer•Snowflake Software Engineer•Snowflake Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.