Staircase Search In A Sorted Matrix
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The 7 in the top-right corner of that example matrix is the whole problem. Goldman Sachs reportedly used this staircase search in a September 2026 OA, and the hinted pattern says breadth-first search, but don't buy it. It's a sorted-matrix walk from a corner, and each comparison kills a full row or column. Rows and columns are both sorted, but adjacent rows don't form one global order, so a flat binary search fails. If you know the corner trick, this is ten lines. If you blank, StealthCoder is a safety net running invisibly during the live OA.
The problem
You are given a nonempty rectangular integer matrix matrix and an integer target. Every row is sorted in nondecreasing order from left to right. Every column is sorted in nondecreasing order from top to bottom. Adjacent rows are not required to form one globally sorted list. Return true if target appears in matrix, and false otherwise. What the interview report shared The Superday report asked a staircase problem to search. It did not restate the matrix contract. This exercise uses conventional 2D Young-tableau staircase search from a corner. Function staircaseSearch(matrix: int[][], target: int) → boolean Examples Example 1 matrix = [[1,4,7],[2,5,8],[3,6,9]] target = 5 return = true Starting at the top-right value 7, move left because 5 is smaller, then move down because 4 is smaller than 5. The next cell is 5. Example 2 matrix = [[1,4,7],[2,5,8],[3,6,9]] target = 10 return = false The same walk from 7 reaches the bottom-right value 9 without seeing 10. Constraints 1 <= matrix.length, matrix[i].length <= 300. matrix is rectangular: every row has the same length. -10^9 <= matrix[i][j], target <= 10^9. Each row is sorted nondecreasing left to right. Each column is sorted nondecreasing top to bottom.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Start at the top-right cell. If it equals target, return true. If it's bigger than target, everything below it in that column is even bigger, so move left. If it's smaller, everything to its left in that row is smaller, so move down. Each step discards a row or a column, giving O(m+n) time and O(1) space. With 300 by 300 limits, that's at most 600 steps. Pitfalls: starting at top-left or bottom-right, where both directions increase and you can't decide. Treating the matrix as one flat sorted array and running binary search on it. Mixing up row and column bounds on a non-square grid. Loop while row < m and col >= 0. BFS or a visited set works but is wasted effort. If the corner logic slips under pressure, StealthCoder can hand you the clean version live.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Staircase Search In A 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as search a 2d matrix ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Staircase Search In A Sorted Matrix FAQ
What's the trick for Staircase Search In A Sorted Matrix?+
Start at the top-right corner. If the value is larger than target, move left. If smaller, move down. Each move eliminates an entire row or column, so you finish in O(m+n). It works because the corner is the one cell where the two directions split into larger and smaller.
Why not use binary search on the whole matrix?+
Adjacent rows aren't globally sorted. The last value of one row can exceed the first value of the next, so flattening the matrix into one sorted list breaks. You could binary search each row for O(m log n), but the staircase walk is faster and simpler.
Can I start from the top-left or bottom-right?+
No. At those corners, both moving right and moving down increase the value, so a comparison with target doesn't tell you which direction to go. You need the top-right or bottom-left corner, where one direction increases and the other decreases.
Is the BFS hint worth following?+
Not really. BFS or DFS over the grid would work but ignores the sorted structure and costs O(m*n). That's 90,000 cells at the limit, which passes, but the staircase walk is the intended answer and the one an interviewer expects.
How do I prep for this in 48 hours?+
Write the staircase search from memory twice, once from top-right and once from bottom-left. Test on a one-row matrix, a one-column matrix, a single cell, and a target smaller or larger than everything. Check the loop bounds carefully. That covers nearly every bug people hit.