FastPrepCount One Groups by Size
Problems/Count One Groups by Size

Count One Groups by Size

Agoda logoAgoda● MediumCONTRACTOA

Updated Jul 2025

QuestionFunctionExamplesConstraints

Problem statement

In a square grid, two cells are connected if they share an edge and contain the same value. Sharing an edge means up, down, left, or right, but not diagonal.

Given a square integer grid grid and an array queries, consider only connected groups of cells whose value is 1. For each query value, return how many connected groups of 1s have size exactly equal to that query.

Return an array where the i-th element is the answer for queries[i].

Function

onesGroups(grid: int[][], queries: int[]) → int[]

Examples

Example 1

grid = [[1,1,1,1,1,1],[1,1,0,0,0,0],[0,0,0,1,1,1],[0,0,0,1,1,1],[0,0,1,0,0,0],[1,0,0,0,0,0]]queries = [6,1,8,2]return = [1,2,1,0]
grid
012345011111111100002000111300011140010005100000

The grid contains four connected groups of 1s: one group with 8 cells, one group with 6 cells, and two groups with 1 cell. Therefore the answers for [6,1,8,2] are [1,2,1,0].

Constraints

  • grid is a non-empty square matrix.
  • grid[i][j] is either 0 or 1.
  • queries.length >= 1

More Agoda Online Assessment questions

  • Special Diameter EndpointsOA · Seen Nov 2025
  • TV Series in a Production WindowOA · Seen Nov 2025
  • Minimum Price With Discount CouponsOA · Seen Jul 2025
  • Lexicographically Smallest Task QueueOA · Seen Jul 2025
  • Highest-Ranked University by CountryOA · Seen Jun 2025
  • Jump Game with Prime-3 StepsOA · Seen Jun 2025
  • Team FormationOA · Seen Apr 2025
  • Two-Core Process AssignmentOA · Seen Apr 2025
  • Unique Digits in RangeOA · Seen Apr 2025
  • Maximize Sum of Processed TimesOA · Seen Sep 2024

Similar questions at other companies

  • Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
  • Shortest Atom Action PathOpenAI · OAOpenAI interview questions
  • Minimum Cleaning Robot RunsHSBC · OAHSBC interview questions
  • Count Islands with DFS and BFSVisa · OAVisa interview questions
  • Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Ascend Vertical Wall (For off-campus 2025 batch hiring :)Uber · OAUber interview questions
  • Grid ExplorationIMC Trading · OAIMC Trading interview questions
  • Maximum Damage PatchGecko Robotics · OA · PHONE SCREEN
  • Nearest Different Maze Exit for a SnakeIntuit · OAIntuit interview questions
  • Minimum Grid MovesAtlassian · OAAtlassian interview questions

Agoda interview experiences

  • Agoda Staff Backend OA Questions: 5 Experiences
All Agoda interview questionsSee Agoda hiring insights
Saved
class Solution {
    public int[] onesGroups(int[][] grid, int[] queries) {
      // write your code here
    }
}
Checking account…