Quick Overview

Search a row-and-column sorted matrix with staircase elimination, proving why each move removes a safe region in linear rows-plus-columns time.

Search a Matrix Sorted by Rows and Columns

Company: ByteDance

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given a rectangular matrix whose rows are sorted ascending left to right and columns ascending top to bottom, determine whether it contains a target value. ### Clarifying Questions to Ask - Are both rows and columns sorted, rather than the entire matrix flattened into one sorted sequence? - Can the matrix be empty or contain duplicate values? ```hint Choose a corner with opposite directions At one corner, moving in one direction increases values while moving in the other decreases them. ``` ### What a Strong Answer Covers - A staircase search from top-right or bottom-left. - Eliminating an entire row or column after each comparison. - O(rows+columns) time, empty input, and duplicate values. ### Follow-up Questions - Why is binary search on the flattened matrix not generally valid? - When might searching each row with binary search be preferable?

Overview: Search a row-and-column sorted matrix with staircase elimination, proving why each move removes a safe region in linear rows-plus-columns time.

Given a rectangular `matrix` and an integer `target`, return whether the matrix contains the target. Each row is sorted in nondecreasing order from left to right, and each column is sorted in nondecreasing order from top to bottom. Duplicate values are allowed. An empty matrix or a matrix with no columns contains no values.

Constraints

  • All rows have the same length. The row count, column count, and total number of cells each fit a signed 32-bit length.
  • The rows and columns are independently sorted in nondecreasing order.
  • Matrix values and the target are integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Examples

Input: ([[1, 5, 9], [3, 7, 12], [6, 10, 16]], 7)

Expected Output: True

Explanation: The middle cell contains 7, even though a row can start below the end of the previous row.

Input: ([[0, 2, 7], [1, 3, 9], [4, 6, 12]], 8)

Expected Output: False

Explanation: The target lies between two matrix values and does not occur.

Hints

  1. Try tracing a comparison in a matrix where the first value in the second row is smaller than the last value in the first row.

Search a Matrix Sorted by Rows and Columns

mediumByteDanceMachine Learning Engineer

Given a rectangular matrix and an integer target, return whether the matrix contains the target. Each row is sorted in nondecreasing order from left to right, and each column is sorted in nondecreasing order from top to bottom. Duplicate values are allowed. An empty matrix or a matrix with no columns contains no values.

View full question (all parts)

Given a rectangular matrix whose rows are sorted ascending left to right and columns ascending top to bottom, determine whether it contains a target value.

Clarifying Questions to Ask

  • Are both rows and columns sorted, rather than the entire matrix flattened into one sorted sequence?
  • Can the matrix be empty or contain duplicate values?

What a Strong Answer Covers

  • A staircase search from top-right or bottom-left.
  • Eliminating an entire row or column after each comparison.
  • O(rows+columns) time, empty input, and duplicate values.

Follow-up Questions

  • Why is binary search on the flattened matrix not generally valid?
  • When might searching each row with binary search be preferable?

Examples

Example 1
Input
([[1, 5, 9], [3, 7, 12], [6, 10, 16]], 7)
Output
True
Notes
The middle cell contains 7, even though a row can start below the end of the previous row.
Example 2
Input
([[0, 2, 7], [1, 3, 9], [4, 6, 12]], 8)
Output
False
Notes
The target lies between two matrix values and does not occur.

Constraints

  • •All rows have the same length. The row count, column count, and total number of cells each fit a signed 32-bit length.
  • •The rows and columns are independently sorted in nondecreasing order.
  • •Matrix values and the target are integers from -(2^53 - 1) to 2^53 - 1, inclusive.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def search_sorted_matrix(matrix: list[list[int]], target: int) -> bool:
Sample call
search_sorted_matrix([[1, 5, 9], [3, 7, 12], [6, 10, 16]], 7)
Language
Case 1
Input
([[1, 5, 9], [3, 7, 12], [6, 10, 16]], 7)
Expected
True
Case 2
Input
([[0, 2, 7], [1, 3, 9], [4, 6, 12]], 8)
Expected
False

Browse More Questions

Show the approach

Approach

Start at the top-right cell of the remaining rectangle. If the cell equals the target, return true. If it is greater than the target, the remaining values below it in that column are at least as large, so discard that column by moving left. If it is smaller, all remaining values to its left in the row are at most as large, so discard that row by moving down.

The remaining rectangle preserves every position that could still contain the target. Each step removes a row or a column, so eventually a match is found or no candidates remain. Empty shapes return false immediately.

Time complexity:
O(rows + columns).
Space complexity:
O(1) auxiliary space.