Interview concept

What is topological sort?

Topological sort produces a linear ordering of a directed acyclic graph so that every directed edge goes from earlier to later in the order. It resolves dependencies such as build steps, course prerequisites, and task scheduling.

Interview concept

How it works

Kahn algorithm

Repeatedly remove nodes with no remaining incoming edges, using in-degree counts and a queue.

DFS approach

Run depth-first search and push each node onto a stack after visiting its descendants, then reverse the stack.

Cycle detection

If no valid ordering exists, the graph has a cycle, so topological sort also detects cycles.

Interview concept

Responsible use

Use any live AI assistant only where interview, workplace, school, and platform rules allow it. Do not use generated answers to misrepresent your skills, experience, or authorship.

FAQ

Common questions.

When is topological sort possible?

Only on a directed acyclic graph; if the graph has a cycle, no valid ordering exists.

What problems use topological sort?

Build systems, course scheduling, task ordering, and general dependency resolution.