Detect Cycles in 2D Grid
A medium-tier problem at 50% community acceptance, tagged with Array, Depth-First Search, Breadth-First Search. Reported in interviews at Nutanix and 0 others.
You're given a 2D grid and need to detect whether a cycle exists. Nutanix asks this, and it shows up sporadically across assessments. The problem sounds straightforward until you realize that in a grid, a cycle isn't just about revisiting a node, it's about finding a closed loop of connected cells without retracing your exact path backward. Half the candidates get it wrong on the first attempt because they confuse cycle detection in graphs with the specific constraints of grid traversal. If this lands in your live OA and you blank on the approach, StealthCoder surfaces a working solution in seconds, invisible to the proctor.
Companies that ask "Detect Cycles in 2D Grid"
Detect Cycles in 2D Grid is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe trap is thinking every return to a visited cell is a cycle. It's not. In an undirected grid, you can move to an adjacent cell that you just came from without forming a cycle. The actual pattern: use DFS to track the current path, mark cells as visited, and detect a cycle only when you reach a visited cell that isn't your immediate parent in the recursion tree. Union Find also works if you're thinking in terms of merging connected components and detecting when a union operation would create a redundant edge. Most candidates start with a naive visited set and fail test cases. The trick is maintaining both visited state and parent tracking (for DFS) or detecting edges that would merge already-connected components (for Union Find). This is fundamentally about graph cycle detection applied to grid topology. StealthCoder handles both approaches and knows the exact boundary conditions that trip up hand-coded solutions.
Pattern tags
You know the problem.
Make sure you actually pass it.
Detect Cycles in 2D Grid recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Detect Cycles in 2D Grid interview FAQ
How is grid cycle detection different from standard graph cycle detection?+
In a grid, cells have up to four neighbors, and you can move in cardinal directions. The key difference: you must distinguish between backtracking to your parent (allowed) and reaching a visited cell via a different path (cycle). In a standard directed graph, any revisit is a cycle. Here, the undirected nature and adjacency rules create specific patterns.
Is this still asked after the acceptance rate dropped?+
Nutanix reportedly asks it. It appears in medium-tier assessments, particularly at infrastructure and systems companies. The 50% acceptance rate suggests it weeds out candidates who conflate grid traversal with general graph problems.
What's the most common failure mode?+
Candidates mark a cell visited, then revisit it from a different neighbor and incorrectly flag a cycle. You need to track not just visited state but also the parent node you came from in DFS, or use Union Find to detect when merging creates a redundant connection.
Is Union Find or DFS faster for this problem?+
Both run in O(rows * cols) time and space. DFS with parent tracking is more intuitive for grids. Union Find is cleaner conceptually if you're already comfortable with path compression and rank optimizations. Pick whichever you practice more.
Will I need to handle connected components separately?+
Possibly. If the grid has multiple disconnected regions, you must run cycle detection on each component. This is automatic with DFS (call it on every unvisited cell) or handled by Union Find's forest structure. Missing this is a common off-by-one error.
Want the actual problem statement? View "Detect Cycles in 2D Grid" on LeetCode →