InterviewStack.io LogoInterviewStack.io

Graph Algorithms and Traversal Questions

Covers fundamental representations, traversal techniques, and classical algorithms for graph structured data. Candidates should understand graph representations such as adjacency list and adjacency matrix and the tradeoffs in time and space for each. Core traversal skills include implementing and reasoning about breadth first search and depth first search for reachability, traversal order, and unweighted shortest path discovery, as well as tree traversal variants and their relationship to graph traversals. Algorithmic topics include cycle detection, topological sorting for directed acyclic graphs, connected components and strongly connected components, and shortest path and pathfinding algorithms for weighted graphs including Dijkstra algorithm and Bellman Ford algorithm with discussion of negative weights and appropriate use cases. Candidates should be able to analyze time and space complexity, choose appropriate auxiliary data structures such as queues, stacks, priority queues, and union find, handle directed versus undirected and weighted versus unweighted graphs, discuss implementation details and trade offs, and explain practical applications such as dependency resolution, scheduling, pathfinding, connectivity queries, and roles of graph algorithms in system design and data processing.

EasyTechnical
76 practiced
Implement topological sort for a DAG using Kahn's algorithm in Python. Function signature: def topological_sort(graph: Dict[int, List[int]]) -> Optional[List[int]]. Return a list representing one valid topological order or None if a cycle exists. Include a small example graph and explain how the algorithm detects cycles.
MediumTechnical
73 practiced
Explain how to implement parallel BFS on GPU using a frontier-based approach. Discuss work distribution across threads/warps, handling duplicates and race conditions when multiple threads attempt to mark visited nodes, memory access patterns and coalescing, and optimizations such as using bitmaps or push/pull (top-down vs bottom-up) strategies.
HardTechnical
74 practiced
Prove the correctness of the Bellman-Ford algorithm for single-source shortest paths. Then describe a concrete algorithm to detect a negative cycle reachable from the source and to extract the nodes forming that cycle. Provide the key proof steps and complexity analysis.
HardSystem Design
56 practiced
Design an I/O-efficient algorithm to perform BFS on a graph that does not fit in memory (external-memory/out-of-core). Describe data layout on disk, streaming patterns to process frontier levels in batches, how to minimize random disk seeks, and how to measure complexity in terms of I/O operations. Mention existing frameworks or systems (GraphChi, X-Stream) that implement such approaches.
EasyTechnical
61 practiced
Given a directed graph in adjacency list form, implement a function in Python to detect whether the graph contains a directed cycle. Use DFS with 3-color marking (unvisited, visiting, visited) or an explicit recursion stack. Function signature: def has_cycle_directed(graph: Dict[int, List[int]]) -> bool. Provide the time and space complexity and example inputs demonstrating detection.

Unlock Full Question Bank

Get access to hundreds of Graph Algorithms and Traversal interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.