← Questions

Same-value region from a start pixel, and its border

0
MetaCodingLast asked Jul 2025

A coding interview question asked in Meta's AI-enabled coding round, last asked Jul 2025. It tests bfs, dfs and grids.

An image is a grid of 0s and 1s. Given a starting pixel, return every pixel connected to it (up, down, left or right) that has the same value as the start. Then return the region's border: the in-bounds pixels next to the region that are not part of it.

Part 1: the region

Unless you agree on a different interface with the interviewer, assume:

  • region(grid: list[list[int]], r: int, c: int) -> list[tuple[int, int]] — the cells of the region, sorted by (row, col).

Part 2: the border

  • border(grid: list[list[int]], r: int, c: int) -> list[tuple[int, int]] — the in-bounds cells that share an edge with the region but are not in it, each once, sorted by (row, col).

Example

grid = [[1, 1, 0],
        [1, 0, 0],
        [0, 0, 1]]

region(grid, 0, 0)  ->  [(0, 0), (0, 1), (1, 0)]
border(grid, 0, 0)  ->  [(0, 2), (1, 1), (2, 0)]

region(grid, 1, 1)  ->  [(0, 2), (1, 1), (1, 2), (2, 0), (2, 1)]
border(grid, 1, 1)  ->  [(0, 1), (1, 0), (2, 2)]

region([[1]], 0, 0) ->  [(0, 0)],  border  ->  []

Constraints

  • 1 <= rows, cols <= 1000; the start cell is in bounds.
  • Connectivity is 4-directional.

Clarifying questions

  • 4-directional or 8-directional connectivity?
  • In what order should the cells be returned?
  • Does the border include only in-bounds cells, and each cell once?
  • May I modify the grid while searching?

What a strong answer covers

  • A flood fill with BFS (queue) or iterative DFS (explicit stack), marking visited cells; O(R * C) time and space in the worst case.
  • The border from one sweep over the region's cells, collecting out-of-region neighbours into a set.
  • BFS versus recursive DFS: recursion depth can reach R * C on a large region, so iterate.
  • Tests: a one-cell grid, a region that fills the whole grid (empty border), a start on the edge.

Follow-ups

  • Recolour the region (flood fill) in place.
  • Return only the region's own cells that touch the border (its outline).
  • Answer many start pixels quickly by labelling every region once.

Where it's asked

  • Meta · AI-enabled coding · Last asked Jul 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

bfs · dfs · grids

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding