Paged KV Cache Access
Updated
Problem statement
Implement the access path of a paged key/value (KV) cache: process operations in order and return one list of integers per operation.
The cache has numPages physical pages, numbered 0 to numPages - 1, and each page holds pageSize tokens. Each sequence has a name and a page table: the list of the physical pages it uses, in order. The tokens of a sequence have positions 0, 1, 2, ... in the order they were appended. Position p is stored in the page at entry p / pageSize of the page table (integer division), at offset p % pageSize in that page. Each token is a single integer that stands in for its key and value vectors.
Each operation is a string, and its result is a list of integers:
"APPEND|name|v1,v2,...,vk"adds the values as new tokens at the end of sequencename. A name that is not stored starts a new, empty sequence. A token takes a new page only when the sequence has no page yet or its last page is full. The result is the page numbers this append took, in the order it took them;[]if all the values fit in the free room of the last page; or[-1]if the append is refused (see the two page rules below)."READ|name|start|count"returns the values at positionsstarttostart + count - 1ofname, in order."LOOKUP|name|pos"returns[page, offset]: the physical page that holds positionposofname, and the offset inside that page."FREE|name"gives all pages ofnameback and forgets the sequence. The result is[the number of pages given back], which is[0]ifnameis not stored. A laterAPPENDto the same name starts a new sequence at position0.
Two page rules for APPEND. First, all pages start free, and a new page is always the lowest-numbered free page, no matter in which order pages were freed. Second, an append is all or nothing: if it needs more new pages than are free, nothing is stored (not even the values that would fit in the last page's free room) and the result is [-1]. A refused append to a name that is not stored does not create it.
Function
pagedKvCache(pageSize: int, numPages: int, operations: String[]) → List<List<Integer>>Examples
Example 1
pageSize = 2numPages = 4operations = ["APPEND|a|5,6,7","APPEND|b|8","APPEND|a|9,4","LOOKUP|a|4","READ|a|2|3","FREE|b"]return = [[0,1],[2],[3],[3,0],[7,9,4],[1]]Each page holds 2 tokens. a needs 2 pages for 3 tokens and takes pages 0 and 1. b takes page 2. Token 9 fills the free spot in page 1, and token 4 needs a new page, so a takes page 3. Position 4 of a is entry 4 / 2 = 2 of its page table (page 3) at offset 4 % 2 = 0. Reading positions 2 to 4 starts in page 1 and continues in page 3. Freeing b gives back 1 page.
Example 2
pageSize = 3numPages = 2operations = ["APPEND|x|1,2,3,4","APPEND|y|7","APPEND|x|5,6","APPEND|x|8","READ|x|2|4","FREE|y"]return = [[0,1],[-1],[],[-1],[3,4,5,6],[0]]x takes both pages. y needs a page and none is free, so nothing is stored and the result is [-1]. Tokens 5 and 6 fit in the 2 free spots of x's second page, so no page is taken. Token 8 would need a third page, so it is refused. y was never stored, so freeing it gives back 0 pages.
Example 3
pageSize = 2numPages = 4operations = ["APPEND|a|1,2","APPEND|b|3","APPEND|c|4,5,6","FREE|c","FREE|a","APPEND|d|7,8,9","APPEND|a|10","LOOKUP|d|2","READ|a|0|1"]return = [[0],[1],[2,3],[2],[1],[0,2],[3],[2,0],[10]]After c and then a are freed, pages 0, 2 and 3 are free. d needs 2 pages and takes the two lowest-numbered ones, 0 and 2, no matter in which order they were freed. a was freed, so it starts again as a new sequence at position 0 and takes the last free page, 3.
Constraints
1 <= pageSize <= 641 <= numPages <= 10001 <= operations.length <= 1000- Each
operations[i]has one of the four forms above and at most530characters. Numbers are written in decimal without leading zeros. - Each sequence name has
1to10characters, each a lowercase letter or a digit. - Each
APPENDhas1to64values, and each value is from0to1000000. - For
READ|name|start|count:nameis stored,0 <= start,1 <= count, andstart + countis at most the number of tokens stored forname. - For
LOOKUP|name|pos:nameis stored and0 <= pos< the number of tokens stored forname.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Key-Value Store on a Block DeviceLambda · PHONE SCREEN
- Design Authentication ManagerApple · PHONE SCREENApple interview questions
- LLM Provider Pool FailoverPerplexity AI · PHONE SCREENPerplexity AI interview questions
- ID Allocator with Assign and ReleaseSnap Inc. · PHONE SCREENSnap Inc. interview questions
- 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Capacity-Limited Timed CacheNetflix · PHONE SCREENNetflix interview questions
- Tiered Expiring Item StoreHudson River Trading · PHONE SCREEN · ONSITE INTERVIEWHudson River Trading interview questions
- Tiered Expiring Item StoreAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Multi-Level Inventory Storage SystemOptiver · OA · PHONE SCREENOptiver interview questions
- Limit Order Book Matching EngineCitadel · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions