Graph, Grid, And Connectivity Algorithms
Asked of: Software Engineer
Last updated

What's being tested
Graph/grid connectivity questions test whether you can model cells or entities as nodes, traverse components correctly, and preserve invariants under edge cases like cycles, wrap-around, or repeated shapes. Interviewers look for clean DFS/BFS, Union-Find, topological sort, and proof-level reasoning about correctness and complexity.
Patterns & templates
-
Grid
DFS/BFScomponents — scan every cell, start traversal on unvisited land;O(R*C)time,O(R*C)worst-case space. -
Distinct island normalization — record relative coordinates from an origin, e.g.
(r-r0, c-c0); sort/serialize shape to compare patterns. -
Bipartite graph coloring — assign colors with
BFS/DFS; conflict on same-color edge means impossible; handle disconnected components. -
Topological sort — use
Kahn’s algorithmwith indegrees or postorderDFS; cycle exists if processed count< n. -
Dynamic connectivity — use Disjoint Set Union with path compression and union by rank; near-constant amortized
α(n)operations. -
Torus grid adjacency — compute neighbors with modulo:
(r+dr+R)%R,(c+dc+C)%C; beware duplicate neighbors in tiny grids. -
Monotonic deque — for sliding maximum, keep indices in decreasing value order;
O(n)time, remove expired front indices.
Common pitfalls
Pitfall: Treating diagonal cells as connected when the problem only allows 4-directional adjacency.
Pitfall: Counting distinct islands by traversal string without stable direction markers or relative coordinates, causing different shapes to collide.
Pitfall: Running topological sort from only one node; disconnected graph components must all be initialized or visited.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Solve pair-counting and account-merging problemsTikTok · Software Engineer · Technical Screen · medium
- Find top-k rated nodes via traversalTikTok · Software Engineer · Onsite · Medium
- Count islands on a torus gridTikTok · Software Engineer · Technical Screen · Medium
- Compute pipeline order from dependenciesTikTok · Software Engineer · Technical Screen · Medium
- Solve Topological Sort and Anagram IndicesTikTok · Software Engineer · Technical Screen · Medium
- Implement distinct islands and sliding maximaTikTok · Software Engineer · Technical Screen · Medium
- Solve intervals and distinct islandsTikTok · Software Engineer · Onsite · Medium
- Check if two-group seating is possibleTikTok · Software Engineer · Technical Screen · Medium
Related concepts
- Graph Algorithms, Dependency Resolution And ConnectivityCoding & Algorithms
- Graphs, Grids, And Connected ComponentsCoding & Algorithms
- Graph, Grid, BFS/DFS, And Union-FindCoding & Algorithms
- Graph Traversal And Shortest PathsCoding & Algorithms
- BFS, DFS, Graph, And Grid TraversalCoding & Algorithms
- DFS/BFS Tree, Graph, And Grid TraversalCoding & Algorithms