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.

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.

EasyTechnical
23 practiced

Explain the Bellman-Ford algorithm and how it detects negative-weight cycles. Discuss the algorithm's complexity and scenarios where Bellman-Ford is preferred over Dijkstra in ML systems or feature computations.

MediumTechnical
21 practiced

Formally derive the time and space complexity of BFS and DFS on graphs represented as (a) adjacency list and (b) adjacency matrix. Show how the number of edge inspections leads to O(V+E) for adjacency lists and O(V^2) for adjacency matrices. Discuss directed vs undirected differences and assumptions about visiting implementation details (e.g., iterators).

MediumTechnical
23 practiced

You're designing a monitoring system that frequently traverses a service graph. Compare adjacency list, adjacency matrix, and compressed-sparse-row (CSR) representations given queries like: iterate neighbors, check edge existence, batch updates, and parallel traversal. Consider cache locality, memory usage, update frequency, and implications for sparse (E << V^2) vs dense graphs.

HardTechnical
21 practiced

Write functions in C++ to convert between adjacency matrix and adjacency list representations for a graph with n nodes labeled 0..n-1. Consider both directed and undirected graphs and explain the time and space complexity of each representation and of the conversion. Provide guidance on when to prefer one representation over the other.

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.