Design an encrypted LRU cache for an on-device AI feature
Company: Apple
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a local cache for an on-device AI feature that stores processed text snippets. The cache must encrypt stored values at rest using a repeating XOR cipher with a provided encryption key, to keep cached data private.
The cache has a fixed maximum capacity. When the cache is full and a new entry must be inserted, the least recently used (LRU) entry is evicted. Accessing (`get`) or updating (`put`) a key makes it the most recently used entry.
When retrieving a snippet, the cache must decrypt it before returning the plaintext. If a key does not exist in the cache, return an empty string `""`. The XOR cipher works by XOR-ing each character of the plaintext with the corresponding character of the encryption key, repeating the key cyclically.
Implement the class `AIFeatureCache(capacity: int, encryption_key: str)` with these methods:
- `put(key: str, value: str) -> None`: store a text snippet in the cache, encrypting it at rest. If the key already exists, update its value and mark it as most recently used. If the cache is at capacity, evict the least recently used entry before inserting.
- `get(key: str) -> str`: retrieve and decrypt a text snippet. Return the decrypted plaintext if the key exists, otherwise an empty string. Mark the accessed key as most recently used.
- `remove(key: str) -> bool`: remove a key from the cache. Return `True` if the key was found and removed, `False` otherwise.
- `size() -> int`: return the current number of entries stored in the cache.
In the examples, the first input line lists the calls, the second line lists their arguments, and the output holds each call's return value (`null` for the constructor and for `put`).
Example 1:
```text
Input:
["AIFeatureCache", "put", "get", "remove", "get", "size"]
[[2, "key"], ["note1", "Private data"], ["note1"], ["note1"], ["note1"], []]
Output: [null, null, "Private data", true, "", 0]
```
The snippet is stored encrypted and decrypted on the first `get`. After `remove` it is gone, so the second `get` returns `""` and the size is 0.
Example 2:
```text
Input:
["AIFeatureCache", "put", "put", "put", "put", "get", "get", "size"]
[[2, "k"], ["a", "alpha"], ["b", "beta"], ["a", "omega"], ["c", "gamma"], ["b"], ["a"], []]
Output: [null, null, null, null, null, "", "omega", 2]
```
Updating `a` makes it the most recently used entry without evicting anything. When `c` arrives the cache is full and `b` is the least recently used entry, so `b` is evicted, `a` returns its new value, and two entries remain.
```hint Data structure
You need lookup by key and an order you can change in constant time. Think about which pair of structures gives both, and which end of the order holds the next eviction candidate.
```
```hint Cipher pitfall
XOR with the same key byte twice gives back the original value. Before you store the result in a byte container, check what happens to a character whose code point is above 255.
```
### Constraints & Assumptions
- `1 <= capacity <= 10^4`
- `1 <= len(encryption_key) <= 100`
- Keys are non-empty strings of alphanumeric characters and underscores.
- Values are non-empty strings.
- At most `10^4` total calls are made to `put`, `get`, `remove` and `size`.
### Clarifying Questions to Ask
- Can values contain characters outside ASCII, and should the cipher work on characters or on encoded bytes?
- If `put` updates a key that is already present while the cache is full, should anything be evicted?
- Does a `get` miss change the recency order of the remaining entries?
- Does "at rest" mean in memory only, or will the cache also be written to disk?
### What a Strong Answer Covers
- A structure that gives average O(1) lookup, recency update and LRU eviction, with a clear statement of which end holds the LRU entry.
- The recency rules: `get` and `put` both refresh an entry, a miss changes nothing, and updating an existing key never evicts.
- Encryption on `put` and decryption on `get` with the key repeating cyclically, and why the same XOR reverses itself.
- How the ciphertext is stored, including zero bytes and characters that do not fit in one byte.
- Per-operation complexity in terms of snippet length, and an honest account of how weak repeating-key XOR is.
### Follow-up Questions
- Repeating-key XOR is easy to break. How would an attacker recover the key, and what would you use instead on a real device?
- How would you make the cache safe to call from several threads at once?
- How would you bound the cache by total bytes stored instead of by entry count?
- The cache keys are stored in plaintext. When could that leak private information, and how would you hide them while keeping O(1) lookup?
Overview: Implement a fixed-capacity LRU cache that stores text snippets encrypted with a repeating-key XOR cipher and decrypts them on read, with get, put, remove and size. It tests hash map and ordered list design, recency and eviction rules, byte encoding of the ciphertext, and the limits of XOR as encryption.