Prepin
Log in

Create an LCU cache design.

Approach / Explanation

This question asks you to create a Least Recently Used (LRU) cache system. In the realm of computing, a cache is a component, either hardware or software, that saves data so that subsequent requests for that data can be fulfilled more quickly. The LRU algorithm prioritizes retaining the most recently accessed items while removing those that are least recently accessed. Implementing this requires a solid grasp of data structures, particularly linked lists and hash tables, as these are typically the primary elements of an LRU cache. To effectively respond to this question, consider the following guidelines: Describe how you utilize data structures in the implementation of the LRU cache. Elaborate on the 'get' and 'put' operations within the cache. Provide insights into the time complexity associated with these operations.

Suggested Answer

A highly effective method for implementing an LRU cache involves using a combination of a doubly linked list and a hash map. The doubly linked list serves to represent the cache size and track both the 'Least Recently Used' and 'Most Recently Used' elements. Meanwhile, the hash map is utilized to verify whether an item is present in the cache and to quickly access its corresponding node in the linked list. The 'get' operation requires checking for the item’s existence in the cache through the hash map and returning its value. If the item is found, the linked list must be updated to indicate that it is now the 'Most Recently Used'. If the item is not found, we can simply return -1 or null. The 'put' operation consists of adding an item to the cache. If the cache is at full capacity, we need to remove the least recently used item before adding the new one, which is located at the tail of the linked list. Both the insertion and removal processes at the head and tail of a doubly linked list, along with operations on a hash map, have a time complexity of O(1).

Alternative Answer

A practical approach to implementing an LRU cache is by using built-in data structures available in some programming languages. For example, in Python, we can utilize an OrderedDict. This structure effectively merges a doubly linked list with a hash map, making it ideal for this application. The 'get' operation retrieves the value associated with a specified key if it exists, and then it moves this key-value pair to the end of the OrderedDict to indicate that it has been recently accessed. If the key is not found, a default return value should be provided. The 'put' operation introduces a new item into the cache. If the cache is full before the new addition, the oldest or Least Recently Used item in the OrderedDict will be removed. The use of the 'move_to_end' method along with the popitem() function ensures that these operations are performed in constant time. The time complexity for both get and put operations is O(1) thanks to the underlying structure of OrderedDict.

Question Details

Difficulty & Category

Medium
system design

Companies

WhatsApp

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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