PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of graph connectivity and grid traversal, specifically the detection of connected components within a 2D matrix. It is commonly asked to assess algorithmic problem-solving, efficiency and handling of matrix-based data structures; it belongs to the Coding & Algorithms domain and tests practical implementation skills alongside conceptual understanding of graph-related complexity under the given input constraints.

  • eBay
  • Coding & Algorithms
  • Software Engineer

Count Connected Land Regions

Company: eBay

Role: Software Engineer

Category: Coding & Algorithms

Interview Round: Technical Screen

You are given an `m x n` 2D grid representing a map. Each cell is either land (`'1'`) or water (`'0'`). A land region is formed by connecting adjacent land cells horizontally or vertically. Diagonal connections do not count. Write a function that returns the number of distinct connected land regions in the grid. **Input:** - `grid`: a 2D array of characters, where each value is either `'1'` or `'0'`. **Output:** - An integer representing the number of connected land regions. **Example:** ```text Input: grid = [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ] Output: 3 ``` **Constraints:** - `1 <= m, n <= 300` - `grid[i][j]` is either `'0'` or `'1'`.

Quick Answer: This question evaluates understanding of graph connectivity and grid traversal, specifically the detection of connected components within a 2D matrix. It is commonly asked to assess algorithmic problem-solving, efficiency and handling of matrix-based data structures; it belongs to the Coding & Algorithms domain and tests practical implementation skills alongside conceptual understanding of graph-related complexity under the given input constraints.

You are given an m x n grid representing a map. Each cell contains either '1' for land or '0' for water. A land region is a group of land cells connected horizontally or vertically. Diagonal cells are not connected. Return the number of distinct connected land regions in the grid.

Constraints

  • 1 <= m, n <= 300
  • grid[i][j] is either '0' or '1'

Examples

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

Expected Output: 3

Explanation: There are three separate groups of connected land cells.

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

Expected Output: 0

Explanation: A single water cell contains no land regions.

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

Expected Output: 1

Explanation: A single land cell forms one region by itself.

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

Expected Output: 5

Explanation: Diagonal connections do not count, so each land cell is its own region.

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

Expected Output: 1

Explanation: All land cells are connected through horizontal and vertical paths, forming one region.

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

Expected Output: 3

Explanation: The row contains three separate land groups: ['1'], ['1','1'], and ['1'].

Hints

  1. Treat each land cell as a node in a graph, where edges exist only in the four cardinal directions.
  2. Whenever you find an unvisited land cell, run BFS or DFS from it to mark the entire region before continuing.
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

  • Format Words into Fixed-Width Lines - eBay (medium)
  • Assign Ads to Browser Positions - eBay (medium)
  • Solve Dependency, Prefix, and Cache Problems - eBay (medium)
  • Implement an In-Memory File System - eBay (medium)
  • Find top co-viewed products - eBay (hard)