Answer: Amazon OA Question 2024 May | Heap & Quickselect | on-campus

Answer · Posted Jun 2026

Solution : Kth Largest Element — Min-Heap Approach While sorting the array takes O(N log N) time, we can optimize this using a Min-Heap (implemented via PriorityQueue in Java) to find the answer in O(N log k) time and O(k) auxiliary space. We traverse the array and add elements to the min-heap. If the size of the heap exceeds k, we remove the smallest element from the heap (using poll()). This ensures that the heap only ever contains the k ...

The full answer & interview discussion are available to premium members.

Log in Create a free account