Data Structures & AlgorithmsVery hardAmazon/Microsoft SDE pattern
An LRU cache of capacity 3 runs this sequence of operations in order: put(8, 80), put(8, 80), put(6, 60), put(7, 70), put(8, 80), put(1, 10). (get also marks a key as most-recently used.) What keys remain in the cache at the end, from least to most recently used?
- A. [7, 8] (most to least recently used: same order)
- B. [1, 8, 7] (most to least recently used: 7, 8, 1)
- C. [7, 8, 1] (most to least recently used: 1, 8, 7)
- D. [6, 7, 1] (most to least recently used: 1, 7, 6)
Correct answer: C. [7, 8, 1] (most to least recently used: 1, 8, 7)
An LRU cache evicts the least-recently-used key only when inserting a new key would exceed capacity, and every get or re-put of a key moves it to the most-recently-used end. Tracing the 6 operations in order leaves [7, 8, 1] (most to least recently used: 1, 8, 7). A FIFO policy (evict by insertion order, ignoring get) would instead leave [6, 7, 1] (most to least recently used: 1, 7, 6), which is a different (and wrong) eviction rule for LRU.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Kadane's algorithm runs on the array [2, -8, 4, -2, 4, -5, -2] to find the maximum sum of a contiguous subarray. What sum does it return?
- What is the postfix (Reverse Polish) form of the infix expression "j + e f -(b + a)", built with the standard stack-based conversion (^…
- What is the sum of the minimum element over every contiguous subarray of [8, 1, 5, 4, 4, 2]?
- Binary search for first/last occurrence runs on the sorted array [5, 6, 7, 7, 15, 18, 19, 20] for target 7. What (first index, last index)…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…
- A singly linked list holds 36 -> 38 -> 51 -> 57 -> 28 -> None (in that order, 5 nodes). Using the standard two-pointer technique (advance…