Which data structure is best suited for implementing LRU Cache?Singly Linked ListDoubly linked list with a hash mapCircular linked listArray
Question
Which data structure is best suited for implementing LRU Cache?Singly Linked ListDoubly linked list with a hash mapCircular linked listArray
Solution
The best data structure for implementing LRU (Least Recently Used) Cache is a Doubly Linked List with a Hash Map.
Here's why:
-
Doubly Linked List: This is used to store the keys of cache with the most recently used keys at the start of the list and the least recently used keys at the end. The advantage of using a doubly linked list is that it supports O(1) time complexity for all operations (insertion, deletion, moving an element to the start) if we have a direct reference to the node containing key.
-
Hash Map: This is used for supporting quick look-up (O(1) time complexity) to check if a key is present in the cache or not. The key in the hash map is the actual key and the value is the address of the node in the doubly linked list.
So, the combination of these two data structures provides an efficient way of implementing LRU Cache.
Similar Questions
Which data structure is best suited for implementing a cache, where the most recently accessed elements are retained?Select one:a. Queueb. Linked Listc. Stackd. Heap
Linked lists are best suitedA for relatively permanent collections of dataB for the size of the structure and the data in the structure are constantly changingC for both of above situationD for none of above situatio
Which data structure is known for its fast random access but has a fixed size once allocated?*1 pointa. Arrayb. Queuec. Linked List
Which of the following is an application of XOR-linked lists?Implementing stacksImplementing queuesMemory-efficient linked list representationCaching data structures
The elements of a linked list are storeda.In a structureb.In an arrayc.Anywhere the computer has space for themd.In contiguous memory locatio
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.