Courseiva
Question 176 of 513
Working with Arrays and CollectionsmediumMultiple ChoiceObjective-mapped

1Z0-829 Working with Arrays and Collections Practice Question

A web server logs user sessions. Each session has a unique session ID (String) and a last access time (long). The system needs to evict sessions that have been inactive for more than 30 minutes. The current implementation uses a HashMap<String, Long> to store session IDs and last access times. A scheduled task iterates over all entries and removes those where currentTime - lastAccess > 30 minutes. However, this iteration is becoming slow as the number of sessions grows (millions). The developer wants to improve the eviction performance without affecting the O(1) put and get operations. Which approach should be taken?

⚠ Common exam trap

A common mix-up: candidates think a sorted structure like TreeMap or PriorityQueue is needed for time-based eviction, but they overlook the O(1) access-time requirement and the fact that LinkedHashMap's access-order mode provides automatic LRU eviction without explicit iteration.

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 LinkedHashMap<String, Long> with access-order=true and override removeEldestEntry() to evict entries older than 30 minutes.

LinkedHashMap with access-order=true and a custom removeEldestEntry() method provides O(1) amortized put/get operations while automatically evicting the least-recently accessed entry when a condition is met. By overriding removeEldestEntry() to check if the eldest entry's last access time is older than 30 minutes, the eviction happens during put operations without requiring a separate iteration over all entries, thus solving the performance issue.

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 PriorityQueue<Session> ordered by last access time and update the time when accessed.

    Why it's wrong here

    PriorityQueue does not support efficient lookup by session ID.

  • Use ConcurrentHashMap<String, Long> and use parallel streams to remove expired entries.

    Why it's wrong here

    Still requires iteration over all entries.

  • Use LinkedHashMap<String, Long> with access-order=true and override removeEldestEntry() to evict entries older than 30 minutes.

    Why this is correct

    Efficiently maintains access order and automatically evicts eldest.

  • Use TreeMap<String, Long> sorted by last access time and poll the first entry if too old.

    Why it's wrong here

    TreeMap does not provide O(1) get/put; sorting by time is not straightforward.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 11, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.