PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW
|Home/Coding & Algorithms/Bloomberg

Count islands by requested sizes

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of grid connectivity, identification of connected components, and the ability to aggregate component sizes for frequency queries.

  • medium
  • Bloomberg
  • Coding & Algorithms
  • Software Engineer

Count islands by requested sizes

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a 2D grid of characters `'0'` and `'1'`, where `'1'` represents land and `'0'` represents water. An **island** is a maximal group of horizontally/vertically (4-directionally) adjacent land cells. The **size** of an island is the number of land cells in that island. You are also given an integer array `queries`, where each element is a size to query. Return an integer array `ans` of the same length as `queries`, where `ans[i]` equals the number of islands in the grid whose size is exactly `queries[i]`. ### Example If the grid contains: - 2 islands of size 10 - 0 islands of size 1 - 5 islands of size 2 - 0 islands of size 3 Then for `queries = [10, 1, 2, 3]`, return `ans = [2, 0, 5, 0]`. ### Constraints (reasonable interview defaults) - `1 <= rows, cols <= 2000` (or smaller depending on the language/time limit) - `grid[r][c] ∈ {'0','1'}` - `1 <= queries.length <= 2e5` - Query sizes are positive integers ### Notes - Use 4-directional adjacency only (no diagonals). - `queries` may contain duplicates; return counts for each position.

Quick Answer: This question evaluates a candidate's understanding of grid connectivity, identification of connected components, and the ability to aggregate component sizes for frequency queries.

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
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
15
0
Loading...

You are given a 2D grid of characters '0' and '1', where '1' represents land and '0' represents water. An island is a maximal group of horizontally/vertically (4-directionally) adjacent land cells.

The size of an island is the number of land cells in that island.

You are also given an integer array queries, where each element is a size to query.

Return an integer array ans of the same length as queries, where ans[i] equals the number of islands in the grid whose size is exactly queries[i].

Example

If the grid contains:

  • 2 islands of size 10
  • 0 islands of size 1
  • 5 islands of size 2
  • 0 islands of size 3

Then for queries = [10, 1, 2, 3], return ans = [2, 0, 5, 0].

Constraints (reasonable interview defaults)

  • 1 <= rows, cols <= 2000 (or smaller depending on the language/time limit)
  • grid[r][c] ∈ {'0','1'}
  • 1 <= queries.length <= 2e5
  • Query sizes are positive integers

Notes

  • Use 4-directional adjacency only (no diagonals).
  • queries may contain duplicates; return counts for each position.

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.