Count islands with eight-direction adjacency
SnapchatSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's competency in graph traversal algorithms and data-structure trade-offs by counting connected components on a grid with eight-direction adjacency, focusing on BFS/DFS behavior and optional Union-Find approaches.
You are given an m×n grid of characters '1' (land) and '0' (water). Count the number of islands, where cells are connected if they touch in any of the eight directions (N, NE, E, SE, S, SW, W, NW). Implement an algorithm that returns the island count. Discuss and implement one approach (BFS or DFS), analyze time and space complexity, and explain how you would handle very large grids to avoid recursion limits. Optional: outline a Union-Find solution and compare trade-offs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.