PayPal · Question 73 · Trees
Kth Smallest Element in a Sorted Matrix
Problem
Find the kth smallest element in a sorted matrix (rows and columns sorted).
Approach
Use min-heap or binary search. (Min-Heap): push row heads, advance winners.
Time O(klogn)Space O(n)
How it works
- 1Seed the heap with every row head.
- 2Pop k times, advancing each winner rightward.
Sample input / output
Input: matrix = [[1, 5, 9], [10, 11, 13], [12, 13, 15]], k = 8 Output: 13
Constraints
n == matrix.length; 1 <= n <= 300; rows and columns sorted.
Solution — read it, then walk it
Time O(klogn)Space O(n)Loops 2Branches 1Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/16
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits