Limited Time Offer:Up to 20% off Hello Interview Premium
Up to 20% off Hello Interview Premium 🎉
Hello Interview
Learn Code
Introduction
Overview
Container With Most Water
Two Sum (Sorted Array)
3-Sum
Triangle Numbers
Move Zeroes
Sort Colors
Trapping Rain Water
Overview
Maximum Sum of Subarrays of Size K
Max Points You Can Obtain From Cards
Max Sum of Distinct Subarrays Length k
Overview
Longest Substring Without Repeating Characters
Longest Repeating Character Replacement
Overview
Can Attend Meetings
Insert Interval
Non-Overlapping Intervals
Merge Intervals
Employee Free Time
Overview
Valid Parentheses
Decode String
Longest Valid Parentheses
Monotonic Stack
Daily Temperatures
Largest Rectangle in Histogram
Overview
Linked List Cycle
Palindrome Linked List
Remove Nth Node From End of List
Reorder List
Swap Nodes in Pairs
Overview
Apple Harvest (Koko Eating Bananas)
Search in Rotated Sorted Array
Split Array Largest Sum
Kth Smallest Element in a Sorted Matrix
Minimum Shipping Capacity
Overview
Kth Largest Element in an Array
K Closest Points to Origin
Find K Closest Elements
Merge K Sorted Lists
Median from Data Stream
Introduction
Fundamentals
Return Values
Maximum Depth of Binary Tree
Path Sum
Passing Values Down and Helper Functions
Validate Binary Search Tree
Calculate Tilt
Diameter of a Binary Tree
Path Sum II
Longest Univalue Path
Graphs Overview
Adjacency List
Copy Graph
Graph Valid Tree
Matrices
Flood Fill
Number of Islands
Surrounded Regions
Pacific Atlantic Water Flow
Introduction
Overview
Level Order Sum
Rightmost Node
Zigzag Level Order
Maximum Width of Binary Tree
Graphs Overview
Minimum Knight Moves
Rotting Oranges
01-Matrix
Bus Routes
Overview
Word Search
Solution Space Trees
Subsets
Generate Parentheses
Combination Sum
Palindrome Partitioning
N-Queens
Overview
Course Schedule
Course Schedule II
Shortest Path Algorithms
Network Delay Time
Cheapest Flights Within K Stops
Path With Minimum Effort
Find City with Fewest Reachable
Fundamentals
Solving a Question with Dynamic Programming
Counting Bits
Decode Ways
Unique Paths
Maximal Square
Longest Increasing Subsequence
Word Break
Maximum Profit in Job Scheduling
Paint House
Paint House II
Minimum Window Subsequence
Overview
Best Time to Buy and Sell Stock
Gas Station
Jump Game
Jump Game II
Partition Labels
Overview
Implement Trie Methods
Prefix Matching
Overview
Count Vowels in Substrings
Subarray Sum Equals K
Spiral Matrix
Rotate Image
Set Matrix Zeroes
Vote For New Content
Pricing
Sign in / Sign up
Search
⌘K
Pricing

Tutor

Graphs

Topological Sort


max (21)341224132;341225102;21365487109
Count: 10
abcValid triangle requires:a + b > c AND a + c > b AND b + c > a(every pair must sum to more than the third side)3511SOURCE23211SOURCE23UNREACHABLE$100$100$100$5000SRC123DST$100$100$1000SRC123DST01233141Threshold: 4Answer: 32 reachable01234231118Threshold: 2Answer: 01 reachable1102233321432263321
Pre-Requisite: Depth-First Search, Breadth-First Search
When working with graphs, make sure you first practice using Depth-First Search (DFS) and Breadth-First Search (BFS) to solve coding interview questions, as they are the most common graph algorithms you'll encounter.
This section covers Topological Sort, an important but less common graph algorithm.

Topological sort takes a directed acyclic graph (DAG) and turns it into a linear ordering of nodes such that the directed edges only point forward, from left-to-right:
2341010324
A graph (left) and its topological sort (right)
In more formal terms, a topological sort is a linear ordering of vertices such that for every directed edge u -> v, vertex u comes before vertex v in the ordering.
A given graph may have more than one valid topological sorts. We'll look at one algorithm for finding a topological sort - Kahn's Algorithm, which uses the concept of indegrees.

Indegrees

Each node in a graph has an indegree, which is the number of incoming edges to that node.
2341001211
The indegree is shown next to each node.
Calculate Indegrees
List of Edges
If our graph is given to us as list of edges, we can calculate the indegree of each node by iterating through the edges and incrementing the indegree of the destination node.
DESCRIPTION
You are given a graph with n nodes, where each node has an integer value from 0 to n - 1.
The graph is represent by a list of edges, where edges[i] = [u, v] is a directed edge from node u to node v. Write a function to calculate the indegree of each node in the graph.
Example Input: edges = [(0, 1), (1, 2), (1, 3), (3, 2), (3, 4)] n = 5
23410
Output: [0, 1, 2, 1, 1]
Note that we can output the indegrees as an array because the nodes are numbered from 0 to n - 1. (We can look up the indegree of node i at index i in the array.) If the nodes were numbered differently, we would need a dictionary to map each node to its indegree.
Solution
Python
Language
def indegree(n, edges):
indegree = [0] * n
for u, v in edges:
indegree[v] += 1
return indegree
Adjacency List
If our graph is given to us as an adjacency list, we can calculate the indegree of each node by iterating through the neighbors of each node and incrementing their indegree.
DESCRIPTION
You are given a graph with n nodes, where each node has an integer value from 0 to n - 1.
The graph is represent by an adjacency list, where each node i is mapped to a list of nodes that have a directed edge from node i to them. Write a function to calculate the indegree of each node in the graph.
Example
Input:
edges = {0: [1], 1: [2, 3], 2: [], 3: [2, 4], 4: []}
n = 5
23410
Output: [0, 1, 2, 1, 1]
Solution
Python
Language
def indegree(adj_list, n):
indegree = [0] * n
for u in adj_list:
# increment the indegree of each neighbor of u
for v in adj_list[u]:
indegree[v] += 1

Kahn's Algorithm

Kahn's algorithm is a form of Breadth-First Search in which nodes with lower indegrees are placed on the queue before nodes with higher indegrees.
The algorithm is as follows:
  1. Calculate the indegree of each node.
  2. Add all nodes with an indegree of 0 to a queue.
  3. While the queue is not empty:
    1. Dequeue the first node from the queue and add it to the topological order.
    2. For each neighbor of the node, decrement its indegree by 1. If the neighbor's indegree is now 0, add it to the queue.
  4. Return the topological order.

Walkthrough

We'll walkthrough how the algorithm works for the graph given by the adjacency list below:
adjList = {
0: [1, 3],
1: [2],
2: [],
3: [1, 4, 5],
4: [5],
5: []
}
012345
Step 1: Calculate the indegree of each node.
001221314152
The indegree is shown next to each node.
Step 2: Add all nodes with an indegree of 0 to the queue.
001221314152queue[0]
Step 3:
While the queue is not empty:
  • Deque the first node from the queue and add it to the topological sort.
Visualization
001221314152order[]queue[0]

enqueue nodes with indegree 0

0 / 1

  • Decrement the indegree of each neighbor of the node. If this results in a neighbor having an indegree of 0, add it to the queue.
Visualization
001221314152order[0]queue[]

dequeue and add node 0 to topological sort

0 / 1

  • We have fully processed node 1, so now repeat the process of dequeuing and decrementing indegrees for the next node in the queue until the queue is empty.
Visualization
001121304152order[0]queue[3]

decrement indegree of neighbors

0 / 8

Step 4: Return the topological order.
Visualization
001020304050order[0, 3, 1, 4, 2, 5]queue[]

dequeue and add node 5 to topological sort

0 / 1

Solution

Solution
Python
Language
from collections import deque
def topological_sort(adj_list, n):
# calculate indegree of each node
indegree = [0] * n
for u in adj_list:
for v in adj_list[u]:
indegree[v] += 1
# enqueue nodes with indegree 0
queue = deque([u for u in range(n) if indegree[u] == 0])
order = []
while queue:
u = queue.popleft()
order.append(u)
for v in adj_list.get(u, []):
indegree[v] -= 1
if indegree[v] == 0:
queue.append(v)
return order if len(order) == n else []
Test Your Knowledge

Login to take the complexity quiz and track your progress

Complexity Analysis

Time Complexity: O(V + E) where V is the number of vertices and E is the number of edges in the graph. We visit each vertex once and each edge twice (once when calculating indegrees, and once when processing vertices in the BFS). This gives us O(V + 2E), which simplifies to O(V + E).

Space Complexity: O(V) where V is the number of vertices in the graph. This is due to the data structure we use to store the indegrees of each vertex, and the queue we use to store vertices with an indegree of 0, both of which store up to V elements.

Mark as read

Next: Course Schedule

Your account is free and you can post anonymously if you choose.

Unlock Premium Coding Content

Interactive algorithm visualizations
Guided Practice
Recent interview questions
Learn More
Reading Progress

On This Page

Indegrees

Kahn's Algorithm

Walkthrough

Questions
Meta SWE Interview QuestionsAmazon SWE Interview QuestionsGoogle SWE Interview QuestionsOpenAI SWE Interview QuestionsEngineering Manager (EM) Interview Questions
Learn
Learn System DesignLearn DSALearn BehavioralLearn ML System DesignLearn Low Level DesignGuided Practice
Links
FAQPricingGift PremiumHello Interview Premium
Legal
Terms and ConditionsPrivacy PolicySecurity
Contact
About UsProduct Support

7511 Greenwood Ave North Unit #4238 Seattle WA 98103


© 2026 Optick Labs Inc. All rights reserved.

Login to track your progress