Locate a Stock Count in a Row and Column Sorted Inventory Grid
Last updated: November 29, 2026
Quick Overview
An inventory grid has stock counts sorted ascending along each row and down each column. Return whether a target count appears in the grid, in O(m + n) time.
Amazon
November 29, 20260
0
0 solved
An inventory grid has stock counts sorted ascending along each row and down each column. Return whether a target count appears in the grid, in O(m + n) time.
A classic Amazon screen question reframed around inventory dashboards. The interviewer checks whether you find the corner that gives a decision at every step, rather than binary searching each row.
What the Interviewer Expects
- Start at the top-right (or bottom-left) corner
- Move left if the value is too big, down if too small
- Explain why each move safely discards a row or column
- State O(m + n) time and O(1) space
- Mention the O(m log n) row-by-row binary search as a baseline
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you count how many cells are below the target?
- What if the grid is fully sorted row-major instead?
- How would you find the k-th smallest value in such a grid?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: an m x n matrix with rows sorted left to right and columns sorted top to bottom, plus an integer target. Output: True if the target exists. Duplicates are allowed. The matrix may be empty....
High-Level Design
Stand at the top-right corner. Every cell to its left is smaller and every cell below is larger. If the value equals the target, return True. If it is greater, the whole column below is even greater, ...