Rebuild a two-row binary matrix from its row sums and column sums

Quick Overview

Rebuild a binary matrix with two rows from its two row sums and its column sums, or return an empty list when no matrix fits. It tests a counting argument for feasibility, a greedy placement of the flexible columns, and a lexicographic tie rule that makes the answer unique.

Rebuild a two-row binary matrix from its row sums and column sums

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A binary matrix has 2 rows and `n` columns, and every entry is `0` or `1`. You do not see the entries. You only know three things about the matrix: - `upper` is the sum of the entries in row 0 (the upper row). - `lower` is the sum of the entries in row 1 (the lower row). - `colsum` is an array of length `n`, where `colsum[i]` is the sum of the two entries in column `i`. Reconstruct a matrix that matches all of these sums and return it as a list of two rows. If no such matrix exists, return an empty list. Several matrices can fit the same sums, so the rules below fix which one to return. ### Function Signature ```python def reconstruct_matrix(upper: int, lower: int, colsum: list[int]) -> list[list[int]]: ``` ### Rules - Return `[top_row, bottom_row]`: two lists of length `n` that hold only `0` and `1`. - When more than one matrix is valid, return the one whose upper row is lexicographically largest when read from left to right. Equivalently, among the columns with sum 1, the ones whose 1 goes to the upper row are the leftmost such columns. - When no valid matrix exists, return `[]` (an empty list, not two empty rows). ### Constraints - `1 <= len(colsum) <= 10^5` - `0 <= upper, lower <= len(colsum)` - `0 <= colsum[i] <= 2` ### Examples ```text Input: upper = 2, lower = 1, colsum = [1, 1, 1] Output: [[1, 1, 0], [0, 0, 1]] ``` The row sums are 2 and 1 and every column sums to 1. The matrices `[[1, 0, 1], [0, 1, 0]]` and `[[0, 1, 1], [1, 0, 0]]` also fit, but their upper rows are lexicographically smaller than `[1, 1, 0]`. ```text Input: upper = 2, lower = 2, colsum = [1, 2, 1] Output: [[1, 1, 0], [0, 1, 1]] ``` Column 1 sums to 2, so both of its cells are 1. Each row then needs one more 1, one from each remaining column, and the rule gives column 0 to the upper row. ```text Input: upper = 2, lower = 3, colsum = [2, 2, 1, 0, 1, 0] Output: [] ``` The column sums add up to 6 while the row sums add up to 5. Every 1 in the matrix counts toward exactly one row and exactly one column, so the two totals would have to match. No matrix fits.

Overview: Rebuild a binary matrix with two rows from its two row sums and its column sums, or return an empty list when no matrix fits. It tests a counting argument for feasibility, a greedy placement of the flexible columns, and a lexicographic tie rule that makes the answer unique.

Grab logo
Grab
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A binary matrix has 2 rows and n columns, and every entry is 0 or 1. You do not see the entries. You only know three things about the matrix:

  • upper is the sum of the entries in row 0 (the upper row).
  • lower is the sum of the entries in row 1 (the lower row).
  • colsum is an array of length n , where colsum[i] is the sum of the two entries in column i .

Reconstruct a matrix that matches all of these sums and return it as a list of two rows. If no such matrix exists, return an empty list. Several matrices can fit the same sums, so the rules below fix which one to return.

Function Signature

def reconstruct_matrix(upper: int, lower: int, colsum: list[int]) -> list[list[int]]:

Rules

  • Return [top_row, bottom_row] : two lists of length n that hold only 0 and 1 .
  • When more than one matrix is valid, return the one whose upper row is lexicographically largest when read from left to right. Equivalently, among the columns with sum 1, the ones whose 1 goes to the upper row are the leftmost such columns.
  • When no valid matrix exists, return [] (an empty list, not two empty rows).

Constraints

  • 1 <= len(colsum) <= 10^5
  • 0 <= upper, lower <= len(colsum)
  • 0 <= colsum[i] <= 2

Examples

Input: upper = 2, lower = 1, colsum = [1, 1, 1]
Output: [[1, 1, 0], [0, 0, 1]]

The row sums are 2 and 1 and every column sums to 1. The matrices [[1, 0, 1], [0, 1, 0]] and [[0, 1, 1], [1, 0, 0]] also fit, but their upper rows are lexicographically smaller than [1, 1, 0].

Input: upper = 2, lower = 2, colsum = [1, 2, 1]
Output: [[1, 1, 0], [0, 1, 1]]

Column 1 sums to 2, so both of its cells are 1. Each row then needs one more 1, one from each remaining column, and the rule gives column 0 to the upper row.

Input: upper = 2, lower = 3, colsum = [2, 2, 1, 0, 1, 0]
Output: []

The column sums add up to 6 while the row sums add up to 5. Every 1 in the matrix counts toward exactly one row and exactly one column, so the two totals would have to match. No matrix fits.