← Visualizers
Graphs · Anti-Pattern LeetCode 463

Island Perimeter / visualized

Why DFS is the wrong instinct here: perimeter is an edge-counting problem, not a traversal. Scan each cell and count +1 for every boundary or water-facing side — solving in O(1) space without recursion.

Execution

idle
ANTI-PATTERN Perimeter is NOT a traversal problem — it is an edge-counting scan. Any land cell contributes +1 for every neighbor that is water or off-grid. Solves in O(1) space!
Press Run to begin.
0 / 0
Speed

Java · running line

scanning cell land evaluated perimeter edge (+1) counted land water / skipped
INTERVIEW SCRIPT

How to Explain Island Perimeter to an Interviewer

1. Reject the DFS Reflex

"While this looks like a graph question, using DFS is an anti-pattern. Traversal requires $O(m \cdot n)$ auxiliary call stack space, which is entirely unnecessary."

2. The O(1) Space Scan

"Every land cell starts with 4 possible perimeter edges. An edge exists if and only if the adjacent cell is off-grid or water. We simply sum these tests in two nested loops."

3. Zero Mutation

"Because we only read adjacent coordinates, the input grid remains completely immutable and unmodified."

4. Complexity Mastery

"Time is $O(m \cdot n)$ as we touch each cell once. Auxiliary space is strictly $O(1)$ — no visited array, no queues, no recursion."

O(m·n) time · O(1) space  ·  each land cell contributes 4 − adjacent_land_neighbors edges. No visited matrix or recursive stack needed.