Biggest Connected Component
Reported by candidates from Codeium's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Codeium OA reported in July 2026 hides its real difficulty in one line: h times w can reach almost a million cells, and there's a test on a roughly 999 x 999 grid that has to finish fast. Trying every row and column, then flood-filling each time, is dead on arrival. This is a connected components problem with a twist, and the pattern is union-find or a labeled flood fill plus counting. If you blank in the live assessment, StealthCoder sits invisibly on your desktop as a safety net. Better to know the trick before you open the invite.
The problem
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 = 3 w = 5 grid = [[1, 0, 1, 0, 0], [0, 1, 0, 0, 0], [1, 0, 1, 0, 0]] return = 9 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Label every connected component once with DFS, BFS, or union-find, and record each component's size. Now for each candidate row r, the result is w new cells plus the sizes of all distinct components touching row r, plus those touching rows r-1 and r+1 (the ones that were empty in row r still count as new cells, so count carefully). Use a set of component ids per row so you don't double count a component that spans multiple adjacent rows. Do the same for columns. Total work is O(h*w). The common pitfall is adding a component's size twice, or counting cells in the filled line that were already 1 as new. Also handle the option of doing no operation. Iterative traversal beats recursion at this grid size, since deep recursion will overflow the stack. If you freeze mid-assessment, StealthCoder can supply the labeling and counting scaffold.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Biggest Connected Component cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Codeium's OA.
Codeium reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Biggest Connected Component FAQ
What's the trick in Biggest Connected Component?+
Don't simulate each operation. Label components once and store sizes. Then for each row or column, sum the distinct component sizes touching that line or its neighbors, plus the number of empty cells you fill. That turns an O((h+w)*h*w) approach into O(h*w).
Why does the 999 x 999 test matter?+
It rules out brute force. Re-running a flood fill for each of roughly 2000 possible operations over a million cells is billions of steps. The constraint tells you the answer must come from one precomputation pass plus cheap per-line lookups.
Should I use union-find or DFS here?+
Either works. Iterative BFS or DFS is simpler for labeling components and sizes. Union-find is fine too if you track size per root. Avoid recursive DFS on a million cells, since you risk a stack overflow in most languages.
What edge cases break most solutions?+
Double counting a component that touches both the filled line and its neighbor lines, forgetting that cells already filled in the chosen line aren't new, an all-zero grid, an all-one grid, and skipping the choice of doing nothing. Test a 1 x N and N x 1 grid too.
How do I prepare for this in 48 hours?+
Write a component-labeling routine from memory until it's automatic. Then practice the add-distinct-neighbors pattern with a set per line. Code the row case, then mirror it for columns. Check your answer against Example 1, which should return 9.