DSAMaster Logo
DSAMaster
Graphs• 2 August 2026• 25 min read

Top 4 Graph Algorithm Interview Questions and Answers (2026)

Master graph algorithm interview questions with detailed answers covering BFS, DFS, graph cloning, topological sort, and cycle detection. Complete solutions in C++, Java, Python, and JavaScript.

D
Written by DSAMaster Team
DSAMaster Editorial

1. Why Graphs Are Critical for Interviews

Graphs are the most versatile data structure in computer science. Real-world problems in navigation (Google Maps), social networks (LinkedIn/Facebook), dependency management, and network routing all map directly to graph algorithms.

Below are top Graph interview questions with detailed explanations, intuition, and implementations in C++, Java, Python, and JavaScript.


2. Graph Representation & Basics

Adjacency list is the standard graph representation:

javascript
const adj = new Map();

3. Easy & Medium Graph Questions

Q1. Number of Islands (Grid BFS/DFS)

Question: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

javascript
function numIslands(grid) { if (!grid || grid.length === 0) return 0; let m = grid.length, n = grid[0].length; let count = 0; function dfs(r, c) { if (r < 0 || r >= m || c < 0 || c >= n || grid[r][c] === '0') return; grid[r][c] = '0'; dfs(r + 1, c); dfs(r - 1, c); dfs(r, c + 1); dfs(r, c - 1); } for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (grid[i][j] === '1') { count++; dfs(i, j); } } } return count; }

Time Complexity: O(m x n) | Space Complexity: O(m x n) call stack


Q2. Clone Graph

Question: Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph.

javascript
function cloneGraph(node) { if (!node) return null; let visited = new Map(); function dfs(curr) { if (visited.has(curr)) return visited.get(curr); let clone = { val: curr.val, neighbors: [] }; visited.set(curr, clone); for (let neighbor of curr.neighbors) { clone.neighbors.push(dfs(neighbor)); } return clone; } return dfs(node); }

Time Complexity: O(V + E) | Space Complexity: O(V)


Q3. Course Schedule (Topological Sort / Cycle Detection)

Question: There are numCourses courses labeled 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a, b] indicates that you must take course b first. Can you finish all courses?

javascript
function canFinish(numCourses, prerequisites) { let adj = Array.from({ length: numCourses }, () => []); let inDegree = new Array(numCourses).fill(0); for (let [dest, src] of prerequisites) { adj[src].push(dest); inDegree[dest]++; } let q = []; for (let i = 0; i < numCourses; i++) { if (inDegree[i] === 0) q.push(i); } let count = 0; while (q.length > 0) { let curr = q.shift(); count++; for (let neighbor of adj[curr]) { inDegree[neighbor]--; if (inDegree[neighbor] === 0) q.push(neighbor); } } return count === numCourses; }

Time Complexity: O(V + E) | Space Complexity: O(V + E)


Q4. Shortest Path in Binary Matrix (BFS)

Question: Given an n x n binary matrix grid, return the length of the shortest clear path from top-left (0,0) to bottom-right (n-1,n-1). Return -1 if no clear path exists.

javascript
function shortestPathBinaryMatrix(grid) { let n = grid.length; if (grid[0][0] !== 0 || grid[n - 1][n - 1] !== 0) return -1; let q = [[0, 0, 1]]; grid[0][0] = 1; let dirs = [[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]]; while (q.length > 0) { let [r, c, steps] = q.shift(); if (r === n - 1 && c === n - 1) return steps; for (let [dr, dc] of dirs) { let nr = r + dr, nc = c + dc; if (nr >= 0 && nr < n && nc >= 0 && nc < n && grid[nr][nc] === 0) { grid[nr][nc] = 1; q.push([nr, nc, steps + 1]); } } } return -1; }

Time Complexity: O(n^2) | Space Complexity: O(n^2)


4. Summary Table

ProblemAlgorithmTimeSpace
Number of IslandsGrid DFS / BFSO(m x n)O(m x n)
Clone GraphDFS with MapO(V + E)O(V)
Course ScheduleKahn's Algorithm (BFS)O(V + E)O(V + E)
Shortest Path in Matrix8-directional BFSO(n^2)O(n^2)

Practice all graph problems on DSAMaster's practice platform.