Quick Overview

This question evaluates algorithmic problem-solving and data-structure knowledge, focusing on selection in a row-and-column-sorted matrix and the comparative use of min-heaps versus value-space binary search along with time/space complexity and edge-case analysis.

Find kth smallest in sorted matrix

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an n x n matrix where each row and each column is sorted in nondecreasing order, and an integer k (1 ≤ k ≤ n^ 2), return the k-th smallest element in the matrix. Compare approaches using a min-heap over row heads versus value-space binary search with counting, and analyze complexities and edge cases.

Overview: This question evaluates algorithmic problem-solving and data-structure knowledge, focusing on selection in a row-and-column-sorted matrix and the comparative use of min-heaps versus value-space binary search along with time/space complexity and edge-case analysis.

Given an n x n matrix where each row and each column is sorted in non-decreasing order, and an integer k (1 ≤ k ≤ n^2), return the k-th smallest element in the matrix. Note that it is the k-th smallest element in the sorted order, not the k-th distinct element. Duplicate values count toward k. A min-heap over the row heads gives an O(k log n) solution: seed the heap with the first element of every row, then pop k times, each time pushing the next element from the same row. An alternative value-space binary search with a counting routine runs in O(n log(max-min)) and counts, for a candidate value x, how many matrix entries are ≤ x by walking from the bottom-left corner. Example: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8 → 13 (sorted order: 1,5,9,10,11,12,13,13,15; the 8th element is 13).

Constraints

  • n == matrix.length == matrix[i].length
  • 1 <= n <= 300
  • Each row and each column of matrix is sorted in non-decreasing order
  • 1 <= k <= n^2
  • -10^9 <= matrix[i][j] <= 10^9

Examples

Input: ([[1,5,9],[10,11,13],[12,13,15]], 8)

Expected Output: 13

Explanation: Sorted order: 1,5,9,10,11,12,13,13,15. The 8th smallest is 13 (the second 13).

Input: ([[1,2],[1,3]], 2)

Expected Output: 1

Explanation: Sorted order: 1,1,2,3. Duplicates count, so the 2nd smallest is the second 1.

Hints

  1. The whole matrix isn't globally sorted, but each row IS sorted. Think of merging n sorted lists (the rows) and stopping after k elements.
  2. Use a min-heap seeded with the head of each row: (value, rowIndex, colIndex). Pop the smallest, then push the next element from that same row.
  3. After k pops, the last popped value is the answer. Each value pushed/popped costs O(log n).
  4. Alternative: binary search on the value range [matrix[0][0], matrix[n-1][n-1]]. For a candidate x, count how many entries are <= x by walking from the bottom-left corner in O(n); shrink the range until you isolate the k-th smallest.

Kth Smallest Element in a Sorted Matrix

mediumMetaMachine Learning Engineer

Given an n x n matrix where each row and each column is sorted in non-decreasing order, and an integer k (1 ≤ k ≤ n^2), return the k-th smallest element in the matrix.

Note that it is the k-th smallest element in the sorted order, not the k-th distinct element. Duplicate values count toward k.

A min-heap over the row heads gives an O(k log n) solution: seed the heap with the first element of every row, then pop k times, each time pushing the next element from the same row. An alternative value-space binary search with a counting routine runs in O(n log(max-min)) and counts, for a candidate value x, how many matrix entries are ≤ x by walking from the bottom-left corner.

Example: matrix = [[1,5,9],[10,11,13],[12,13,15]], k = 8 → 13 (sorted order: 1,5,9,10,11,12,13,13,15; the 8th element is 13).

View full question (all parts)

Given an n x n matrix where each row and each column is sorted in nondecreasing order, and an integer k (1 ≤ k ≤ n^2), return the k-th smallest element in the matrix. Compare two approaches and analyze their time, auxiliary space, and boundary cases.

Interface and Constraints

Implement Solution.kthSmallest(matrix, k) (C++: int kthSmallest(vector<vector<int>>& matrix, int k)). The existing Python practice name is kth_smallest(matrix, k).

  • n == len(matrix) == len(matrix[i]) and 1 <= n <= 300.
  • Every entry is an integer in [-1000000000, 1000000000].
  • Each row and each column is sorted in nondecreasing order.
  • 1 <= k <= n * n.
  • Count each occurrence, including duplicates. Return the value at rank k, not the k-th distinct value.
  • Use auxiliary memory asymptotically smaller than O(n^2), excluding the supplied matrix.

Examples

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

In the first example the sorted occurrences are [1,5,9,10,11,12,13,13,15]; the two copies of 13 occupy ranks 7 and 8.

Clarifying Questions

  • Are duplicate values removed before ranking? No; every matrix entry contributes one occurrence.
  • Is the matrix rectangular? This version is square, with the same size for every row and column.
  • Must sortedness be verified? No; it is guaranteed by the input contract.

What a Strong Answer Covers

  • Returns the correct occurrence rank, including duplicate and negative values.
  • Explains why its decisions preserve the desired rank and establishes the memory bound.
  • Accounts for all setup and selection costs, including single-entry and all-equal matrices.
  • Distinguishes complexity under the stated bounded integer domain from complexity with an unbounded value range.

Follow-up Questions

  • Can you achieve O(1) auxiliary memory?
  • Can you achieve time linear in the side length n under the stated integer bounds?
  • As an advanced optional direction, what additional argument would be needed for linear selection independent of the numeric value range?

Examples

Example 1
Input
([[1,5,9],[10,11,13],[12,13,15]], 8)
Output
13
Notes
Sorted order: 1,5,9,10,11,12,13,13,15. The 8th smallest is 13 (the second 13).
Example 2
Input
([[1,2],[1,3]], 2)
Output
1
Notes
Sorted order: 1,1,2,3. Duplicates count, so the 2nd smallest is the second 1.

Constraints

  • •n == matrix.length == matrix[i].length
  • •1 <= n <= 300
  • •Each row and each column of matrix is sorted in non-decreasing order
  • •1 <= k <= n^2
  • •-10^9 <= matrix[i][j] <= 10^9

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Sample call
kth_smallest([[1,5,9],[10,11,13],[12,13,15]], 8)
Language
Case 1
Input
([[1,5,9],[10,11,13],[12,13,15]], 8)
Expected
13
Case 2
Input
([[1,2],[1,3]], 2)
Expected
1

Browse More Questions