Topological Sort on graph

Last updated: October 19, 2025

Quick Overview

Given a directed acyclic graph (DAG), implement a function to perform a topological sort and return the nodes in a linear order such that for every directed edge from node A to node B, node A comes before node B in the ordering. The input will be represented as a list of edges, and the output should be a list of nodes in topologically sorted order.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
October 19, 2025
Software Engineer
Onsite
Coding & Algorithms
Easy

9

6

929 solved


Given a directed acyclic graph (DAG), implement a function to perform a topological sort and return the nodes in a linear order such that for every directed edge from node A to node B, node A comes before node B in the ordering. The input will be represented as a list of edges, and the output should be a list of nodes in topologically sorted order.

Microsoft uses this problem in the Onsite to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Time and space complexity analysis
Tree structures and recursion
Edge cases and input validation
Binary search and divide and conquer
Graph algorithms and traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Can you optimize the space complexity of your solution?
  • What if the input doesn't fit in memory?
  • How would you parallelize this solution?
  • How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

To solve the problem of performing a topological sort on a directed acyclic graph (DAG), we can utilize the Kahn's algorithm, which employs the concept of in-degrees and a queue. A topological sort is...

Approach
  1. Initialize Data Structures: Create an in-degree dictionary to count incoming edges for each node and a list to hold the result of the topological sort. Also, create a queue to manage nodes with...

Submit Your Answer
Markdown supported

Related Questions