How do you count islands in a 2D grid?
Reported in SumUp European engineering loops. Graph traversal question using DFS or BFS flood-fill.
Interview scenario
Context for SumUp candidates:
Given a grid of land and water, count connected land components horizontally and vertically.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at SumUp: 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.
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.