Search a Matrix with Ordered Rows and Columns

Search a Matrix with Ordered Rows and Columns

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Search a Matrix with Ordered Rows and Columns Given a rectangular integer `matrix` and an integer `target`, return whether the matrix contains the target. Each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Use these ordering properties to search efficiently. ### Function `bool searchMatrix(vector<vector<int>>& matrix, int target)` ### Example 1 ```text matrix = [[-3,0,4,9],[-1,2,6,12],[1,5,8,15]] target = 6 Output: true ``` The value `6` is at row `1`, column `2`, using zero-based indices. ### Example 2 ```text matrix = [[-3,0,4,9],[-1,2,6,12],[1,5,8,15]] target = 7 Output: false ``` ### Constraints - `1 <= matrix.length <= 300`. - The matrix is rectangular, with between `1` and `300` columns. - `-1000000000 <= matrix[i][j] <= 1000000000`. - `-1000000000 <= target <= 1000000000`. - Every row and every column has the stated ascending order. ### Clarifications There is no guarantee that reading an entire row and then the next row gives one globally sorted sequence. Return only a boolean; no target position is required. A uniqueness condition on matrix entries is not provided. ### What a Strong Answer Covers - A search decision justified by both row order and column order. - Why discarded positions cannot contain the target. - An absent target, a single row or column, and values at the matrix boundaries. - Time and auxiliary-space costs in terms of the row and column counts.
Amazon logo
Amazon
Sep 28, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Search a Matrix with Ordered Rows and Columns

Given a rectangular integer matrix and an integer target, return whether the matrix contains the target. Each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom.

Use these ordering properties to search efficiently.

Function

bool searchMatrix(vector<vector<int>>& matrix, int target)

Example 1

matrix = [[-3,0,4,9],[-1,2,6,12],[1,5,8,15]]
target = 6
Output: true

The value 6 is at row 1, column 2, using zero-based indices.

Example 2

matrix = [[-3,0,4,9],[-1,2,6,12],[1,5,8,15]]
target = 7
Output: false

Constraints

  • 1 <= matrix.length <= 300 .
  • The matrix is rectangular, with between 1 and 300 columns.
  • -1000000000 <= matrix[i][j] <= 1000000000 .
  • -1000000000 <= target <= 1000000000 .
  • Every row and every column has the stated ascending order.

Clarifications

There is no guarantee that reading an entire row and then the next row gives one globally sorted sequence. Return only a boolean; no target position is required. A uniqueness condition on matrix entries is not provided.

What a Strong Answer Covers Guidance

  • A search decision justified by both row order and column order.
  • Why discarded positions cannot contain the target.
  • An absent target, a single row or column, and values at the matrix boundaries.
  • Time and auxiliary-space costs in terms of the row and column counts.