[ INFO ]category: Coding difficulty: medium freq: high first seen: 2026-01-15
[MEDIUM][CODING][HIGH]Custom Data StructureDesignPrefix Sum
$catproblem.md
Design a data structure that maintains an ordered sequence of items, each with an associated length, and supports two operations:
add(item_name, length) – appends the item to the end of the sequence (or updates the length if the item already exists, replacing its old position and collapsing the gap).
offset(item_name) – returns the sum of the lengths of all items that appear before the named item in the current order (i.e., the byte-offset at which this item’s data would start in a flat serialization). If the item is not present, return -1.
The structure must efficiently handle these operations as the sequence grows, and queries should return the correct offset even after any number of interleaved additions and updates.