Transformer KV Cache Operations
Updated
Problem statement
Implement a Transformer key-value cache configured with layerCount layers, headCount attention heads, and headDimension values per head. Each sequence ID owns an independent ordered token cache at each layer. A token stores one key tensor and one value tensor, each flattened in row-major order into exactly headCount * headDimension integers.
Process these operations in order:
APPEND|sequenceId|layer|keyValues|valueValues: append one token to the sequence and layer. The two value fields are comma-separated flattened tensors. Return the token's zero-based index in that layer.READ|sequenceId|layer|startInclusive|endExclusive: return the requested ordered token slice. Serialize each token askeyValues/valueValuesand separate tokens with semicolons. Return an empty string for an empty slice or a missing sequence.RESET|sequenceId: remove every cached token for the sequence across all layers and return the total number removed. A missing sequence returns0. A later append for that sequence starts again at index0.
Return one result string for every operation, in operation order.
Function
processTransformerKvCache(layerCount: int, headCount: int, headDimension: int, operations: String[]) → String[]Examples
Example 1
layerCount = 2headCount = 1headDimension = 2operations = ["APPEND|chat|0|1,2|10,20","APPEND|chat|0|3,4|30,40","APPEND|chat|1|5,6|50,60","READ|chat|0|0|2","RESET|chat","READ|chat|0|0|0"]return = ["0", "1", "0", "1,2/10,20;3,4/30,40", "3", ""]Token indices count separately for each sequence and layer. The first append to the first layer of chat gets index zero and the second gets index one. The append to the other layer starts at index zero. The read returns both tokens in order, each written as its keys, a slash, then its values, with a semicolon between tokens. Reset removes two tokens from layer 0 and one token from layer 1, so it returns three. The last read finds nothing and is empty.
Example 2
layerCount = 2headCount = 1headDimension = 1operations = ["APPEND|a|0|7|70","APPEND|b|0|8|80","READ|a|0|0|1","RESET|b","APPEND|b|0|9|90","READ|b|0|0|1"]return = ["0", "0", "7/70", "1", "0", "9/90"]Sequences a and b are independent. Each gets its first token at index zero, and the read of a returns its token. Resetting b removes its one token and returns one, but a keeps its token. The next token for b starts again at index 0, and reading it back returns that new token.
Example 3
layerCount = 1headCount = 2headDimension = 2operations = ["READ|missing|0|0|0","RESET|missing","APPEND|s|0|1,-2,3,-4|5,6,7,8","READ|s|0|0|1"]return = ["", "0", "0", "1,-2,3,-4/5,6,7,8"]The sequence missing does not exist, so reading it gives an empty string and resetting it gives zero. The append to s gets index zero. Each key and value has four numbers, because it holds one tensor per head and each tensor has two values. The last read returns the keys and values in the same flat order they were appended.
Constraints
1 <= layerCount <= 1281 <= headCount <= 1281 <= headDimension <= 2561 <= operations.length <= 10000- There are at most
1000active sequence IDs and at most100000cached tokens at any time. - Every layer is in
[0, layerCount - 1]. Each appended key and value contains exactlyheadCount * headDimensionsigned integers. - For an existing sequence and layer, every read satisfies
0 <= startInclusive <= endExclusive <= tokenCount.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE 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
Similar questions at other companies
- Process On-Call Routing OperationsOpenAI · PHONE SCREENOpenAI interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Role-Based Network Access ControlOkta · PHONE SCREENOkta interview questions
- Track Package Receipt GapsAmazon · PHONE SCREEN
- Rate Limiter with Hourly and Daily Effort QuotasZscaler · PHONE SCREENZscaler interview questions
- Chatter Message WindowReddit · PHONE SCREENReddit interview questions
- Versioned Recipe Shopping CartDecagon · PHONE SCREENDecagon interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions