KV memory and quantization · Medium

KV Append and Rollback

Problem 34 of 50 · Python and NumPy

Problem

Apply a trace of cache operations and return {"keys": final key rows, "values": final value rows, "length": final token count}. Operation ["append", new_keys, new_values] appends matching rows to both caches. Operation ["rollback", length] truncates both caches to that absolute prefix length, not by that many tokens.

Return independent lists and do not mutate input rows or operation payloads. An empty append is a no-op; rollback to zero clears the cache.

def update_kv_cache(keys, values, operations):

Constraints

  • Initial and appended K/V have matching row counts and a consistent feature width 1..8 when nonempty.
  • 0..32 operations; every rollback target is between zero and the current length; at most 64 live rows.
  • Inputs satisfy these constraints; invalid-input handling is not graded. Use only Python standard library and NumPy.

Examples

Example 1

Arguments: [
  [],
  [],
  [
    [
      "append",
      [
        [
          1
        ]
      ],
      [
        [
          2
        ]
      ]
    ]
  ]
]
Expected result: {
  "keys": [
    [
      1
    ]
  ],
  "values": [
    [
      2
    ]
  ],
  "length": 1
}

Appending one key/value row produces a cache of length 1.

Example 2

Arguments: [
  [
    [
      1
    ],
    [
      2
    ]
  ],
  [
    [
      3
    ],
    [
      4
    ]
  ],
  [
    [
      "rollback",
      1
    ]
  ]
]
Expected result: {
  "keys": [
    [
      1
    ]
  ],
  "values": [
    [
      3
    ]
  ],
  "length": 1
}

Rollback target 1 keeps the first row in both caches and removes the second.

Sign in to solve this problem

Browse all coding problems · Learn the foundations