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.

HardTechnical
29 practiced

Given a directed acyclic graph (DAG) representing tasks with durations and precedence constraints, design an algorithm to compute the earliest completion time for each task and the overall project completion time. Explain how topological ordering and the critical path method are combined and how to modify the algorithm when resources are constrained (limited parallel workers).

MediumTechnical
25 practiced

When should you use Union-Find vs DFS/BFS to compute connected components? Compare their practical performance on large sparse graphs and discuss memory and incremental update implications for each approach.

EasyTechnical
30 practiced

Explain the union-find (disjoint set) data structure: the operations find and union, how path compression and union by rank/size improve performance, and why the amortized cost is nearly constant. Describe two SRE use cases where union-find is useful, such as offline connectivity queries or grouping servers by failure domain.

MediumTechnical
26 practiced

Implement Union-Find (Disjoint Set Union) with path compression and union by rank in Python. Provide methods: find(x), union(x,y), connected(x,y). Use it to answer k connectivity queries on an undirected graph given as a list of edges and queries. Aim for near-constant amortized time per operation.

EasyTechnical
25 practiced

Given an undirected graph represented as an adjacency list, write a function to compute the number of connected components and return a list of components (each component is a list of nodes). Your implementation should handle isolated nodes correctly and run in O(V+E) time. Explain your choice of BFS vs DFS and space complexity.

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.