Count islands on a torus grid
TikTokSoftware Engineer · Technical ScreenLast updated
This question evaluates skills in graph and grid algorithms, topology-aware adjacency (torus wrap-around), and maintaining dynamic connectivity under incremental updates.
You are given an m x n binary grid representing water ( 0) and land (
- laid out on a torus: the top edge is adjacent to the bottom edge and the left edge is adjacent to the right edge. Two land cells are connected if they share a side (4-directional), considering wrap-around adjacency. Return the number of connected land components (islands). Follow-ups: (
- Provide two distinct approaches (e.g., graph search and disjoint-set). (
- Analyze the time and space complexities. (
- Extend the solution to support a stream of single-cell flips between 0 and 1 and report the island count after each update.
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.