Quick revision
The short answer
Unique visitor IDs go in a HashSet, or a BitSet if the IDs are small integers. A leaderboard ordered by score needs a TreeSet or TreeMap with a comparator that compares by score and then by ID, so that two players with the same score are not treated as duplicates. A recent-items list fits an ArrayDeque, or a LinkedHashMap with removeEldestEntry when it must also be a bounded lookup, and products by SKU in insertion order belong in a LinkedHashMap. None of these are thread-safe, so shared use needs a concurrent alternative.
What to remember
In this answer, we chose a HashSet or BitSet for unique visitor IDs, a TreeSet with a score-then-ID comparator for the leaderboard so ties are not dropped, an ArrayDeque or a LinkedHashMap with removeEldestEntry for recent items, and a LinkedHashMap for products by SKU in insertion order. Each choice came from one deciding property, and we finished with the reminder that none of these collections is thread-safe.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Unique visitor IDs go in a HashSet, or a BitSet if the IDs are small integers. A leaderboard ordered by score needs a TreeSet or TreeMap with a comparator that compares by score and then by ID, so that two players with the same score are not treated as duplicates. A recent-items list fits an ArrayDeque, or a LinkedHashMap with removeEldestEntry when it must also be a bounded lookup, and products by SKU in insertion order belong in a LinkedHashMap. None of these are thread-safe, so shared use needs a concurrent alternative.
Why this question is asked
Choosing a collection is one of the most frequent small decisions we make, and the wrong choice shows up later as slow code, lost data or surprising order. Interviewers like this question because each case has one property that decides the choice, and a good answer names that property instead of reaching for ArrayList and HashMap by habit. In this answer, we will recap the questions that drive the choice, then go through the four cases one by one with code, including the subtle bug in the leaderboard, and finish with what changes when several threads share the collection.
1A Quick Recap: The Questions That Decide
Four questions settle most choices. Do we need uniqueness? Then a Set or the keys of a Map. Do we look things up by a key? Then a Map. Does order matter, and which order: insertion order, sorted order, or none? That separates the Hash, LinkedHash and Tree variants. And which operations must be fast: contains, adding at the ends, removing the oldest, finding a range?
The Hash variants give average constant-time lookups but no order. The LinkedHash variants add a linked list through the entries, so they keep insertion order at a small memory cost. The Tree variants keep elements sorted with O(log n) operations and support range queries.
| Case | Deciding property | Choice |
|---|---|---|
| Unique visitor IDs | Uniqueness, fast contains | HashSet, or BitSet for small ints |
| Leaderboard by score | Sorted order, ties allowed | TreeSet with score-then-ID comparator |
| Recent items, drop oldest | Cheap add and remove at the ends | ArrayDeque, or LinkedHashMap |
| Product by SKU, insertion order | Key lookup plus stable order | LinkedHashMap |
2Unique Visitor IDs: HashSet, or BitSet for Small Integers
We only need to know whether we have seen an ID, and to count unique ones. A HashSet does that with constant-time add and contains, and add tells us whether the element was new.
If the IDs are small, dense integers, a BitSet is far smaller. It stores one bit per possible ID, so a million IDs take about 125 KB, while a HashSet<Integer> of a million entries takes tens of megabytes because every entry is a node object plus a boxed Integer. When IDs are sparse or very large, such as random 64-bit values, the BitSet would be mostly empty and the HashSet is the better choice.
3The Leaderboard: Why the Comparator Needs a Tie-Breaker
A leaderboard must stay sorted by score as scores arrive. The obvious choice is a TreeMap<Integer, String> keyed by score, or a TreeSet with a comparator by score. Both have the same hidden bug.
The reason is that a TreeSet never calls equals. It decides that two elements are the same when the comparator returns 0. With a comparator by score alone, every player with the same score is a duplicate, and the second one is silently dropped. Adding the player ID, or name, as a second key makes the order total, so only truly identical entries compare as equal. When a player's score changes, we must remove the old entry and add a new one, because changing a field of an element inside a TreeSet breaks its order. For a top ten, we iterate the first ten elements or use headSet.
4Recent Items: ArrayDeque or a Bounded LinkedHashMap
A recent-items list grows at one end and loses its oldest element from the other. An ArrayDeque does both in constant time, which an ArrayList cannot, because removing index 0 shifts every element.
If we also need to look items up by key, or move an item to the front when it is viewed again, the LinkedHashMap version is the better fit. With access order turned on, every get moves the entry to the end, and removeEldestEntry drops the least recently used entry after each insertion. That is a complete small LRU cache in a few lines.
5Product by SKU in Insertion Order: LinkedHashMap
We look products up by SKU, so we need a Map, and we want to list them in the order they were added, for example to match a supplier's catalogue. A HashMap gives fast lookup but an order that depends on hash codes and can change as the map grows. A TreeMap would sort by SKU, which is a different order. A LinkedHashMap gives the HashMap's constant-time get and put, and iterates in insertion order. Re-inserting an existing key does not change its position.
One more note applies to all four answers: none of these classes is thread-safe. If several request threads share the collection, we need ConcurrentHashMap.newKeySet() for the visitor IDs, a ConcurrentSkipListSet for the leaderboard, and a ConcurrentLinkedDeque or explicit locking for the recent items. A concurrent LRU cache is harder; a library such as Caffeine is usually the right tool for it.
6Follow-up questions
Why not use a PriorityQueue for the leaderboard?
A PriorityQueue only gives fast access to the single best element. Its iterator does not return elements in sorted order, and removing an arbitrary player is linear. A leaderboard needs the whole order, a top ten and updates, so a TreeSet fits better.
What happens if two players have the same score in a TreeMap keyed by score?
The second put replaces the first player, because the key is equal. We could map each score to a list of players, but a TreeSet of entries with a tie-breaking comparator is usually simpler.
Does LinkedHashMap cost much more memory than HashMap?
Each entry adds two references for the linked list, about 8 bytes with compressed references, on top of the 32 or so bytes of a HashMap node. For most maps that is a small price for a predictable order.
7Conclusion
In this answer, we chose a HashSet or BitSet for unique visitor IDs, a TreeSet with a score-then-ID comparator for the leaderboard so ties are not dropped, an ArrayDeque or a LinkedHashMap with removeEldestEntry for recent items, and a LinkedHashMap for products by SKU in insertion order. Each choice came from one deciding property, and we finished with the reminder that none of these collections is thread-safe.
Open with
I would pick each one from its main property. For unique visitor IDs, I need uniqueness and fast contains, so a HashSet; if the IDs are small dense integers, a BitSet is much smaller, about one bit per possible ID.
Then, if they want more
For the leaderboard, I need sorted order, so a TreeSet or TreeMap, but the comparator has to compare by score and then by player ID. A TreeSet decides duplicates by the comparator returning zero, not by equals, so with score alone, two players with the same score would collapse into one and the second would be silently dropped.
When a score changes, I remove and re-add the entry. For recent items with fast removal of the oldest, an ArrayDeque, because adding at one end and polling from the other are both constant time, unlike removing index zero from an ArrayList.
If I also need lookup by key or a bounded cache, a LinkedHashMap with access order and removeEldestEntry gives me a small LRU. For products by SKU in insertion order, a LinkedHashMap: HashMap speed for lookups, plus iteration in the order products were added.
Finally, none of these is thread-safe, so if request threads share them, I would use ConcurrentHashMap's key set, a ConcurrentSkipListSet, or a library like Caffeine for the cache.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Pick a collection for each case and justify it: unique visitor IDs, a leaderboard...": https://scrivio-interview-library.netlify.app/java/collections-overview/636e75dc1a/