Kth Smallest Latency in a Row and Column Sorted Grid

Last updated: November 29, 2026

Quick Overview

An n by n grid of p99 latencies is sorted ascending along every row and every column. Return the kth smallest value in the grid.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


An n by n grid of p99 latencies is sorted ascending along every row and every column. Return the kth smallest value in the grid.

This appears in Meta onsites as a heap-or-binary-search choice point. A heap of row heads works, but strong candidates offer the value-space binary search with an O(n) staircase count and compare the two.

What the Interviewer Expects
  • Offer a heap-based k-way merge in O(k log n)
  • Offer binary search on the value range with a staircase count
  • Prove the binary search lands on a value present in the grid
  • Compare complexities and when each wins
  • Handle duplicates and negatives
Key Topics to Cover
binary search on answer
sorted matrix
min-heap
staircase count
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if the grid is n by m rather than square?
  • What if values are floats?
  • Find the median of the grid.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Square grid, rows and columns non-decreasing.
  • 1 <= k <= n*n.
  • Values are integers (clarify; floats change the termination rule).
  • Return the value, not its position....
High-Level Design

Two good approaches:

  1. Heap: push the first element of each row, pop k times, pushing the next element in that row. O(k log n), fine when k is small.
  2. Binary search on value: for a candida...

Submit Your Answer
Markdown supported