Largest Equal-Arm T-Shape of 1s in a Binary Matrix

Quick Overview

Find the largest arm length of a T-shape made of 1s in a binary matrix, where the crossbar and stem arms all have the same length and the stem can point in any of four directions. It tests directional run-length precomputation on a grid and turning a per-cell check into a linear-time scan.

Largest Equal-Arm T-Shape of 1s in a Binary Matrix

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an `m x n` binary matrix of `0`s and `1`s, find the largest arm length of a T-shape made entirely of `1`s. A T-shape has a center cell, a crossbar that extends from the center in two opposite directions, and a stem that extends from the center in a third direction, perpendicular to the crossbar. The T can appear in any of the 4 rotations: the stem can point up, down, left, or right. All 3 arms must have the same length. The arm length is the number of `1`s in each of the 3 arms, not counting the center. Return the largest arm length of any valid T-shape, or `0` if no valid T-shape exists. ### Function Signature ```python def largest_t(matrix: list[list[int]]) -> int: ``` ### Rules - Cells are written as `(row, column)`, 0-indexed, with row 0 at the top. - A T with center `(i, j)` and arm length `k` is valid when the center and the `k` cells in each of its 3 arm directions are all `1`. The cell just past the end of an arm and every cell in the fourth direction can hold anything, so a longer run of `1`s still contains a T of every shorter arm length. - If no T with arm length at least 1 exists, return `0`. ### Constraints - `1 <= m, n <= 200`, where `m` is the number of rows and `n` is the number of columns - `matrix[i][j]` is either `0` or `1` - Every row has the same length ### Examples ```text Input: matrix = [ [1,1,1,1,1], [0,0,1,0,0], [0,0,1,0,0], [0,0,1,0,0] ] Output: 2 ``` The center is `(0, 2)`. Its crossbar runs along row 0 with two `1`s on each side, and its stem runs down column 2 with three `1`s, so the longest equal arm length is `min(2, 2, 3) = 2`. No other cell has `1`s in three directions. ```text Input: matrix = [ [0,1,0], [1,1,1], [0,1,0], [0,1,0], [0,1,0] ] Output: 1 ``` The center `(1, 1)` has one `1` above, one to the left, one to the right and three below. Every orientation at that center has a shortest arm of 1. The cells lower in column 1 have `1`s only above and below, so they cannot be the center of a T. ```text Input: matrix = [ [1,1], [1,1] ] Output: 0 ``` Every cell has a neighboring `1` in only two directions, and a T of arm length 1 needs three, so no valid T exists even though the matrix is all `1`s.

Overview: Find the largest arm length of a T-shape made of 1s in a binary matrix, where the crossbar and stem arms all have the same length and the stem can point in any of four directions. It tests directional run-length precomputation on a grid and turning a per-cell check into a linear-time scan.

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

Given an m x n binary matrix of 0s and 1s, find the largest arm length of a T-shape made entirely of 1s.

A T-shape has a center cell, a crossbar that extends from the center in two opposite directions, and a stem that extends from the center in a third direction, perpendicular to the crossbar. The T can appear in any of the 4 rotations: the stem can point up, down, left, or right. All 3 arms must have the same length.

The arm length is the number of 1s in each of the 3 arms, not counting the center. Return the largest arm length of any valid T-shape, or 0 if no valid T-shape exists.

Function Signature

def largest_t(matrix: list[list[int]]) -> int:

Rules

  • Cells are written as (row, column) , 0-indexed, with row 0 at the top.
  • A T with center (i, j) and arm length k is valid when the center and the k cells in each of its 3 arm directions are all 1 . The cell just past the end of an arm and every cell in the fourth direction can hold anything, so a longer run of 1 s still contains a T of every shorter arm length.
  • If no T with arm length at least 1 exists, return 0 .

Constraints

  • 1 <= m, n <= 200 , where m is the number of rows and n is the number of columns
  • matrix[i][j] is either 0 or 1
  • Every row has the same length

Examples

Input: matrix = [
  [1,1,1,1,1],
  [0,0,1,0,0],
  [0,0,1,0,0],
  [0,0,1,0,0]
]
Output: 2

The center is (0, 2). Its crossbar runs along row 0 with two 1s on each side, and its stem runs down column 2 with three 1s, so the longest equal arm length is min(2, 2, 3) = 2. No other cell has 1s in three directions.

Input: matrix = [
  [0,1,0],
  [1,1,1],
  [0,1,0],
  [0,1,0],
  [0,1,0]
]
Output: 1

The center (1, 1) has one 1 above, one to the left, one to the right and three below. Every orientation at that center has a shortest arm of 1. The cells lower in column 1 have 1s only above and below, so they cannot be the center of a T.

Input: matrix = [
  [1,1],
  [1,1]
]
Output: 0

Every cell has a neighboring 1 in only two directions, and a T of arm length 1 needs three, so no valid T exists even though the matrix is all 1s.