Kth Smallest Value in a Row- and Column-Sorted Matrix
Updated
Problem statement
You are given an integer matrix matrix with m rows and n columns. Each row is sorted from left to right, and each column is sorted from top to bottom, both in non-decreasing order (equal neighbors are allowed).
Imagine writing all m * n values in one list and sorting it from smallest to largest. Return the value at position k of that list, counting from 1. Repeated values count separately: a value that appears three times takes three positions.
Follow-up: can you find the answer while keeping much less than m * n values in memory at once?
Function
kthSmallest(matrix: int[][], k: int) → intExamples
Example 1
matrix = [[2,6,9],[3,7,10],[8,12,15]]k = 5return = 8Sorted, the nine values are 2, 3, 6, 7, 8, 9, 10, 12, 15. The 5th smallest is 8. Note that 8 is in the last row, so reading the matrix row by row does not give the answer.
Example 2
matrix = [[1,1,3,4],[1,2,3,6]]k = 4return = 2The matrix has 2 rows and 4 columns. Sorted with repeats kept, the values are 1, 1, 1, 2, 3, 3, 4, 6. The 4th smallest is 2.
Example 3
matrix = [[-7],[-7],[4]]k = 2return = -7One column. The values are -7, -7 and 4. Repeats count separately, so the 1st and 2nd smallest are both -7.
Constraints
m == matrix.length, and every row has the same lengthn.1 <= m, n <= 100-10^9 <= matrix[i][j] <= 10^9- Every row and every column is sorted in non-decreasing order.
1 <= k <= m * n
More OpenAI Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Swim in Rising WaterUiPath · PHONE SCREEN
- Find the Safest Path in a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- K-th Smallest Value in a Sorted MatrixPhonePe · ONSITE INTERVIEWPhonePe interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Rat's Safest PathGoogle · PHONE SCREENGoogle interview questions
- Shortest Path with Directional Move CostsThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Search a 2D Matrix IIBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Split Tender Part 4 - RefundsStripe · OA · PHONE SCREENStripe interview questions