PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Return a topological ordering of a graph

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of graph algorithms—particularly topological sorting and cycle detection—and competency in ordering dependencies in directed acyclic graphs.

  • hard
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Return a topological ordering of a graph

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem: Output a topological ordering Given a directed graph with `n` nodes labeled `0..n-1` and a list of directed edges `edges`, return a topological ordering of the nodes. A topological ordering is a permutation of all nodes such that for every directed edge `(u, v)`, node `u` appears before node `v` in the ordering. ### Input - Integer `n` - List `edges` where each element is a pair `(u, v)` meaning `u -> v` ### Output - Return a list of length `n` containing one valid topological order. - If no topological order exists (graph contains a cycle), return an empty list (or explicitly signal failure). ### Constraints (reasonable defaults) - `1 <= n <= 2e5` - `0 <= |edges| <= 2e5` ### Follow-up - Explain how you would ensure the returned order is correctly produced (i.e., not just detecting that a topo sort exists, but actually outputting the sequence).

Quick Answer: This question evaluates understanding of graph algorithms—particularly topological sorting and cycle detection—and competency in ordering dependencies in directed acyclic graphs.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0
Loading...

Problem: Output a topological ordering

Given a directed graph with n nodes labeled 0..n-1 and a list of directed edges edges, return a topological ordering of the nodes.

A topological ordering is a permutation of all nodes such that for every directed edge (u, v), node u appears before node v in the ordering.

Input

  • Integer n
  • List edges where each element is a pair (u, v) meaning u -> v

Output

  • Return a list of length n containing one valid topological order.
  • If no topological order exists (graph contains a cycle), return an empty list (or explicitly signal failure).

Constraints (reasonable defaults)

  • 1 <= n <= 2e5
  • 0 <= |edges| <= 2e5

Follow-up

  • Explain how you would ensure the returned order is correctly produced (i.e., not just detecting that a topo sort exists, but actually outputting the sequence).

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.