← Create New Paste

Topological Sort in Coding Interviews: Recognizing and Solving Dependency Problems

6 views
Topological sort shows up in coding interviews more often than candidates expect, usually disguised as a scheduling or ordering problem: course prerequisites, build systems, task dependencies, or reconstructing an alphabet from a sorted list of words. Once you learn to spot the pattern, these problems become some of the most predictable on the list. Recognizing the pattern Look for these signals in the problem statement: - Items must happen "before" or "after" other items. - You are asked for a valid order, or whether any valid order exists. - Cycles would make the task impossible. If you can phrase the input as "A must come before B," you have a directed graph, and the question is about ordering its nodes. Building the graph Turn each dependency into an edge. For course prerequisites given as [course, prereq], add an edge from prereq to course. Use an adjacency list (a dictionary of lists) and an in-degree count for each node. Getting the edge direction wrong is the single most common bug, so say it out loud: "an edge from X to Y means X must come first." Kahn's algorithm (BFS) 1. Compute the in-degree of every node. 2. Put every node with in-degree zero into a queue. 3. Pop a node, append it to the result, and decrement the in-degree of each neighbor. When a neighbor reaches zero, push it. 4. When the queue is empty, if the result contains every node, it is a valid order. Otherwise there is a cycle. This version is easy to explain and makes cycle detection natural, which is why many interviewers prefer it. DFS approach Alternatively, run DFS and add each node to a list after visiting all its descendants, then reverse the list. Cycle detection needs three states: unvisited, visiting, and done. Reaching a "visiting" node means a back edge, so a cycle exists. The DFS version is compact but recursion depth can be an issue in Python for large inputs; mention this trade-off. Complexity Both approaches run in O(V + E) time and space. State that clearly, since it is what interviewers listen for. Classic problems to practice - Course Schedule I and II: can you finish all courses, and in what order? - Alien Dictionary: compare adjacent words to find the first differing character, which gives one edge. Watch for the edge case where a longer word appears before its own prefix, which makes the input invalid. - Parallel Courses: use BFS level by level to find the minimum number of semesters. - Sequence Reconstruction: check whether the topological order is unique by confirming the queue never holds more than one node. Follow-up questions - What if you need the lexicographically smallest order? Replace the queue with a min-heap. - What if the graph is huge and changes over time? Discuss incremental approaches and caching. - How would you report which tasks are part of a cycle? Nodes never reaching in-degree zero are in or downstream of a cycle. Communicating under pressure Graph problems reward narration: describe the graph, the edge direction, and the algorithm before writing code, then trace a small example. If you tend to blank when an interviewer is watching, Linkjob AI (https://linkjob.ai) provides real-time help during job interviews and technical interviews, so you can keep your reasoning structured while you code.
Created Nov 14, 2026 19:00 Expires Nov 14, 2027 19:00