FastPrepBiggest Connected Component
Problems/Biggest Connected Component

Biggest Connected Component

Codeium logoCodeium● HardOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The screenshot cuts off the final output, but the grid, explanation, and stated rules determine it. FastPrep derived the displayed answer from those details and will update it if a fuller source becomes available.

Alice has a grid represented as a 2D integer array grid with h rows and w columns.

A cell is considered "filled" if its value is 1, and "empty" if its value is 0. A group of filled cells is connected if you can reach any cell from any other cell by moving horizontally or vertically. The size of a connected group is the number of cells in it.

Alice can perform one operation: choose a row or column and fill all its cells with 1.

Help Alice find the maximum possible size of the largest connected group of filled cells after performing the operation at most once.

Function

getBiggestConnectedComponent(h: int, w: int, grid: int[][]) → int

Examples

Example 1

h = 3w = 5grid = [[1, 0, 1, 0, 0], [0, 1, 0, 0, 0], [1, 0, 1, 0, 0]]return = 9
grid
01234010100101000210100

In this example, Alice should set the 2nd row to all 1s.

This connects all 5 cells in the 2nd row with the four originally filled cells in columns 1 and 3, so the largest connected group has 9 cells.

Constraints

  • The product of h and w is less than 1,000,000.
  • Your solution should work in a reasonable amount of time regardless of the contents of the grid. There are test cases checking that on a roughly 999 x 999 grid, your solution finishes in less than 1 second.

More Codeium Online Assessment questions

  • Eric's SequenceOA · Seen Sep 2024

Similar questions at other companies

  • Connected GroupsVisa · OAVisa interview questions
  • Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
  • Dynamic Island CountsUber · PHONE SCREENUber interview questions
  • Number of Islands IISpaceX · PHONE SCREENSpaceX interview questions
  • Largest Island After One Land ConversionGoogle · ONSITE INTERVIEWGoogle interview questions
  • Making a Large IslandMeta · ONSITE INTERVIEWMeta interview questions
  • Capture Surrounded Regions with Union-FindApple · PHONE SCREENApple interview questions
  • Connected GroupsFivetran · ONSITE INTERVIEWFivetran interview questions
  • Count Islands in a GridDoorDash · PHONE SCREENDoorDash interview questions
  • Disaster RecoveryHeadlands Technologies · OAHeadlands Technologies interview questions
All Codeium interview questionsSee Codeium hiring insights
Saved
class Solution {
    public int getBiggestConnectedComponent(int h, int w, int[][] grid) {
      // write your code here
    }

}
Checking account…