1Z0-829 Working with Arrays and Collections Practice Question
An application needs to maintain a set of unique customer IDs (type String) and frequently check if an ID is already present. The set is expected to contain up to 100,000 IDs. The current implementation uses a TreeSet, but performance tests show that the contains() operation is slower than desired. The developer considers switching to a HashSet. However, the business requires that when iterating the set, IDs must appear in sorted order. The developer proposes to convert the HashSet to a sorted list each time iteration is needed. Iteration occurs rarely (once per hour). What is the best approach?
⚠ Common exam trap
Test-takers frequently assume sorted iteration must be maintained at all times, overlooking the fact that the requirement is only for rare iteration, making the conversion cost acceptable in exchange for faster contains().
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Use HashSet and sort when iteration is needed
The primary performance bottleneck is the contains() operation, which is O(log n) for TreeSet but O(1) average for HashSet. Since iteration with sorted order is required only once per hour, the cost of sorting the HashSet into a list (O(n log n)) is negligible compared to the frequent contains() checks. This trade-off optimizes for the dominant use case while still meeting the sorted iteration requirement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use ConcurrentSkipListSet
Why it's wrong here
Overkill for single-threaded; similar performance to TreeSet.
- ✗
Use LinkedHashSet
Why it's wrong here
Preserves insertion order, not sorted order.
- ✗
Keep the TreeSet because it maintains sorted order
Why it's wrong here
contains() is O(log n); slower than HashSet.
- ✓
Use HashSet and sort when iteration is needed
Why this is correct
Fast contains() and sorting once per hour is fine.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.