Deloitte · Question 54 · Heaps
Kth Largest Element in an Array
Problem
Use a min-heap of size k or Quickselect to find the kth largest (heap solution below).
Approach
Keep only the k largest in a min-heap.
Time O(nlogk)Space O(k)
How it works
- 1Push every number, evicting past size k.
- 2The heap top is the kth largest.
Sample input / output
Input: nums = [3, 2, 1, 5, 6, 4], k = 2 Output: 5
Constraints
1 <= k <= nums.length <= 10^5.
Solution — read it, then walk it
Time O(nlogk)Space O(k)Loops 1Branches 1Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/11
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