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 a BFS-based bipartiteness check in Python. Function is_bipartite(graph) should return True/False and a 2-coloring if bipartite. Graph may be disconnected. Explain why BFS/DFS coloring works and the complexity.

MediumTechnical
21 practiced

While on-call you detect that a recent change introduced a circular dependency causing a failed rollout. Describe a practical approach to detect such cycles automatically in CI before merges are accepted: which graph algorithm to run, how to construct the dependency graph from manifests/configs, where to integrate the check, and how to scale it for large repos and many services.

EasyTechnical
31 practiced

Explain the formal differences between a tree and a general graph. Describe properties that define a tree (connected, acyclic, exactly n-1 edges for n nodes), implications such as unique simple path between nodes, and how those properties simplify algorithms (e.g., no need for visited set in some traversals). Give concrete examples of when you'd model a problem as a tree versus as a general graph.

EasyTechnical
23 practiced

Implement a multi-source BFS in Python. Input: n (number of nodes 0..n-1), edges list for an undirected unweighted graph, and a list of source nodes. Return an integer array dist of length n where dist[v] is the minimum number of edges from v to the nearest source, or -1 if unreachable. Your solution must run in O(V + E) time and use O(V) extra space.

HardTechnical
29 practiced

Prove that Dijkstra's algorithm returns correct shortest paths for graphs with non-negative edge weights. Then give a counterexample graph with a negative-weight edge that causes Dijkstra to fail, and explain which property of the algorithm is violated by negative weights.

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.