Prepin
Log in

P

R

W

Create a Least Recently Used (LRU) cache.

Approach / Explanation

This task requires you to create a Least Recently Used (LRU) cache system, which is a type of caching mechanism that evicts the least recently accessed entries when capacity is reached. You should cover the various components, their roles, and how they interact with one another. Key aspects to address in your response include: Discussing the features of an LRU Cache and its benefits for particular application scenarios. Outlining your approach to initializing the structure, managing the cache, and removing the least recently used entries. Detailing the data structures and algorithms you intend to utilize, with common options being a doubly-linked list combined with a hashmap.

Suggested Answer

An LRU cache, which stands for Least Recently Used Cache, removes the least recently accessed items first when it reaches its capacity. This type of data structure is advantageous for providing quick access to the most recently used data. To implement an LRU cache, I would utilize a combination of a hashmap and a doubly linked list. The hashmap functions as an index, while the doubly linked list keeps track of the order of access. Each node in the doubly linked list represents a cache entry, and the hashmap connects each key to its corresponding node in the list. When a get operation is performed, we use the hashmap to quickly find the item and return its value. At the same time, we reposition the accessed node to the front of the list to indicate that it is the most recently used. For a put operation, if the cache is not already full, we create a new node and insert it at the front of the list, while also adding the key-node pair to the hashmap. If the cache is full, we remove the last node in the list along with its key from the hashmap before adding the new node. This ensures that the least recently used item is eliminated.

Alternative Answer

I would opt for a strategy that employs a doubly linked list in conjunction with a hash map to create an LRU cache. This combination allows for efficient time complexity for both retrieval and modification operations. For each get and put operation, I would consult the hash map. If the element is found, I would either return its value or update it (in the case of a put operation) and then move the corresponding node to the front of the doubly linked list. If the element is not found and the cache has reached its capacity, I would remove the least recently used item located at the tail of the doubly linked list. After that, I would insert the new data at the front of the linked list and update the hash map accordingly. This design ensures that the most recent data is always positioned at the front, simplifying the eviction process when the cache is full.

Question Details

Difficulty & Category

Medium
system design

Companies

Microsoft
Peloton
Robinhood
WhatsApp

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.