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

Given a grid of '1's (land) and '0's (water), implement a Python function def num_islands(grid: List[List[str]]) -> int that counts the number of islands (connected components of 1s) using DFS or BFS. Include handling of edge cases such as empty grid and non-rectangular inputs. Discuss in-place marking vs using a visited matrix.

HardTechnical
30 practiced

In reinforcement learning, the environment's state-space can be represented as a directed graph. Discuss how choosing search strategies like DFS or BFS (and their variants) affects policy search, exploration efficiency, and safety. Propose heuristics (depth limits, iterative deepening, A*/heuristic-guided search) for large or infinite state spaces and mention when each is appropriate.

EasyTechnical
23 practiced

Describe the adjacency list and adjacency matrix graph representations. For each, state the time and space complexity for common operations: iterate neighbors, check edge existence, add/remove an edge (for directed and undirected graphs). Given two example graphs (V=100000, E=200000) and (V=10000, E=80,000,000), recommend which representation you'd choose and why. Discuss tradeoffs for dynamic graphs and alternatives like CSR or edge-lists.

EasyTechnical
27 practiced

Write a Python function that detects whether an undirected graph (adjacency list Dict[int, List[int]]) contains any cycle. The function should return True if a cycle exists and False otherwise. Explain why you must track the parent node during DFS to avoid false-positive detection from immediate back-edges. Ensure O(|V|+|E|) time.

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.