Top Three Scores with PriorityQueue
A training center records many quiz scores during the week, but the instructor only wants to display the top three scores on a notice board. The full list may be longer than needed, and the board should end up with just the highest three values.
Your task is to return a list containing the three highest scores in descending order. If there are fewer than three scores, return all of them, still in descending order.
For example, if the input is [70, 88, 91, 65, 95, 84], the result should be [95, 91, 88]. If the input is [40, 50], the result should be [50, 40].
This problem is a good medium-level Collections Framework exercise because it is not just about storing values. You need a collection that helps you keep the best few items while scanning the input.
A PriorityQueue is useful here because it can hold the current top candidates and let you remove the smallest one whenever the group grows beyond three.
Example Input & Output
If there are fewer than three values, return all of them in descending order.
Equal high scores can both stay in the result.
Only the three highest scores remain, in descending order.
Algorithm Flow

Solution Approach
This problem becomes much cleaner once you realize that you never need to keep the whole list sorted at all times. You only care about the best three scores seen so far. That is exactly the kind of situation where a PriorityQueue helps.
In Java, the default PriorityQueue acts like a min-heap. That means the smallest value in the queue is easy to inspect and remove. So the strategy is to keep a min-heap whose size never grows beyond three.
As you scan each score, add it to the queue. If the queue size becomes four, remove one item. Because it is a min-heap, the removed item will be the smallest among the four, leaving only the three highest scores seen so far.
The heart of the algorithm looks like this:
After that loop, the queue contains the answer values, but not yet in the final descending format. Since polling from a min-heap gives the smallest remaining value first, you can build a list from the queue and then reverse it, or insert values at the front as you remove them.
This approach is more efficient than sorting the entire list when the requirement is only to keep the top few values. The full runtime is O(n log 3), which is effectively O(n) here because the heap never gets large. The important lesson is choosing a collection based on the exact job you need it to do.
Best Answers
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
class Solution {
public static List<Integer> topThreeScores(List<Integer> scores) {
PriorityQueue<Integer> heap = new PriorityQueue<>();
for (int score : scores) {
heap.offer(score);
if (heap.size() > 3) {
heap.poll();
}
}
List<Integer> result = new ArrayList<>(heap);
result.sort(Collections.reverseOrder());
return result;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
