PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of dynamic connectivity, incremental graph updates, and algorithmic efficiency when maintaining connected components under repeated modifications.

  • medium
  • Uber
  • Coding & Algorithms
  • Software Engineer

Count Islands After Land Additions

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given an initially empty `m x n` grid filled with water. Land is added one cell at a time. Implement a function that receives: - `m`: number of rows - `n`: number of columns - `positions`: a list of coordinates `[row, col]` indicating cells that are converted from water to land After each land addition, return the current number of islands. An island is a group of land cells connected horizontally or vertically. If the same cell is added more than once, the island count should not change for that operation. Example: ```text m = 3, n = 3 positions = [[0,0], [0,1], [1,2], [2,1], [1,1]] Output: [1, 1, 2, 3, 1] ``` Design an efficient solution suitable for large grids and many land-addition operations.

Quick Answer: This question evaluates understanding of dynamic connectivity, incremental graph updates, and algorithmic efficiency when maintaining connected components under repeated modifications.

You are given an initially empty m x n grid of water. Land is added one cell at a time according to the list positions, where each element is [row, col]. After each operation, return the current number of islands in the grid. An island is a group of land cells connected horizontally or vertically. If a cell is added more than once, that operation does not change the island count. Your solution should be efficient enough for large grids and many additions.

Constraints

  • 1 <= m, n <= 10^4
  • 0 <= len(positions) <= 10^5
  • 0 <= row < m and 0 <= col < n for every position
  • positions may contain duplicate coordinates

Examples

Input: (3, 3, [[0, 0], [0, 1], [1, 2], [2, 1], [1, 1]])

Expected Output: [1, 1, 2, 3, 1]

Explanation: The first two additions form one island, then two separate islands are created, and the final addition connects all land into a single island.

Input: (1, 1, [[0, 0], [0, 0], [0, 0]])

Expected Output: [1, 1, 1]

Explanation: Adding the same single cell multiple times does not change the island count after the first addition.

Input: (2, 2, [])

Expected Output: []

Explanation: No land additions means there are no intermediate island counts to report.

Input: (2, 2, [[0, 0], [1, 1], [0, 1], [1, 0], [1, 0]])

Expected Output: [1, 2, 1, 1, 1]

Explanation: Two separate islands are created, then merged into one by [0, 1]. Adding [1, 0] keeps everything connected, and the duplicate final addition changes nothing.

Input: (1, 3, [[0, 0], [0, 2], [0, 1]])

Expected Output: [1, 2, 1]

Explanation: The middle cell connects the two end cells into one island.

Hints

  1. Recomputing the number of islands with a full BFS or DFS after every addition will be too slow.
  2. Use a Disjoint Set Union (Union-Find) structure to connect newly added land with its existing neighboring land cells and maintain the island count incrementally.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Maximize Throughput and Count Trigger Components - Uber (medium)
  • Replace Dashes With Nearest Letters - Uber (medium)
  • Find Earliest Column With One - Uber (easy)
  • Solve Wonderful Strings and Grid Queries - Uber (hard)
  • Implement Last-Click Attribution APIs - Uber (medium)