Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: An LRU (Least Recently Used) cache with O(1) get and put operations is classically implemented with which combination of data structures?
- A. A hash map (key -> node) plus a doubly linked list (to reorder/evict in O(1))
- B. A min-heap keyed by last-access time
- C. A single sorted array, binary-searched on every access
- D. A plain hash map alone, with no ordering structure
Correct answer: A. A hash map (key -> node) plus a doubly linked list (to reorder/evict in O(1))
The hash map gives O(1) lookup of a node by key; the doubly linked list lets that node be unlinked and moved to the front (most-recently-used end) in O(1), and the least-recently-used node (at the tail) can be evicted in O(1) too.
More Coding Interview Problems questions
- Problem: Which statement about Kruskal's algorithm's overall time complexity is correct for a graph with V vertices and E edges?
- Problem: Topological sort is only defined for which kind of graph, and what happens if Kahn's algorithm is run on a graph that violates…
- Problem: Which best explains why merge sort is preferred over quicksort for sorting linked lists (as opposed to arrays)?
- Problem: Which best explains the time complexity difference between recursive (top-down with memoization) and iterative (bottom-up…
- Problem: What is the time complexity of building a trie from n words with total character count L, and what is the space complexity?
- Problem: A problem is solved via binary search on the answer (not on an array index) -- e.g. 'minimum capacity to ship packages within D…