This question made me flop
Anonymous User
343

Hello guys, I recently had an IBM assessment where I was unable to solve this last question relating to a caching problem. I've been leetcoding for a while so it's kind of a bummer that i couldnt solve it. Does anyone know how to would approach this?


.

.

.

.

A caching system uses 'priority' to decide what memory items are moved to the cache. All items start in the main memory with a priority of 0. Priority of all items decrease by 1 per second, but the priority of an item is bumped up by 2 instead of being decremented at that second whenever it is accessed. The minimum priority is O.
• When the priority of an item exceeds 5, it is moved to the cache.
• When the priority of an item in cache becomes less than or equal to 3 it is moved back to the main memory.
• If an item is accessed more than once in the same second, it will be incremented by 2 x (number of times it is accessed at that second).
The logs of all calls to access memory items will be provided in the format given below, not necessarily in any sorted order.
< timestamp> ‹item_id>

Return the item IDs of all items in the cache in ascending order, once the final log entry is made. If there is no item in the cache, return the array [-1]
For example, the logs are calllogs = [[1, 1], [2, 1], [2,1], [4, 2], [5, 2], [6, 2]]. Both of the items start at priority 0. In the table below, the number of times an item is accessed at a time is shown in the "access" column. An item is in the cache at the times it has an asterisk. At the end, only item 2 is in the cache. Note that at second 2, item 1 was accessed twice, so its priority increased 2 * times accessed that second = 4.

Time               Item 1                                 Item 2
                   access     priority                    access  priority
0                             0                                   0
1                   1         2                                   0
2                   2         6*                                  0
3                             5*                                  0
4                             4*                          1       2
5                             3                           1       4
6                             2                           1       6*

Function Description

Complete the cacheContents function. The function must return an integer array denoting the items present in the cache.

cacheContents has the following parameter(s): callLogs: A 2D integer array, that denotes the logs of the calls made to memory.

Constraints

  • 1≤n≤10
  • 1 ≤ callLogsbaseij≤ 103
Comments (1)