I Had Screwed Up Twice Early In My Career.

The One Coding Question That I Have Been Asked The Most
Photo by Sebastian Boring on Unsplash

Not On Medium? Read it here.

The Problem (That Fooled Me)

You’re given a grid of '1's and '0's. '1's represent land, and '0's are water. You need to count how many islands there are. An island is formed by connecting adjacent '1's (horizontally or vertically).

Example Input

[
["1","1","0","0","0"],
["1","1","0","0","0"],
["0","0","1","0","0"],
["0","0","0","1","1"]
]

Expected Output?

3 islands. Simple. But when I tried to code it, I got slapped.

Yeah, an easy question. Right?

Some coding problems look easy until you actually try to solve them

And I have been asked this 6 times until now. And I have thought I’d breeze through it. But twice in my early days, I didn’t.

Here’s how I got stuck, stumbled, and eventually found my way out.

My First Dumb Approach

To be honest, I’ll blame the interviewer. Because I wasn’t told anything properly.

I thought, “I’ll just loop through the grid and count every '1' I find."

Guess what? That doesn’t work. You end up counting the same island multiple times because you’re not accounting for connected lands. Great job, me.

The Realization (Finally)

It hit me later — this is basically a graph traversal problem. Every '1' is like a node, and connected '1's form a component. The goal? Count how many components (islands) exist.

And how do you explore components in a grid? Depth-First Search (DFS).

The Approach That Actually Worked

  1. Loop through the grid.
  2. When I find a '1', trigger DFS to "flood" the entire island and mark all its parts as visited.
  3. Increment the island count.
  4. Continue.

For marking visited nodes, I just changed '1' to '2'.

Why complicate things with extra arrays? (This is the optimization I show every time, to look cool in front of the interviewer)

Here’s the Code Btw:

class Solution:
def dfs(self, i: int, j: int, grid: List[List[str]]):
# Stop if out of bounds or on water/visited land
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != '1':
return

# Mark as visited
grid[i][j] = '2'

# Visit all four directions
self.dfs(i + 1, j, grid)
self.dfs(i - 1, j, grid)
self.dfs(i, j + 1, grid)
self.dfs(i, j - 1, grid)
    def numIslands(self, grid: List[List[str]]) -> int:
count = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == '1':
self.dfs(i, j, grid)
count += 1
return count

Why This Worked

  • In-place marking: No need for a separate visited array. I just converted '1' to '2' after visiting.
  • DFS for exploration: Each call ensures all connected lands are marked.
  • Simple and clean: No unnecessary complexity.

The Tricky Bits

  • Edge Cases: I made sure to test for grids with all water, all land, or no grid at all.
  • Recursion Limit: For massive grids, Python’s recursion limit might be a problem. But for the basic problem, this was fine.
  • Avoiding Overthinking: Initially, I thought about BFS, separate arrays, or even fancy algorithms. But simple DFS works just fine with interviewers

What I Learned

  • Don’t Overcomplicate: My first few attempts were messy because I tried to be too smart. Sometimes, a straightforward solution is the best one.
  • Understand the Problem: This wasn’t just about counting. It was about identifying connected components.
  • Keep It Simple: Replacing '1' with '2' is enough to mark a cell as visited. Why bother with extra arrays?

Final Thoughts

This wasn’t just a problem about islands. It was about how I tend to overthink and complicate things. Sometimes the simplest path, like running a basic DFS, is the one that works.

If you’re stuck, take a step back and ask: “Am I overcomplicating this?” Because I was. And once I simplified, the answer was right there.

So, does that picture tell you, I’m talking about the Number of Islands Problem from Leetcode?

In case we are meeting for the first time, come over here, it’ll be worth the roller coaster of articles that are gonna come up in the next few weeks.

Use this to get to know more about me — https://linktr.ee/shashwat_writes