Graph BFS on Dependency Chains

Last updated: September 4, 2025

Quick Overview

Given a directed acyclic graph (DAG) representing dependency chains, implement a breadth-first search (BFS) algorithm to traverse the graph and return the nodes in the order they can be processed based on their dependencies. The input will be a list of edges representing the dependencies, and the output should be a list of nodes in the correct execution order. Ensure that your solution handles cases with multiple valid execution orders.

Perplexity
Coding & Algorithms
Software Engineer
Perplexity
September 4, 2025
Software Engineer
Coding Round
Coding & Algorithms
Medium

6

12

3,422 solved


Given a directed acyclic graph (DAG) representing dependency chains, implement a breadth-first search (BFS) algorithm to traverse the graph and return the nodes in the order they can be processed based on their dependencies. The input will be a list of edges representing the dependencies, and the output should be a list of nodes in the correct execution order. Ensure that your solution handles cases with multiple valid execution orders.

Standard graph problem that appears in distributed system coding follow-ups. Relevant to Perplexity's crawl scheduling and pipeline orchestration.

What the Interviewer Expects
  • Implement topological sort using Kahn's algorithm (BFS)
  • Detect cycles in the dependency graph
  • Return a valid execution order or indicate impossibility
  • Handle disconnected components
  • Discuss time and space complexity
Key Topics to Cover
Topological sort
BFS
Directed acyclic graph
Cycle detection
Task scheduling
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
  • How would you parallelize tasks that have no dependencies between them?
  • What if dependencies can be added dynamically?
  • How would you handle weighted dependencies (some tasks are more expensive)?
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 finding an execution order in a directed acyclic graph (DAG) based on dependency chains, we can utilize the BFS approach known as Kahn's Algorithm for topological sorting. This...

Approach
  1. Input Representation: Start by representing the graph using an adjacency list, and also maintain an array to track the in-degrees of each node (number of incoming edges).
  2. **Build the Graph...

Submit Your Answer
Markdown supported

Related Questions