Search for a Target in a Matrix Sorted by Rows and Columns
Company: Goldman Sachs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Onsite
Given a matrix of integers in which every row is sorted in non-decreasing order from left to right and every column is sorted in non-decreasing order from top to bottom, determine whether a target value appears anywhere in the matrix.
### Function Signature
`search_sorted_matrix(matrix: list[list[int]], target: int) -> bool`
### Rules
- Return `True` if at least one cell equals `target`, and `False` otherwise.
- Values may repeat within a row, within a column, or across the matrix.
- A row does not have to start after the previous row ends: the first value of a row may be smaller than the last value of the row above it.
### Constraints
- `matrix` has `m` rows and `n` columns, with `1 <= m <= 300` and `1 <= n <= 300`; every row has length `n`.
- Every element and `target` is an integer in `[-1000000000, 1000000000]`.
- The intended solution does not examine every cell.
### Examples
Input: `matrix = [[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]], target = 9`
Output: `True`
Input: `matrix = [[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]], target = 15`
Output: `False`
Input: `matrix = [[-5,-5,0]], target = 1`
Output: `False`
Overview: Decide whether a target value appears in an integer matrix whose rows and columns are each sorted in non-decreasing order. It tests how well a candidate exploits ordering in two directions to avoid scanning every cell, and how carefully they handle duplicates, single-row inputs, and boundary values.
You are given a matrix of integers in which every row is sorted in non-decreasing order from left to right and every column is sorted in non-decreasing order from top to bottom. Given an integer `target`, determine whether `target` appears anywhere in the matrix.
Return `True` if at least one cell of the matrix equals `target`, and `False` otherwise. The answer is a single boolean, so there is no ordering or tie-breaking to decide: duplicates do not change the result, because you only report existence and never a position.
Values may repeat within a row, within a column, or across the matrix. A row does not have to start after the previous row ends: the first value of a row may be smaller than the last value of the row above it, so reading the matrix row by row into one flat list does not necessarily produce a sorted list.
The intended solution does not examine every cell.
Every element and `target` lies in `[-1000000000, 1000000000]`, so no value can exceed `2^31 - 1` and no arithmetic on these values is required; a 32-bit signed integer is sufficient in every language (Java `int`, C++ `int`).
### Examples
Example 1:
```
matrix = [[1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17]]
target = 9
-> True
```
The cell at row 2, column 2 holds 9, so the target is present. Note that this matrix illustrates the overlap rule: row 1 starts at 2, which is smaller than 11, the last value of row 0.
Example 2:
```
matrix = [[1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17]]
target = 15
-> False
```
The matrix contains 14 and 16 but no 15, so the answer is False.
Example 3:
```
matrix = [[-5, -5, 0]]
target = 1
-> False
```
A single row with a duplicated value and no cell above 0.
Constraints
- `matrix` has `m` rows and `n` columns, with `1 <= m <= 300` and `1 <= n <= 300`; every row has length `n`.
- Every row of `matrix` is sorted in non-decreasing order from left to right.
- Every column of `matrix` is sorted in non-decreasing order from top to bottom.
- Values may repeat within a row, within a column, or across the matrix.
- A row does not have to start after the previous row ends: the first value of a row may be smaller than the last value of the row above it.
- Every element of `matrix` and `target` is an integer in `[-1000000000, 1000000000]`; no value can exceed `2^31 - 1`, so a 32-bit signed integer type is sufficient.
- Return `True` if at least one cell equals `target`, and `False` otherwise.
- The intended solution does not examine every cell.
Examples
Input: ([[1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17]], 9)
Expected Output: True
Explanation: Source example 1: 9 sits at row 2, column 2, so the target is present.
Input: ([[1, 4, 7, 11], [2, 5, 8, 12], [3, 6, 9, 16], [10, 13, 14, 17]], 15)
Expected Output: False
Explanation: Source example 2: the matrix holds 14 and 16 but never 15, a gap between present values.
Hints
- Both sorted properties hold at the same time. Ask what a single comparison against one well-chosen cell can rule out: is there a starting position from which one comparison eliminates an entire row or an entire column of candidates?
- Reading the cells row by row into one flat list does not give you a sorted list, because a row may begin below where the previous row ended. Any approach that assumes a single global order over all cells is unsound on this input.
- You only have to report whether the value exists, not where it is or how many times, so repeated values never need to be disambiguated.