Reported August 2025
Motivebinary search

Search a Row-Major Sorted Matrix

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

Motive reportedly put a matrix search question in front of candidates in August 2025, and the statement tells you the answer before you start: use binary search with logarithmic time over the total number of cells. Each row is strictly increasing, and every row starts above the last value of the row before it. So the whole grid is one sorted list wearing a disguise. If you've got an OA coming, this is a pattern recognition question, not a hard one. The hint says breadth-first search, but ignore it. StealthCoder sits invisibly on your screen as a safety net if you blank on the index math mid-assessment.

The problem

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 = 3
return = true
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 = 13
return = false
Binary search ends between 11 and 16, so 13 is absent.
Example 3
matrix = []
target = 1
return = 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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Treat the matrix as a flattened sorted array of length rows times cols. Binary search lo = 0, hi = rows*cols - 1. For each mid, row = mid / cols, col = mid % cols, then compare matrix[row][col] to target and move the bounds. That's O(log(m*n)) time and O(1) space. The pitfalls are small but they bite. An empty matrix like Example 3 has no columns, so check matrix.length == 0 before reading matrix[0].length. Mixing up the divisor (use cols, not rows) gives wrong cells on non-square grids. Use lo + (hi - lo) / 2 if your language overflows, though these bounds are small. Don't scan every row, and don't run BFS. If the index conversion slips away under pressure, StealthCoder can surface the clean solution in the live OA so you can verify your own version against it.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Search a Row-Major Sorted Matrix cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as search a 2d matrix. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Search a Row-Major Sorted Matrix FAQ

How hard is the Motive matrix search question really?+

Easy to medium. The statement basically hands you the approach. The only real work is converting a flat index into row and column correctly and handling the empty matrix case. If you've seen binary search once, you can finish this quickly.

What's the trick to solving it?+

Pretend the matrix is one sorted array. Search indices 0 to rows*cols - 1, and for each mid use mid / cols for the row and mid % cols for the column. The global ordering guarantee is what makes this valid.

Do I need BFS or DFS here?+

No. Traversal would work but gives O(m*n) time, which breaks the logarithmic requirement stated in the problem. Binary search over the flattened index is the intended solution.

What edge cases should I test?+

Test an empty matrix, a single cell, a single row, a single column, and a target smaller than the first value or larger than the last. Also check a target that falls between two existing values, like 13 in Example 2.

How do I prepare in 48 hours for this kind of OA?+

Write the flattened binary search from memory twice, then do the two-step variant where you binary search rows first and then columns. Practice narrating your bounds out loud. Spend the rest of your time on other binary search boundary problems.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it