Find kth largest element in matrix

Last updated: June 9, 2026

Quick Overview

Given an n x n matrix where each row and column is sorted in ascending order, write a function to find the kth largest element in the matrix. The function should take the matrix and an integer k as inputs and return the kth largest element as output. The solution should be efficient, ideally with a time complexity better than O(n^2).

Airbnb
Coding & Algorithms
Machine Learning Engineer
Airbnb
June 9, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Hard

46

11

2,649 solved


Given an n x n matrix where each row and column is sorted in ascending order, write a function to find the kth largest element in the matrix. The function should take the matrix and an integer k as inputs and return the kth largest element as output. The solution should be efficient, ideally with a time complexity better than O(n^2).

Coding interviews at Airbnb focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Quickly identify the optimal approach and its theoretical basis
  • Handle complex algorithm design with multiple interacting components
  • Write concise, elegant code under time pressure
  • Prove correctness of your approach and discuss alternative solutions
  • Optimize beyond the obvious: discuss constant factor improvements
  • Address follow-up variations and explain how the solution generalizes
Key Topics to Cover
Dynamic programming and memoization
Sorting and searching
Edge cases and input validation
Graph algorithms and traversal
Binary search and divide and conquer
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
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
  • How would you parallelize this solution?
  • Can you solve this in a single pass?
  • What if the input doesn't fit in memory?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

To find the kth largest element in a sorted n x n matrix, we can utilize the properties of the matrix: each row and each column is sorted in ascending order. This means the largest elements will be lo...

Approach
  1. Binary Search Setup: Start with the smallest possible value (the top-left corner of the matrix) and the largest possible value (the bottom-right corner).
  2. Binary Search Execution: While ...

Submit Your Answer
Markdown supported

Related Questions