Pad uneven integer arrays with trailing zeros into a rectangular matrix

Quick Overview

Pad a collection of integer arrays of different lengths with trailing zeros so every row matches the longest one, and return the rectangular matrix. It tests finding a maximum length in one pass, building new rows without aliasing, and handling empty rows.

Pad uneven integer arrays with trailing zeros into a rectangular matrix

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a collection of integer arrays `arrays` with different lengths, pad each array with zeros at the end so that all arrays have the same length, equal to the length of the longest array. Return the resulting 2D matrix, in which every row has that uniform length. ### Function Signature ```python def pad_matrix(arrays: list[list[int]]) -> list[list[int]]: ``` ### Rules - Rows stay in their original order, and each row keeps its original values in their original positions. Zeros are only added after the last element. - If every array is empty, the longest length is 0 and every row stays empty. ### Constraints - `1 <= len(arrays) <= 100` - `0 <= len(arrays[i]) <= 100` - `-10^4 <= arrays[i][j] <= 10^4` ### Examples ```text Input: arrays = [[1,2,3],[4,5],[6]] Output: [[1,2,3],[4,5,0],[6,0,0]] ``` The longest array has length 3. `[4,5]` is padded to `[4,5,0]` and `[6]` is padded to `[6,0,0]`. ```text Input: arrays = [[1],[2,3],[4,5,6],[7,8,9,10]] Output: [[1,0,0,0],[2,3,0,0],[4,5,6,0],[7,8,9,10]] ``` The longest array has length 4. Each shorter array is padded with zeros to length 4, and the last row is unchanged. ```text Input: arrays = [[],[-3,7],[]] Output: [[0,0],[-3,7],[0,0]] ``` The longest array has length 2, so each empty array becomes two zeros.

Overview: Pad a collection of integer arrays of different lengths with trailing zeros so every row matches the longest one, and return the rectangular matrix. It tests finding a maximum length in one pass, building new rows without aliasing, and handling empty rows.

NVIDIA logo
NVIDIA
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a collection of integer arrays arrays with different lengths, pad each array with zeros at the end so that all arrays have the same length, equal to the length of the longest array.

Return the resulting 2D matrix, in which every row has that uniform length.

Function Signature

def pad_matrix(arrays: list[list[int]]) -> list[list[int]]:

Rules

  • Rows stay in their original order, and each row keeps its original values in their original positions. Zeros are only added after the last element.
  • If every array is empty, the longest length is 0 and every row stays empty.

Constraints

  • 1 <= len(arrays) <= 100
  • 0 <= len(arrays[i]) <= 100
  • -10^4 <= arrays[i][j] <= 10^4

Examples

Input: arrays = [[1,2,3],[4,5],[6]]
Output: [[1,2,3],[4,5,0],[6,0,0]]

The longest array has length 3. [4,5] is padded to [4,5,0] and [6] is padded to [6,0,0].

Input: arrays = [[1],[2,3],[4,5,6],[7,8,9,10]]
Output: [[1,0,0,0],[2,3,0,0],[4,5,6,0],[7,8,9,10]]

The longest array has length 4. Each shorter array is padded with zeros to length 4, and the last row is unchanged.

Input: arrays = [[],[-3,7],[]]
Output: [[0,0],[-3,7],[0,0]]

The longest array has length 2, so each empty array becomes two zeros.