Search a Row-Major Sorted Matrix
Updated
Problem statement
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) → booleanExamples
Example 1
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]]target = 3return = trueThe 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 = falseBinary search ends between 11 and 16, so 13 is absent.
Example 3
matrix = []target = 1return = falseAn empty matrix contains no target.
Constraints
0 <= matrix.length <= 100.- When
matrixis non-empty,1 <= matrix[i].length <= 100and 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