Find the largest value in every 3x3 window of a square matrix

Quick Overview

Given an n x n integer matrix, build the (n - 2) x (n - 2) matrix that holds the largest value of each overlapping 3x3 window. It tests 2D array indexing, correct loop bounds for sliding windows, and building an output grid of the right size.

Find the largest value in every 3x3 window of a square matrix

Company: OpenAI

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an `n x n` integer matrix `grid`. Generate an integer matrix `maxLocal` of size `(n - 2) x (n - 2)` such that `maxLocal[i][j]` is equal to the largest value of the `3 x 3` matrix in `grid` centered around row `i + 1` and column `j + 1`. In other words, find the largest value in every contiguous `3 x 3` matrix in `grid`. Return the generated matrix. ### Function Signature ```python def largest_local(grid: list[list[int]]) -> list[list[int]]: ``` ### Rules - `grid` is a list of `n` rows with `n` integers each. - Return a list of `n - 2` rows with `n - 2` integers each. Entry `[i][j]` is the maximum of `grid[r][c]` over rows `r` from `i` to `i + 2` and columns `c` from `j` to `j + 2`. - Windows overlap, so one cell of `grid` can be the maximum of several output entries. ### Constraints - `n == len(grid) == len(grid[i])` - `3 <= n <= 100` - `1 <= grid[i][j] <= 100` ### Examples ```text Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]] Output: [[9,9],[8,6]] ``` The two top windows both include a 9 from row 0. The bottom-left window (rows 1-3, columns 0-2) peaks at the 8 in `grid[2][0]`, and the bottom-right window (rows 1-3, columns 1-3) peaks at 6. ```text Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]] Output: [[2,2,2],[2,2,2],[2,2,2]] ``` All nine 3x3 windows of a 5x5 grid contain the center cell `grid[2][2]`, so every entry is 2. ```text Input: grid = [[3,1,2],[1,7,1],[2,1,4]] Output: [[7]] ``` A 3x3 grid has exactly one window, the grid itself, and its largest value is 7.

Overview: Given an n x n integer matrix, build the (n - 2) x (n - 2) matrix that holds the largest value of each overlapping 3x3 window. It tests 2D array indexing, correct loop bounds for sliding windows, and building an output grid of the right size.

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

You are given an n x n integer matrix grid.

Generate an integer matrix maxLocal of size (n - 2) x (n - 2) such that maxLocal[i][j] is equal to the largest value of the 3 x 3 matrix in grid centered around row i + 1 and column j + 1.

In other words, find the largest value in every contiguous 3 x 3 matrix in grid.

Return the generated matrix.

Function Signature

def largest_local(grid: list[list[int]]) -> list[list[int]]:

Rules

  • grid is a list of n rows with n integers each.
  • Return a list of n - 2 rows with n - 2 integers each. Entry [i][j] is the maximum of grid[r][c] over rows r from i to i + 2 and columns c from j to j + 2 .
  • Windows overlap, so one cell of grid can be the maximum of several output entries.

Constraints

  • n == len(grid) == len(grid[i])
  • 3 <= n <= 100
  • 1 <= grid[i][j] <= 100

Examples

Input: grid = [[9,9,8,1],[5,6,2,6],[8,2,6,4],[6,2,2,2]]
Output: [[9,9],[8,6]]

The two top windows both include a 9 from row 0. The bottom-left window (rows 1-3, columns 0-2) peaks at the 8 in grid[2][0], and the bottom-right window (rows 1-3, columns 1-3) peaks at 6.

Input: grid = [[1,1,1,1,1],[1,1,1,1,1],[1,1,2,1,1],[1,1,1,1,1],[1,1,1,1,1]]
Output: [[2,2,2],[2,2,2],[2,2,2]]

All nine 3x3 windows of a 5x5 grid contain the center cell grid[2][2], so every entry is 2.

Input: grid = [[3,1,2],[1,7,1],[2,1,4]]
Output: [[7]]

A 3x3 grid has exactly one window, the grid itself, and its largest value is 7.