Social feed~45 min2s · 256 MBtarget O(n) time, O(n) spacelru · linked-lists · hashing
A feed app keeps the rendered feed page of each user in memory, so that opening the app is fast. A feed page has a version number and a size, in units of memory. The cache holds feed pages up to a total size, not up to a number of pages.
When the cache is over its size, it removes the page that was used least recently, until everything fits. A user who changes their settings can also have their page dropped on purpose.
Write run_feed_cache(capacity, commands, args).
capacity.commands[i] is "put", "get" or "drop". For "put", args[i] is [user, version, size]. For "get" and "drop", it is [user]."get": if the user has a page, return its version and make it the most recently used. Otherwise return -1."drop": if the user has a page, remove it and return 1. Otherwise return 0."put": first remove the user's old page, if any. If size is more than capacity, store nothing and return -1. Otherwise store the page as the most recently used, then remove least recently used pages while the total size is more than capacity. Return the number of pages removed that way.Input: capacity = 10, commands = ["put", "put", "put", "get", "put", "put", "drop", "drop", "get", "get"], args = [[1, 100, 4], [2, 200, 3], [3, 300, 5], [2], [1, 101, 2], [4, 400, 11], [3], [9], [3], [1]]
Output: [0, 0, 1, 200, 0, -1, 1, 0, -1, 101]The trace lists the pages in the cache, most recently used first.
| i | Command | Cache, most recent first | Total size | Answer |
|---|---|---|---|---|
| 0 | put user 1, version 100, size 4 | 1 (v100, size 4) | 4 | 0 |
| 1 | put user 2, version 200, size 3 | 2 (v200, size 3), 1 (v100, size 4) | 7 | 0 |
| 2 | put user 3, version 300, size 5 | 3 (v300, size 5), 2 (v200, size 3) | 8 | 1 |
| 3 | get user 2 | 2 (v200, size 3), 3 (v300, size 5) | 8 | 200 |
| 4 | put user 1, version 101, size 2 | 1 (v101, size 2), 2 (v200, size 3), 3 (v300, size 5) | 10 | 0 |
| 5 | put user 4, version 400, size 11 | 1 (v101, size 2), 2 (v200, size 3), 3 (v300, size 5) | 10 | -1 |
| 6 | drop user 3 | 1 (v101, size 2), 2 (v200, size 3) | 5 | 1 |
| 7 | drop user 9 | 1 (v101, size 2), 2 (v200, size 3) | 5 | 0 |
| 8 | get user 3 | 1 (v101, size 2), 2 (v200, size 3) | 5 | -1 |
| 9 | get user 1 | 1 (v101, size 2), 2 (v200, size 3) | 5 | 101 |
The third put brings the total to 12, so the least recently used page, the one of user 1, is removed. The put of user 4 is bigger than the whole cache, so it stores nothing. The last get finds user 1, whose second page was stored at i = 4.
Input: capacity = 5, commands = ["put", "put", "get", "get"], args = [[1, 10, 5], [2, 20, 5], [1], [2]]
Output: [0, 1, -1, 20]The first page fills the cache. The second page needs the space, so the first is removed. The get of user 1 returns -1, and the get of user 2 returns 20.
Input: capacity = 6, commands = ["put", "put", "put", "put"], args = [[1, 1, 3], [2, 2, 3], [1, 3, 3], [4, 4, 3]]
Output: [0, 0, 0, 1]The third put replaces the page of user 1, so user 1 is now the most recently used. The fourth put needs space, and removes the least recently used page, which belongs to user 2. One page is removed.
1 ≤ capacity ≤ 1,000,000,000 (units of memory)1 ≤ commands.length ≤ 100,000; each command is "put", "get" or "drop"; args has the same lengthput row has 3 numbers and the others have 1; users, versions and sizes are between 0 and 1,000,000,000, and sizes are at least 1Looking for a user's page, and finding the least recently used page, by scanning the whole cache costs up to n × n / 2 steps, about 5 × 10^9 steps. A hash map finds a page in one step, and a doubly linked list keeps the order of use.
The page to remove is always at the back. A page that is used moves to the front. So every command takes constant time, apart from removals, and a page is removed at most once.