Number of Islands II
Updated
Problem statement
Start with an m-by-n grid of water. Each position changes that cell to land. Adding land to a cell that is already land does nothing.
After every addition, return the number of islands. Land is connected through four sides, not diagonals.
Function
numIslands2(m: int, n: int, positions: int[][]) → int[]Examples
Example 1
m = 3n = 3positions = [[0,0],[0,1],[1,2],[2,1],[1,1]]return = [1,1,2,3,1]The last addition joins the top-left island and both isolated cells, leaving one island.
Example 2
m = 1n = 2positions = [[0,0],[0,0],[0,1]]return = [1,1,1]The repeated first cell changes nothing. The final cell joins the existing island.
Constraints
- 1 <= m, n <= 100
- 1 <= positions.length <= 10000
- Each position is [row, column] with 0 <= row < m and 0 <= column < n.
More Citadel Phone Screen questions
- Portfolio Tickers with Constant-Time Random PickPHONE SCREEN · Seen Nov 2026
- Square Root FunctionPHONE SCREEN · Seen Oct 2026
- Reachable Transfer PositionsPHONE SCREEN · Seen Oct 2026
- Spreadsheet Formula DependenciesPHONE SCREEN · Seen Oct 2026
- Merge Price-Delta FeedsPHONE SCREEN · Seen Sep 2026
- Reconcile Unmatched Trades with Timestamp TolerancePHONE SCREEN · Seen Sep 2026
- Top Ten Trades by Notional ValuePHONE SCREEN · Seen Sep 2026
- Exchange BBO and Market-Wide NBBOPHONE SCREEN · Seen Sep 2026
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Capture Surrounded Regions with Union-FindApple · PHONE SCREENApple interview questions
- Minimum Rooks in a Peaceful State (SDE 2, Phone Interview)Amazon · PHONE SCREEN
- Count the IslandsAdobe · PHONE SCREENAdobe interview questions
- Count Islands in a GridDoorDash · PHONE SCREENDoorDash interview questions
- Dynamic Island CountsUber · PHONE SCREENUber interview questions
- Number of Islands IISpaceX · PHONE SCREENSpaceX interview questions
- Connected GroupsFivetran · ONSITE INTERVIEWFivetran interview questions
- Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
- Rectangular K-in-a-RowDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Connected GroupsVisa · OAVisa interview questions