Count Islands in a New Survey Map Fully Inside the Old Map

Last updated: November 29, 2026

Quick Overview

Given two binary grids of equal size, grid1 (old survey) and grid2 (new survey), count the islands in grid2 for which every land cell is also land in grid1.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given two binary grids of equal size, grid1 (old survey) and grid2 (new survey), count the islands in grid2 for which every land cell is also land in grid1.

A technical screen question themed on comparing two versions of a Maps land layer. It checks whether you traverse each grid2 island completely even after finding a disqualifying cell, a subtle bug that splits one island into several.

What the Interviewer Expects
  • Traverse each island in grid2 with BFS or DFS
  • Check each cell against grid1
  • Keep exploring after a mismatch so the island is fully marked
  • Count islands whose all cells matched
  • Explain why early exit causes over-counting
Key Topics to Cover
connected components
grid
BFS
set containment
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Count islands in grid2 that overlap grid1 by at least half their area.
  • Return islands of grid1 that disappeared entirely in grid2.
  • What if the grids are huge and tiled?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Inputs: grid1, grid2, same R by C, values 0/1.
  • An island in grid2 is a sub-island if all its cells are 1 in grid1.
  • Output: count of sub-islands....
High-Level Design

Loop over grid2. When an unvisited land cell starts a new island, BFS it fully, and AND together grid1[r][c] == 1 across all its cells. Only after the BFS finishes do we decide whether to count it. ...


Submit Your Answer
Markdown supported