⚡️ Tryprepforge
Backend Engineer Senior programming

You are given a directed acyclic graph (DAG) represented as an adjacency list. Your task is to return a topological sorting of the graph. If the graph is not a DAG, return an empty list. **Function Signature:** `def topological_sort(graph: List[List[int]]) -> List[int]:` **Constraints:** - The number of vertices in the graph will be between 1 and 10^5. - The number of edges will be between 0 and 10^5. **Example 1:** Input: `graph = [[1, 2], [3], [3], []]` Output: `[0, 1, 2, 3]` **Example 2:** Input: `graph = [[1], [2], [0]]` Output: `[]` (Graph contains a cycle)

Suggested Answer

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

No credit card required

More Backend Engineer Interview Prep

Link copied to clipboard