Implement topological sort and tree boundary traversal
Problem A: Course Scheduling with Prerequisites
You are given n courses, labeled 0 to n-1, and a list of prerequisites. Each prerequisite pair [a, b] indicates that to take course a, you must first complete course b.
Task: Return any valid ordering of all courses that satisfies these prerequisites. If a valid ordering is impossible due to a circular dependency (a cycle), return an empty list.
Input:
n: An integer representing the total number of courses.prerequisites: A list of pairs, where each pair[a, b]signifies thatbis a prerequisite fora.
Output:
A list of length n representing a valid course order, or an empty list [] if no such order exists.
Constraints:
1 <= n <= 10^50 <= len(prerequisites) <= 2*10^5
Notes:
- Implement the algorithm yourself (e.g., using topological sort).
- Be prepared to write a few basic tests, including cases with cycles and disconnected graphs.
Problem B: Binary Tree Boundary Traversal
You are given the root of a binary tree. The tree is guaranteed to be complete and balanced for the interview variant, but your solution should ideally work for any general binary tree.
Define the boundary of the tree in anti-clockwise order as:
- The root node (included once).
- The left boundary (excluding leaf nodes): Starting from
root.leftand moving downwards, always taking the next boundary node. - All leaf nodes: Traversed from left to right.
- The right boundary (excluding leaf nodes): Starting from
root.rightand moving downwards, collected top-down but output bottom-up.
Task: Return a list of node values representing the boundary traversal, ensuring no duplicate values are included in the final output.
Input:
root: The root node of a binary tree.
Output:
A list of integers representing the boundary traversal.
Edge Cases to Consider:
- A single-node tree.
- A root node with only one child.
- Trees where left or right boundary paths might encounter missing children (even if the interview variant specifies a complete/balanced tree).
Complexity Target:
- Time:
O(N) - Space:
O(H)(for recursion stack) orO(N)in the worst case, depending on implementation.
AI interview prep
Core dilemma
Efficiently applying and adapting fundamental graph and tree traversal algorithms to satisfy specific ordering and structural requirements, while robustly handling edge cases, cycles, and optimizing for time and space complexity.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement topological sort and tree boundary traversal
Problem A: Course Scheduling with Prerequisites
You are given n courses, labeled 0 to n-1, and a list of prerequisites. Each prerequisite pair [a, b] indicates that to take course a, you must first complete course b.
Task: Return any valid ordering of all courses that satisfies these prerequisites. If a valid ordering is impossible due to a circular dependency (a cycle), return an empty list.
Input:
n: An integer representing the total number of courses.prerequisites: A list of pairs, where each pair[a, b]signifies thatbis a prerequisite fora.
Output:
A list of length n representing a valid course order, or an empty list [] if no such order exists.
Constraints:
1 <= n <= 10^50 <= len(prerequisites) <= 2*10^5
Notes:
- Implement the algorithm yourself (e.g., using topological sort).
- Be prepared to write a few basic tests, including cases with cycles and disconnected graphs.
Problem B: Binary Tree Boundary Traversal
You are given the root of a binary tree. The tree is guaranteed to be complete and balanced for the interview variant, but your solution should ideally work for any general binary tree.
Define the boundary of the tree in anti-clockwise order as:
- The root node (included once).
- The left boundary (excluding leaf nodes): Starting from
root.leftand moving downwards, always taking the next boundary node. - All leaf nodes: Traversed from left to right.
- The right boundary (excluding leaf nodes): Starting from
root.rightand moving downwards, collected top-down but output bottom-up.
Task: Return a list of node values representing the boundary traversal, ensuring no duplicate values are included in the final output.
Input:
root: The root node of a binary tree.
Output:
A list of integers representing the boundary traversal.
Edge Cases to Consider:
- A single-node tree.
- A root node with only one child.
- Trees where left or right boundary paths might encounter missing children (even if the interview variant specifies a complete/balanced tree).
Complexity Target:
- Time:
O(N) - Space:
O(H)(for recursion stack) orO(N)in the worst case, depending on implementation.