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
65 practiced
Explain the core differences between Breadth-First Search (BFS) and Depth-First Search (DFS). For each algorithm, list three common practical use cases in machine learning engineering or systems (e.g., unweighted shortest path, topological order, feature propagation).
MediumTechnical
52 practiced
Implement a function k_hop_neighbors(graph, source, k) that returns all nodes at distance <= k from source in Python using BFS. Graph may be large but fits in memory; ensure you avoid revisiting nodes and that the function returns counts per hop as well as the flattened list.
HardTechnical
54 practiced
Implement multi-source Dijkstra in Python: given multiple source nodes with initial distances (some may be zero or different), compute shortest distances to all nodes. Explain how to initialize the priority queue and why correctness still holds. Target complexity O(E log V).
HardSystem Design
112 practiced
Design a compact on-disk format and API for storing a large static graph (hundreds of millions of edges) used for read-heavy nearest-neighbor and multi-hop queries in a low-latency online service. Consider compression, indexing, memory mapping, and how to serve adjacency slices to many concurrent workers efficiently.
EasyTechnical
70 practiced
Write a Python function to detect a cycle in an undirected graph. Use either DFS with parent tracking or Union-Find. Explain why the parent check is necessary in DFS-based detection. Graph is given as adjacency list; V up to 2*10^5.

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.