Reported September 2026
Microsoftmatrix

Foreground Region Sizes

Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Microsoft OA. Under 2s to a working solution.
Founder's read

Microsoft reportedly asked this one in September 2026, and the constraint is what matters: a grid up to 1000 by 1000 means a million cells. Anything that re-scans for each region or revisits cells will die. It's a connected components problem on a matrix, and the twist is that you return sizes in first-encounter order, not sorted. If you've got an OA invite and 48 hours, this is flood fill with one clean rule. Visit every cell once, measure each region as you find it, and keep the output order. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and gives you the solution in real time.

The problem

A rectangular binary matrix marks background with 0 and foreground with 1. Foreground cells belong to the same region when connected horizontally or vertically.
Scan the matrix in row-major order. Whenever an unvisited foreground cell is encountered, measure its complete region and append the region size to the result. Return sizes in that first-encounter order; do not sort them.

Function
getRegionSizes(grid: int[][]) → int[]

Examples
Example 1
grid = [[1,1,0,1],[0,1,0,0],[1,0,1,1]]
return = [3,1,1,2]
The regions are discovered from their first cells at (0,0), (0,3), (2,0), and (2,2).
Example 2
grid = [[0,0],[0,0]]
return = []
There is no foreground.
Example 3
grid = [[1,0,1],[1,1,1]]
return = [5]
All five foreground cells are connected through the second row.

Constraints
0 <= grid.length <= 1000.
An empty grid returns an empty array.
Non-empty rows have equal length at most 1000 and contain only 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a single row-major scan with a visited marker. When you hit an unvisited 1, run a flood fill from it, count the cells, mark them visited, and push the count. Each cell gets touched a constant number of times, so it's O(rows * cols). The order falls out for free because you append in discovery order. Don't sort. The big pitfall is recursion depth. A 1000 by 1000 grid of all 1s can produce a million-deep DFS, which blows the stack in most languages. Use an explicit stack or a BFS queue instead. Other traps: an empty grid (length 0, so don't read grid[0]), and diagonal neighbors, which do not count. Mark cells visited when you push them, not when you pop them, or you'll double count. If the iterative version slips your mind live, StealthCoder is the hedge that hands you a working one.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Foreground Region Sizes 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Microsoft's OA.

Microsoft reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Foreground Region Sizes FAQ

What's the trick in Foreground Region Sizes?+

It's connected components via flood fill. Scan row-major, and on each unvisited 1, run BFS or an iterative DFS to count its region. Append the count to the result. Because you scan in order, the output is already in first-encounter order, so there's nothing to sort.

Why can't I just use recursive DFS?+

The grid can be 1000 by 1000, and a fully connected foreground can push recursion about a million calls deep. That overflows the stack in most languages. Use an explicit stack or a queue. It's the same logic and it's safe at the max input size.

Do diagonal cells count as connected?+

No. The problem says regions connect horizontally or vertically only. So you check four neighbors: up, down, left, right. Adding diagonals gives wrong sizes on Example 1, where (0,1) and (1,1) connect but nothing links diagonally.

What edge cases should I test before submitting?+

Test an empty grid (length 0, return an empty array), a grid of all zeros, a single cell of 1, and a full grid of 1s for max size. Also check Example 3, where a region wraps through the second row. Guard against reading grid[0] when the grid is empty.

How do I prep for this in 48 hours?+

Write the iterative BFS flood fill from scratch twice, on a grid. Make sure you can mark visited on push, handle bounds, and return sizes in order. Then do one variant, like max area of island. This pattern shows up constantly, so it's worth the time.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Microsoft.

OA at Microsoft?
Invisible during screen share
Get it