Implement a thread-safe generic LRU cache and reduce lock contention

Quick Overview

Implement a generic Java LRU cache with concurrent get and put operations. Explain global-lock correctness, then compare sharding and deferred recency updates, including their effects on contention and eviction behavior.

Implement a thread-safe generic LRU cache and reduce lock contention

Company: Apple

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Implement a generic Java `LRUCache<K, V>` with `void put(K key, V value)` and `V get(K key)`. The cache has a fixed capacity and evicts the least recently used entry when a new insertion would exceed that capacity. Explain how to make it thread-safe, starting with one global lock, then discuss sharding and deferring linked-list updates on reads. ### Constraints and Clarifications For this exercise, assume a positive capacity, non-null keys and values, and `null` for a missing key. A successful `get` and every `put` make that entry most recently used. Updating an existing key does not increase the size. These API choices are exercise assumptions because the report does not specify them. ### Clarifying Questions - Must every completed access affect one exact global LRU order, or is approximate eviction acceptable? - May the cache return shared mutable values, and who is responsible for synchronizing changes to those values? - Is a miss represented by `null`, an exception, or a separate result type? ### Part 1: Implement the cache with one lock Write the cache operations, including eviction, and make concurrent `get` and `put` calls preserve the map, linked list, and capacity invariants. Explain the relevant Java synchronization mechanics. ```hint A read changes the cache Identify every piece of shared state that a successful lookup touches. ``` #### What This Part Should Cover - Entry lookup and recency representation, with insertion, replacement, and eviction behavior - The atomic scope of each public operation and Java memory visibility - Running time, storage, and a trace that exercises a hit and an eviction ### Part 2: Reduce contention Discuss sharding the cache and delaying linked-list updates during `get`. Explain what each choice does to eviction semantics and concurrency behavior. ```hint Define the promise first Compare the victim chosen by a global ordering with the victim chosen when only part of the cache's access history is available. ``` #### What This Part Should Cover - Per-shard capacity and lock ownership - Deferred access tracking and synchronization with replacement or eviction - Throughput, contention, and exact versus approximate recency ### What a Strong Answer Covers - A complete implementation whose map and list always describe the same entries - A clear distinction between synchronization of cache structure and synchronization of cached values - An explicit statement of which LRU guarantees survive each optimization ### Follow-up Questions - Why is a read lock alone insufficient for a successful `get` that updates recency? - How can a delayed access record avoid promoting an entry that was evicted and reinserted under the same key? - What happens when most requests land on one shard?

Overview: Implement a generic Java LRU cache with concurrent get and put operations. Explain global-lock correctness, then compare sharding and deferred recency updates, including their effects on contention and eviction behavior.

Apple logo
Apple
Nov 29, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Implement a generic Java LRUCache<K, V> with void put(K key, V value) and V get(K key). The cache has a fixed capacity and evicts the least recently used entry when a new insertion would exceed that capacity. Explain how to make it thread-safe, starting with one global lock, then discuss sharding and deferring linked-list updates on reads.

Constraints and Clarifications

For this exercise, assume a positive capacity, non-null keys and values, and null for a missing key. A successful get and every put make that entry most recently used. Updating an existing key does not increase the size. These API choices are exercise assumptions because the report does not specify them.

Clarifying Questions Guidance

  • Must every completed access affect one exact global LRU order, or is approximate eviction acceptable?
  • May the cache return shared mutable values, and who is responsible for synchronizing changes to those values?
  • Is a miss represented by null , an exception, or a separate result type?

Part 1: Implement the cache with one lock

Write the cache operations, including eviction, and make concurrent get and put calls preserve the map, linked list, and capacity invariants. Explain the relevant Java synchronization mechanics.

What This Part Should Cover Guidance

  • Entry lookup and recency representation, with insertion, replacement, and eviction behavior
  • The atomic scope of each public operation and Java memory visibility
  • Running time, storage, and a trace that exercises a hit and an eviction

Part 2: Reduce contention

Discuss sharding the cache and delaying linked-list updates during get. Explain what each choice does to eviction semantics and concurrency behavior.

What This Part Should Cover Guidance

  • Per-shard capacity and lock ownership
  • Deferred access tracking and synchronization with replacement or eviction
  • Throughput, contention, and exact versus approximate recency

What a Strong Answer Covers Guidance

  • A complete implementation whose map and list always describe the same entries
  • A clear distinction between synchronization of cache structure and synchronization of cached values
  • An explicit statement of which LRU guarantees survive each optimization

Follow-up Questions Guidance

  • Why is a read lock alone insufficient for a successful get that updates recency?
  • How can a delayed access record avoid promoting an entry that was evicted and reinserted under the same key?
  • What happens when most requests land on one shard?