Data Structures & AlgorithmsHardAmazon · Microsoft OA
An LRU cache must support get and put in O(1). Which design achieves this?
- A. A min-heap keyed by last-access time
- B. A hash map to nodes of a doubly linked list kept in recency order
- C. A sorted array of (time, key) pairs
- D. A singly linked list with a hash map from key to node
Correct answer: B. A hash map to nodes of a doubly linked list kept in recency order
The map finds a node in O(1); the doubly linked list removes and reinserts it in O(1) because each node knows its predecessor. A heap needs O(log n) updates, and a singly linked list cannot unlink a node without its predecessor.
More Coding Interview Problems questions
- Edge case: identity versus equality in Python. python a = [1, 2]; b = [1, 2]; c = a print(a == b, a is b, a is c, [] is []) What is printed?
- Edge case: chained comparisons and booleans as integers in Python. python print(1 < 2 > 1.5, (1 < 2) > 1.5, True + True, 3 > 2 == 2) What…
- Edge case: closures over a loop variable declared with var versus let in JavaScript. javascript const a = [], b = []; for (var i = 0; i <…
- Problem: Minimum number of swaps (any two positions) needed to sort an array of distinct values, by counting cycles of the permutation…
- Problem: Count subarrays whose sum lies in [lo, hi] using prefix sums and a sorted list (bisect). python from bisect import bisectleft…
- Problem: Starting at index s, you may jump from i to i + a[i] or i − a[i]. Can you reach any index holding 0? python def canreach(a, s)…