FastPrepKth Smallest Value in a Row- and Column-Sorted Matrix
Problems/Kth Smallest Value in a Row- and Column-Sorted Matrix

Kth Smallest Value in a Row- and Column-Sorted Matrix

OpenAI logoOpenAI● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report from June 2026 says the coding part of a technical phone screen asked for the value of a given rank in a matrix whose rows and columns are sorted. The candidate was also asked to weigh a heap against a search over the range of values. That included the case where the matrix is too big for memory.

The report does not give sizes or examples. It does not say whether the rank counts from the smallest value or how repeated values count. Here k counts from the smallest, repeats count separately, and rows and columns are sorted in non-decreasing order. The input here fits in memory. The cost and memory comparison is in the editorial. The examples are our own.

Our version is about 80% the same as the reported question.

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) → int

Examples

Example 1

matrix = [[2,6,9],[3,7,10],[8,12,15]]k = 5return = 8
matrix
012026913710281215

Sorted, 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 = 2
matrix
01230113411236

The 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 = -7
matrix
00-71-724

One 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 length n.
  • 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

OpenAI interview experiences

  • OpenAI OA: What It Tests (2026)
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        // Write your code here.
    }
}
Checking account…