Graphs & Matrices Interview Questions & Solutions

27 solutions ยท 6 Easy ยท 18 Medium ยท 3 Hard

Graph problems intimidate candidates until they notice the pattern: the vast majority are solved by the same two traversals, BFS and DFS. The skill isn't memorizing exotic algorithms โ€” it's modeling the problem as a graph, picking the right traversal, and tracking visited state so you don't loop forever. A huge share of "matrix" questions are really graph questions where each cell is a node and its four neighbors are edges.

These solutions make that modeling explicit. You'll cover grid traversal (number of islands, flood fill, rotting oranges), connected components and union-find, cycle detection and topological sort for dependency problems, and shortest-path problems where BFS on an unweighted graph is exactly the right tool. We're careful about the details that actually sink submissions: marking cells visited correctly, handling boundaries, and choosing BFS when you need the shortest number of steps versus DFS when you just need reachability. Once this pattern lands, a whole tier of "hard" interview problems becomes approachable.

Practice Graphs & Matrices the right way

SkillFlow serves you Graphs & Matrices problems matched to your level, times you like a real interview, and tracks your proficiency as it climbs. Free.

Start practicing free โ†’

Other interview patterns