InterviewStack.io LogoInterviewStack.io

Graphs and Graph Algorithms Questions

Graph representations (adjacency list and adjacency matrix) and the traversal algorithms applied to general, non-tree structures: BFS, DFS, topological sort (Kahn's algorithm and DFS-based), shortest paths (Dijkstra, Bellman-Ford, A*), minimum spanning trees, cycle detection, connected components, and union-find. Covers modeling a problem as a graph even when the underlying data is not obviously graph-shaped, such as state-space search, an implicit graph over strings or grid cells (for example Word Ladder), or a task-dependency graph, and implementing these traversals with a hash map or hash set as the storage vehicle (adjacency map, visited set, memoization table), not the subject being tested. The graded skill is traversal, ordering, connectivity, or shortest-path reasoning over nodes and edges. This topic does not own: traversal, reconstruction, or serialization of a single-rooted binary tree (preorder, inorder, postorder, or level-order implementation, rebuilding a tree from traversal arrays, lowest common ancestor, binary search tree validation), which belongs to binary trees and binary search trees even though a tree is technically a graph; hash table internals such as hash function design, collision resolution, and load factor and resizing, which belong to hashing and hash tables; and deriving or comparing algorithmic complexity across graph algorithms without implementing them, such as comparing the time complexity of BFS, DFS, Dijkstra, and A*, which belongs to time and space complexity analysis. One of the highest-signal areas in senior coding interviews.

EasyTechnical
26 practiced

Implement breadth-first search (BFS) in Python to find the shortest path between two nodes in an unweighted graph. Function signature: def shortest_path(adj: Dict[int, List[int]], start: int, target: int) -> List[int]. Return the list of nodes forming the shortest path from start to target inclusive, or an empty list if no path exists. The adjacency input is an adjacency list mapping node ids to neighbor lists. Aim for O(V+E) time and O(V) memory.

EasyTechnical
24 practiced

Discuss how common graph algorithms should handle input edge cases: self-loops, parallel/multi-edges, isolated nodes, and nodes with multiple labels. For BFS/DFS, Dijkstra, union-find and topological sort, describe concrete pitfalls you might encounter in production and how to sanitize or validate input to make implementations robust.

EasyTechnical
27 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
24 practiced

Implement Kruskal's algorithm to compute the Minimum Spanning Tree (MST) for an undirected weighted graph. Provide a Python function: def kruskal(n: int, edges: List[Tuple[int,int,int]]) -> List[Tuple[int,int,int]] that returns the list of edges in the MST. Use Union-Find for cycle detection, and discuss sorting complexity and overall runtime.

HardTechnical
20 practiced

Prove why the Bellman-Ford algorithm detects negative cycles reachable from the source: show why V-1 relaxations suffice for shortest path correctness and why an extra pass exposes negative cycles. Analyze its worst-case time complexity. Then propose practical approaches to scale negative-cycle detection for massive graphs with limited memory (sampling, partitioned checks, heuristics).

Unlock Full Question Bank

Get access to all Graphs and Graph Algorithms interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.