Prepin
Log in

Create an LRU cache using JavaScript.

Approach / Explanation

This question necessitates an understanding of both JavaScript and the Least Recently Used (LRU) Cache concept. An LRU Cache organizes items based on their usage frequency, enabling quick identification of the item that has not been used for the longest time. To clarify the question further, you are tasked with implementing an LRU Cache in JavaScript. The cache should be capable of storing a specified number of key-value pairs determined by the initial capacity. If a key that is already in the cache is inserted again, or if a read operation is executed on an existing key, that key should be moved to the most recently used position. When the cache reaches its capacity limit, it should remove the items that have been least recently used. The cache must support the following operations: Retrieving a key from the cache Inserting a key-value pair into the cache Ensure that your implementation reflects a clear understanding of the LRU algorithm, particularly the "least recently used" aspect, and how JavaScript data structures can be utilized to achieve this functionality.

Suggested Answer

A simple approach to implementing an LRU Cache can involve using JavaScript's Map data structure, which preserves the order of key insertion, making it easier to identify and remove the least recently used entries. Below is a basic LRU Cache class: class LRUCache { constructor ( capacity ) { this . capacity = capacity ; this . cache = new Map ( ) ; } get ( key ) { if ( ! this . cache . has ( key ) ) return - 1 ; let temp = this . cache . get ( key ) ; this . cache . delete ( key ) ; this . cache . set ( key , temp ) ; return temp ; } put ( key , value ) { if ( this . cache . has ( key ) ) { this . cache . delete ( key ) ; } else if ( this . cache . size >= this . capacity ) { this . cache . delete ( this . cache . keys ( ) . next ( ) . value ) ; } this . cache . set ( key , value ) ; } }

Alternative Answer

Furthermore, you can manually create a linked list alongside a map to manage the cache and maintain the LRU (Least Recently Used) property: class Node { constructor ( key , value ) { this . key = key ; this . value = value ; this . prev = null ; this . next = null ; } } class LRUCache { constructor ( capacity ) { this . capacity = capacity ; this . map = new Map ( ) ; this . head = new Node ( 0 , 0 ) ; this . tail = new Node ( 0 , 0 ) ; this . head . next = this . tail ; this . tail . prev = this . head ; } _delete ( node ) { node . prev . next = node . next ; node . next . prev = node . prev ; } _add ( node ) { let temp = this . head . next ; this . head . next = node ; node . prev = this . head ; node . next = temp ; temp . prev = node ; } get ( key ) { if ( this . map . has ( key ) ) { let node = this . map . get ( key ) ; this . _delete ( node ) ; this . _add ( node ) ; return node . value ; } else { return - 1 ; } } put ( key , value ) { if ( this . map . has ( key ) ) { this . _delete ( this . map . get ( key ) ) ; } let node = new Node ( key , value ) ; this . _add ( node ) ; this . map . set ( key , node ) ; if ( this . map . size > this . capacity ) { let last = this . tail . prev ; this . _delete ( last ) ; this . map . delete ( last . key ) ; } } } This method introduces additional complexity by requiring the creation of a doubly linked list, but it effectively demonstrates how to implement an LRU Cache without utilizing the built-in ordered feature provided by JavaScript's Map.

Question Details

Difficulty & Category

Medium
algorithms

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

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