1Z0-829 Working with Arrays and Collections Practice Question
A financial application processes transactions in batches. Each transaction is represented as a Transaction object with fields: long id, BigDecimal amount, LocalDateTime timestamp. Transactions are stored in a List<Transaction> in the order they arrive. The system needs to frequently check if a transaction with a specific id exists, and also needs to iterate through transactions in chronological order. The list currently contains millions of transactions, and the existence check is becoming a performance bottleneck because it currently uses a linear search. The system must also maintain insertion order for iteration. Which approach best improves the performance of the existence check while maintaining the required iteration order?
⚠ Common exam trap
Candidates often confuse TreeMap's natural ordering with insertion order, or assume that sorting a List and using binary search is the best optimization, overlooking the O(1) lookup advantage of hash-based structures like LinkedHashMap.
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
✓
Replace List<Transaction> with LinkedHashMap<Long, Transaction> mapping id to transaction. Iterate using values() which preserves insertion order.
LinkedHashMap maintains insertion order for iteration via its values() view, while providing O(1) key-based lookup for the existence check. This directly addresses the performance bottleneck of linear search in a List without sacrificing the required chronological iteration order.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Keep List<Transaction> and add a separate HashSet<Long> of ids for fast lookup. Use the list for iteration.
Why it's wrong here
While adding a separate HashSet<Long> provides O(1) lookup and the List maintains insertion order, this approach requires manual synchronization between the two collections (adding/removing from both) and uses extra memory. Option B achieves the same goals with a single data structure, making it cleaner and more efficient.
- ✓
Replace List<Transaction> with LinkedHashMap<Long, Transaction> mapping id to transaction. Iterate using values() which preserves insertion order.
Why this is correct
LinkedHashMap<Long, Transaction> provides O(1) key-based lookup for existence checks and its values() iterator returns entries in insertion order, exactly matching the requirement. This is the best combination of performance and simplicity.
- ✗
Keep List<Transaction> and sort it by id. Use Collections.binarySearch() for lookup.
Why it's wrong here
Sorting the List and using binary search gives O(log n) lookup, which is slower than O(1) from a hash-based structure. Additionally, sorting would lose the original chronological order unless you maintain a separate copy or index, adding complexity.
- ✗
Replace List<Transaction> with TreeMap<Long, Transaction> mapping id to transaction. Iterate using values() which returns in natural key order.
Why it's wrong here
TreeMap<Long, Transaction> provides O(log n) lookup and its values() iterator returns entries in natural key order (sorted by id), not insertion order. This fails the requirement to maintain chronological (insertion) order.
Go deeper
Related to this question
About these practice questions
This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. 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.