Topological Sort on graph

Last updated: September 4, 2025

Quick Overview

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

Anthropic
Coding & Algorithms
Software Engineer
Anthropic
September 4, 2025
Software Engineer
Take-home Project
Coding & Algorithms
Easy

2

0

3,004 solved


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

Anthropic uses this problem in the Take-home Project 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
Edge cases and input validation
Tree structures and recursion
Hash maps and frequency counting
Time and space complexity analysis
Graph algorithms and traversal
Sorting and searching
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 solve this iteratively instead of recursively (or vice versa)?
  • Can you optimize the space complexity of your solution?
  • How would you parallelize this solution?
  • Can you solve this in a single pass?
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 topological sorting for a directed acyclic graph (DAG), we can utilize the Kahn's algorithm or Depth-First Search (DFS) approach. Here, I'll focus on Kahn's algorithm because i...

Approach
  1. Initialize In-Degree Count: Create a dictionary to count the in-degrees of each vertex in the graph. For example, given a graph represented as `{'A': ['B', 'C'], 'B': ['D'], 'C': ['D'], 'D': []...

Submit Your Answer
Markdown supported

Related Questions