Implement an LRU Cache, Then Approach LFU Eviction and the CAP Theorem

Quick Overview

Implement a fixed-capacity least-recently-used cache with constant-time get and put, then give another solution, outline a least-frequently-used variant with a tie rule, and explain the CAP theorem for a replicated cache. It tests data structure design, complexity reasoning and distributed systems basics.

Implement an LRU Cache, Then Approach LFU Eviction and the CAP Theorem

Company: Apple

Role: Machine Learning Engineer

Category: Software Engineering Fundamentals

Difficulty: hard

Interview Round: Onsite

Implement a least-recently-used (LRU) cache with a fixed capacity, then answer the interviewer's follow-ups: whether there is another solution, how you would approach least-frequently-used (LFU) eviction, and a theorem question. ### Clarifying Questions - Should `get` on a missing key return -1, return `None`, or raise an error? - Does updating an existing key with `put` count as a use of that key? - Can the capacity be zero? - Will several threads use the cache at once? ### Part 1: LRU cache with constant-time operations Implement the class below. `get(key)` returns the value stored for `key`, or -1 if the key is absent, and makes the key the most recently used. `put(key, value)` inserts the key or updates its value and makes it the most recently used; if the cache then holds more than `capacity` keys, it removes the least recently used key. Both operations should take O(1) average time. ```python class LRUCache: def __init__(self, capacity: int): ... def get(self, key: int) -> int: ... def put(self, key: int, value: int) -> None: ... ``` For example, with capacity 2: `put(1, 1)`, `put(2, 2)`, `get(1)` returns 1, `put(3, 3)` evicts key 2, `get(2)` returns -1, `put(4, 4)` evicts key 1, `get(1)` returns -1, `get(3)` returns 3 and `get(4)` returns 4. ```hint Two jobs One structure gives fast lookup by key and another gives fast reordering. Decide what each operation must find and what it must move, then combine them. ``` #### What This Part Should Cover - Structures that make lookup, marking a key as used, and eviction all O(1), and why a single structure is not enough - Correct handling of updates to existing keys and of eviction at capacity - Working code traced against the example sequence ### Part 2: Another solution Is there another way to implement the same cache? Describe at least one alternative and compare it with your Part 1 implementation. ```hint What the library already tracks Some dictionaries remember an order and can move a key to one end in constant time. Ask which of your Part 1 pieces that would replace. ``` #### What This Part Should Cover - A correct implementation or precise description of the alternative - Time and space complexity of each option, including simpler but slower ones - When each option is the better choice ### Part 3: An approach for LFU How would you approach an LFU cache, which evicts the key with the fewest accesses instead of the key used longest ago? Explain how to keep `get` and `put` efficient. ```hint What eviction must find At eviction time you need the smallest access count and, among keys with that count, one particular key. Think about what changes when one key's count goes up by one. ``` #### Clarifying Questions for this Part - When several keys share the lowest count, which one is evicted? The least recently used among them is the common rule. - Does a `put` that updates an existing key count as an access? #### What This Part Should Cover - A design that keeps both operations O(1), or a justified O(log n) alternative - The tie-breaking rule, and how the minimum count is maintained, including after a new key is inserted - How LFU behaves differently from LRU, for better and for worse ### Part 4: The CAP theorem The report records the final question as the "CAS theorem". No standard theorem has that name, so this part assumes the CAP theorem was meant. State the theorem, and explain what it implies if a cache like this one were replicated across several machines. ```hint When the network splits Picture two replicas that cannot reach each other while a client writes to one of them, and decide what a read from the other should return. ``` #### Clarifying Questions for this Part - Did the interviewer mean the CAP theorem about distributed data stores, or compare-and-swap (CAS), the atomic instruction used for lock-free updates? #### What This Part Should Cover - A precise statement: what consistency, availability and partition tolerance mean, and when the trade-off actually arises - How a replicated cache would behave if it favored consistency versus availability - Common misreadings of the theorem ### What a Strong Answer Covers - Runnable LRU code with O(1) operations, checked against an example - Accurate complexity for every alternative discussed - An LFU design with a defined tie rule and correct bookkeeping of the minimum count - A correct CAP statement tied to concrete cache behavior - Awareness of production concerns such as concurrency and memory overhead per entry ### Follow-up Questions - Many threads share the cache in one process. How would you make `get` and `put` safe, and where could compare-and-swap help or fall short? - With LFU, keys that were popular long ago can stay forever. How would you make counts decay? - How would you bound the cache by total bytes instead of number of keys?

Overview: Implement a fixed-capacity least-recently-used cache with constant-time get and put, then give another solution, outline a least-frequently-used variant with a tie rule, and explain the CAP theorem for a replicated cache. It tests data structure design, complexity reasoning and distributed systems basics.

Read the full Apple Machine Learning Engineer interview experience this question came from

Apple logo
Apple
Aug 17, 2026
hardMachine Learning EngineerOnsiteSoftware Engineering Fundamentals
1
0

Implement a least-recently-used (LRU) cache with a fixed capacity, then answer the interviewer's follow-ups: whether there is another solution, how you would approach least-frequently-used (LFU) eviction, and a theorem question.

Clarifying Questions Guidance

  • Should get on a missing key return -1, return None , or raise an error?
  • Does updating an existing key with put count as a use of that key?
  • Can the capacity be zero?
  • Will several threads use the cache at once?

Part 1: LRU cache with constant-time operations

Implement the class below. get(key) returns the value stored for key, or -1 if the key is absent, and makes the key the most recently used. put(key, value) inserts the key or updates its value and makes it the most recently used; if the cache then holds more than capacity keys, it removes the least recently used key. Both operations should take O(1) average time.

class LRUCache:
    def __init__(self, capacity: int): ...
    def get(self, key: int) -> int: ...
    def put(self, key: int, value: int) -> None: ...

For example, with capacity 2: put(1, 1), put(2, 2), get(1) returns 1, put(3, 3) evicts key 2, get(2) returns -1, put(4, 4) evicts key 1, get(1) returns -1, get(3) returns 3 and get(4) returns 4.

What This Part Should Cover Guidance

  • Structures that make lookup, marking a key as used, and eviction all O(1), and why a single structure is not enough
  • Correct handling of updates to existing keys and of eviction at capacity
  • Working code traced against the example sequence

Part 2: Another solution

Is there another way to implement the same cache? Describe at least one alternative and compare it with your Part 1 implementation.

What This Part Should Cover Guidance

  • A correct implementation or precise description of the alternative
  • Time and space complexity of each option, including simpler but slower ones
  • When each option is the better choice

Part 3: An approach for LFU

How would you approach an LFU cache, which evicts the key with the fewest accesses instead of the key used longest ago? Explain how to keep get and put efficient.

Clarifying Questions for this Part Guidance

  • When several keys share the lowest count, which one is evicted? The least recently used among them is the common rule.
  • Does a put that updates an existing key count as an access?

What This Part Should Cover Guidance

  • A design that keeps both operations O(1), or a justified O(log n) alternative
  • The tie-breaking rule, and how the minimum count is maintained, including after a new key is inserted
  • How LFU behaves differently from LRU, for better and for worse

Part 4: The CAP theorem

The report records the final question as the "CAS theorem". No standard theorem has that name, so this part assumes the CAP theorem was meant. State the theorem, and explain what it implies if a cache like this one were replicated across several machines.

Clarifying Questions for this Part Guidance

  • Did the interviewer mean the CAP theorem about distributed data stores, or compare-and-swap (CAS), the atomic instruction used for lock-free updates?

What This Part Should Cover Guidance

  • A precise statement: what consistency, availability and partition tolerance mean, and when the trade-off actually arises
  • How a replicated cache would behave if it favored consistency versus availability
  • Common misreadings of the theorem

What a Strong Answer Covers Guidance

  • Runnable LRU code with O(1) operations, checked against an example
  • Accurate complexity for every alternative discussed
  • An LFU design with a defined tie rule and correct bookkeeping of the minimum count
  • A correct CAP statement tied to concrete cache behavior
  • Awareness of production concerns such as concurrency and memory overhead per entry

Follow-up Questions Guidance

  • Many threads share the cache in one process. How would you make get and put safe, and where could compare-and-swap help or fall short?
  • With LFU, keys that were popular long ago can stay forever. How would you make counts decay?
  • How would you bound the cache by total bytes instead of number of keys?