FastPrepK-th Smallest Value in a Sorted Matrix

K-th Smallest Value in a Sorted Matrix

PhonePe logoPhonePe● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given a non-empty rectangular integer matrix whose rows and columns are each sorted in nondecreasing order, return the k-th smallest matrix value when all values are counted with multiplicity.

Function

kthSmallest(matrix: int[][], k: int) → int

Examples

Example 1

matrix = [[1,5,9],[10,11,13],[12,13,15]]k = 8return = 13

The sorted values place 13 in position 8.

Example 2

matrix = [[-5]]k = 1return = -5

The only value is the first smallest.

Constraints

  • 1 <= matrix.length, matrix[i].length <= 300
  • Every row has the same positive length.
  • Rows and columns are sorted in nondecreasing order.
  • 1 <= k <= rows * columns
  • Matrix values are between -10^9 and 10^9.

Source note: Interview prompt excerpt preserved as source evidence.

More PhonePe problems

See PhonePe hiring insights
public int kthSmallest(int[][] matrix, int k) {
    // Write your code here.
}
matrix[[1,5,9],[10,11,13],[12,13,15]]
k8
expected13
Checking account…