Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA
Problem: An LRU cache of capacity c must support get(key) and put(key, value) in O(1) each, evicting the least recently used key. Which data structure combination achieves this?
- A. Min-heap keyed by last access time
- B. Hash map from key to node + doubly linked list ordered by recency
- C. Singly linked list alone
- D. Balanced BST keyed by last access time
Correct answer: B. Hash map from key to node + doubly linked list ordered by recency
The map finds the node in O(1); a doubly linked list moves it to the front or removes the tail in O(1) because each node knows its predecessor. A heap or BST makes every access O(log c); a singly linked list cannot unlink a node in O(1) without its predecessor and needs O(c) search.
More Coding Interview Problems questions
- Problem: The function below should check whether a singly linked list is a palindrome in O(1) extra space. python class Node: def…
- Problem: Return the height (number of nodes on the longest root-to-leaf path) of an org-chart tree. Trees are built by build(xs): a…
- Problem: Print the level-order traversal of a tree, one list per level. Trees are built by build(xs): a level-order list where index i has…
- Problem: Return the right-side view of a tree (the last node of each level). Trees are built by build(xs): a level-order list where index…
- Problem: Check whether a binary tree is a valid BST. The candidate solution compares each node only with its children. Trees are built by…
- Problem: Find the lowest common ancestor of two keys in a BST. Trees are built by build(xs): a level-order list where index i has children…