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
22 practiced

Solve the maximum weight independent set on a tree: given a tree where each node has a non-negative weight, select a set of nodes with no adjacent nodes maximizing total weight. Implement in Python with O(N) time using tree DP. Provide signature: def max_independent_set(adj: Dict[int, List[int]], weights: Dict[int,int]) -> int and explain your DP states.

HardTechnical
21 practiced

Design and implement in Python a serialization and deserialization scheme for a general directed graph with cycles and labeled node IDs. Functions: serialize(graph) -> str and deserialize(s) -> graph. The format should preserve node identities and adjacency lists, handle disconnected graphs, and avoid infinite loops during serialization. You may use JSON or edge-list encodings; explain how you avoid duplicating nodes and how you handle large graphs.

MediumTechnical
29 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.

EasyTechnical
30 practiced

Explain visited-state management in graph traversals. Describe the differences between marking nodes visited on discovery (enqueue) versus when they are processed (dequeue), or using color states (white/gray/black). Discuss implications for correctness, duplicate work, cycle detection, multi-source traversal, and parallel traversals in SRE systems.

HardTechnical
24 practiced

Given a DAG where multiple valid topological orders exist, implement a deterministic topological sort that returns the lexicographically smallest (by node id) valid order. Implement def topo_lex(graph) -> Optional[List[int]] in Python using Kahn's approach with tie-breaking. Detect cycles and return None when DAGness is violated.

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.