FastPrepSearch a Row-Major Sorted Matrix
Problems/Search a Row-Major Sorted Matrix

Search a Row-Major Sorted Matrix

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand Motive onsite report for a full-time role names the well-known problem of searching a matrix whose rows are in sorted order, row by row.

Our version is about 99% the same as the reported question.

Given an integer matrix matrix and an integer target, return true when target appears in the matrix and false otherwise.

Each row is sorted in strictly increasing order. The first value of every row after the first is greater than the final value of the previous row.

Use a binary-search solution with logarithmic running time in the total number of cells.

Function

searchMatrix(matrix: int[][], target: int) → boolean

Examples

Example 1

matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]target = 3return = true
matrix
012301357110111620223303460

The value 3 is the second cell of the first row.

Example 2

matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]target = 13return = false
matrix
012301357110111620223303460

Binary search ends between 11 and 16, so 13 is absent.

Example 3

matrix = []target = 1return = false

An empty matrix contains no target.

Constraints

  • 0 <= matrix.length <= 100.
  • When matrix is non-empty, 1 <= matrix[i].length <= 100 and every row has the same length.
  • -10^9 <= matrix[i][j], target <= 10^9.
  • Rows satisfy the stated global ordering.

More Motive Onsite Interview questions

  • Group AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Longest Equal-Zero-One SubarrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Maximum Weighted Sum After Left RotationsONSITE INTERVIEW · Seen Jan 2026
  • Valid Parenthesis StringPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2026
  • Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Dec 2025
  • Target Triplet SumONSITE INTERVIEW · Seen Dec 2025
  • Validate IP AddressONSITE INTERVIEW · Seen Dec 2025
  • Generate Palindromic PermutationsONSITE INTERVIEW · Seen Dec 2025
  • Minimum Swaps to Group Circular OnesONSITE INTERVIEW · Seen Dec 2025
  • Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Dec 2025

Similar questions at other companies

  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Row with Maximum OnesAmazon · ONSITE INTERVIEW
  • Grid Path With the Most ClearanceDocuSign · OADocuSign interview questions
  • Search a 2D Matrix IIBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
  • Server SelectionAirbnb · OAAirbnb interview questions
  • Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
  • Rotting OrangesSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • First and Last Target Position in a Mountain ArrayGoogle · ONSITE INTERVIEWGoogle interview questions
All Motive interview questionsSee Motive hiring insights
Saved
class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
      // write your code here
    }
}
Checking account…