Skip to content
Learn Netverks
Company prep Nokia
Fresher (0–1 years) Coding / DSA Medium

How do you count islands in a 2D grid?

Reported in Nokia European engineering loops. Graph traversal question using DFS or BFS flood-fill.

Role
Frontend Engineer
Location
Copenhagen, Denmark

Context for Nokia candidates:

Given a grid of land and water, count connected land components horizontally and vertically.

Try answering aloud first

Cover trade-offs, structure, and a concrete example before revealing the baseline response.

Spoiler-free prep mode

How to frame this at Nokia: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.

Scan every cell; when you find unvisited land, increment island count and run DFS or BFS to mark its full connected component as visited. This prevents counting the same island multiple times.

The complexity is O(rows * cols) because each cell is visited at most once. Mention recursion depth risk for large grids and suggest iterative BFS with queue for safer production behavior.

Comments (0)

Share how this question came up in your loop, or add tips for others preparing.

Log in to comment on this question.