Search a Matrix with Ordered Row Boundaries

Quick Overview

Search for a value in a row-ordered matrix within logarithmic time, preserving non-decreasing rows and strictly separated row boundaries.

Search a Matrix with Ordered Row Boundaries

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem The rectangular integer matrix `table` has these ordering properties: - Each row is non-decreasing from left to right. - The first entry of every row after the first is strictly larger than the final entry of the preceding row. Given an integer `key`, return `true` if it occurs anywhere in `table`, and return `false` otherwise. Your solution must run in `O(log(rows * columns))` time, where `rows` and `columns` are the matrix dimensions. ### Examples **Example 1** ```text Input: table = [[-5,-5,1],[4,6,8]], key = -5 Output: true ``` The key appears in the first row. Repeated values within one row are permitted. **Example 2** ```text Input: table = [[0,2],[5,9]], key = 4 Output: false ``` None of the matrix entries equals the key. ### Constraints - The number of rows and the number of columns are each between `1` and `100`, inclusive. - Every row has equal length and obeys the stated ordering rules. - Every entry of `table`, and `key`, lies between `-10^4` and `10^4`, inclusive.

Overview: Search for a value in a row-ordered matrix within logarithmic time, preserving non-decreasing rows and strictly separated row boundaries.

Google logo
Google
Oct 13, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

The rectangular integer matrix table has these ordering properties:

  • Each row is non-decreasing from left to right.
  • The first entry of every row after the first is strictly larger than the final entry of the preceding row.

Given an integer key, return true if it occurs anywhere in table, and return false otherwise.

Your solution must run in O(log(rows * columns)) time, where rows and columns are the matrix dimensions.

Examples

Example 1

Input: table = [[-5,-5,1],[4,6,8]], key = -5
Output: true

The key appears in the first row. Repeated values within one row are permitted.

Example 2

Input: table = [[0,2],[5,9]], key = 4
Output: false

None of the matrix entries equals the key.

Constraints

  • The number of rows and the number of columns are each between 1 and 100 , inclusive.
  • Every row has equal length and obeys the stated ordering rules.
  • Every entry of table , and key , lies between -10^4 and 10^4 , inclusive.