PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW
|Home/Coding & Algorithms/Bloomberg

Implement island counting with BFS and DFS

Last updated: Mar 29, 2026

Quick Overview

The question evaluates understanding and implementation of graph traversal techniques and connected-component detection on grids, contrasting recursive DFS with an explicit iterative BFS while requiring attention to edge cases such as empty or very large grids and different adjacency definitions.

  • Medium
  • Bloomberg
  • Coding & Algorithms
  • Software Engineer

Implement island counting with BFS and DFS

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Given an m x n grid of characters where '1' represents land and '0' represents water, count how many connected land regions exist. Two cells are connected if they share a side (up, down, left, right). Part A: implement a function countIslands(grid) using any approach. Part B: re-implement it using an explicit breadth-first search (no recursion). Handle edge cases such as an empty grid, single-cell grids, very large grids, and scenarios where land cells touch only at corners (treat them as separate regions). Analyze the time and space complexity of both approaches, and briefly describe how the solution would change if diagonal adjacency were also considered.

Quick Answer: The question evaluates understanding and implementation of graph traversal techniques and connected-component detection on grids, contrasting recursive DFS with an explicit iterative BFS while requiring attention to edge cases such as empty or very large grids and different adjacency definitions.

Related Interview Questions

  • Solve meeting and tree problems - Bloomberg (easy)
  • Minimize travel cost with two cities - Bloomberg (easy)
  • Check connectivity between two subway stations - Bloomberg (easy)
  • Design a data structure for dynamic top‑K frequency - Bloomberg (hard)
  • Find tree root and bucket numbers - Bloomberg (hard)
Bloomberg logo
Bloomberg
Jul 31, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
4
0

Given an m x n grid of characters where '1' represents land and '0' represents water, count how many connected land regions exist. Two cells are connected if they share a side (up, down, left, right). Part A: implement a function countIslands(grid) using any approach. Part B: re-implement it using an explicit breadth-first search (no recursion). Handle edge cases such as an empty grid, single-cell grids, very large grids, and scenarios where land cells touch only at corners (treat them as separate regions). Analyze the time and space complexity of both approaches, and briefly describe how the solution would change if diagonal adjacency were also considered.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Bloomberg•More Software Engineer•Bloomberg Software Engineer•Bloomberg Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,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.