PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Oracle

Count closed islands in a grid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates grid traversal and connected-component detection skills, focusing on 4-directional adjacency and boundary-condition reasoning in matrix-based maps.

  • easy
  • Oracle
  • Coding & Algorithms
  • Data Scientist

Count closed islands in a grid

Company: Oracle

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem You are given an `m x n` 2D grid representing a map after flooding: - `0` = water - `1` = land An **island** is a maximal 4-directionally connected component of land cells (`1`s), where connectivity is via **up, down, left, right** (not diagonals). A **closed island** is an island that **does not touch the grid boundary** (i.e., none of its cells lie in the first/last row or first/last column). Any island that touches the boundary is **not counted**. ### Task Return the number of **closed islands** in the grid. ### Input - `grid`: `int[m][n]` ### Output - `int`: the number of closed islands ### Notes / Assumptions - Use 4-directional adjacency. - You may assume `m, n >= 1`. ### Example If an island is entirely surrounded by water but a land cell touches the border, it should **not** be counted as closed.

Quick Answer: This question evaluates grid traversal and connected-component detection skills, focusing on 4-directional adjacency and boundary-condition reasoning in matrix-based maps.

Related Interview Questions

  • Solve Five Coding Problems - Oracle (medium)
  • Compute letter frequencies from encoded string - Oracle (medium)
  • Implement in-memory data structures and booking API - Oracle (hard)
  • Implement an LRU cache - Oracle (medium)
  • Return a valid course completion order - Oracle (medium)
Oracle logo
Oracle
Feb 18, 2026, 11:23 PM
Data Scientist
Technical Screen
Coding & Algorithms
6
0
Loading...

Problem

You are given an m x n 2D grid representing a map after flooding:

  • 0 = water
  • 1 = land

An island is a maximal 4-directionally connected component of land cells (1s), where connectivity is via up, down, left, right (not diagonals).

A closed island is an island that does not touch the grid boundary (i.e., none of its cells lie in the first/last row or first/last column). Any island that touches the boundary is not counted.

Task

Return the number of closed islands in the grid.

Input

  • grid : int[m][n]

Output

  • int : the number of closed islands

Notes / Assumptions

  • Use 4-directional adjacency.
  • You may assume m, n >= 1 .

Example

If an island is entirely surrounded by water but a land cell touches the border, it should not be counted as closed.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Oracle•More Data Scientist•Oracle Data Scientist•Oracle Coding & Algorithms•Data Scientist Coding & Algorithms
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.