KatInterview

Parcel Attribute Store with TTL and Prefix Queries

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationsorting

Last updated

Simulate a timestamped key-value attribute store with overwrite, conditional update, sorted prefix listing, and per-attribute expiry.

You are given a list of operation names and a parallel list of argument lists. Process them in order against an in-memory store of parcels. Each parcel is identified by a string parcel ID and holds attributes, each having a name, a single value, and an expiration deadline. Every argument list begins with a non-negative integer timestamp. Produce a result for each operation, appended in the order the operations are processed, and return the list of results.

Supported operations:

  • "set": args are (timestamp, parcel_id, attribute, value) or (timestamp, parcel_id, attribute, value, ttl). Store value under attribute for the parcel, replacing any prior value and deadline. If ttl is omitted treat it as 0. A ttl of 0 means the attribute never expires; otherwise the attribute expires exactly ttl time units after timestamp, i.e. it is live at time t if and only if t < timestamp + ttl. Result: None.

  • "get": args are (timestamp, parcel_id, attribute). Result: the attribute's value if it exists and is live at timestamp, otherwise None.

  • "update_if_match": args are (timestamp, parcel_id, attribute, expected, new_value) or (timestamp, parcel_id, attribute, expected, new_value, ttl). If the attribute exists, is live at timestamp, and its current value equals expected exactly, replace its value with new_value and its deadline according to ttl (omitted means 0, meaning never expires); result True. Otherwise change nothing and result False. This operation cannot create a missing attribute.

  • "list_attributes": args are (timestamp, parcel_id). Result: a list of strings, one per live attribute of the parcel, each formatted as name(value), ordered ascending by attribute name using plain string comparison.

  • "list_attributes_with_prefix": args are (timestamp, parcel_id, prefix). Same as list_attributes but only attributes whose names start with prefix are included.

An unknown parcel behaves like a parcel with no attributes. Expired attributes are treated as absent for all reads and for the conditional update; you may either hide them or delete them. When a write succeeds, the new lifetime starts from that call's timestamp using that call's ttl.

Constraints

  • 1 <= len(operations) <= 10^4
  • len(arguments) == len(operations), and argument lists have the lengths described above.
  • timestamp is a non-negative integer and is non-decreasing across calls.
  • ttl is a non-negative integer; ttl == 0 means the attribute never expires.
  • Attribute names and values are strings; matching in update_if_match is exact and case-sensitive.
  • An unknown operation should raise ValueError, but valid inputs only contain the five operations above.

Examples

Example 1

Input: process_parcel_operations(['set', 'get', 'update_if_match', 'get', 'list_attributes', 'list_attributes_with_prefix'], [[1, 'p1', 'color', 'blue'], [2, 'p1', 'color'], [3, 'p1', 'color', 'blue', 'green'], [4, 'p1', 'color'], [5, 'p1'], [6, 'p1', 'co']])

Output: [None, 'blue', True, 'green', ['color(green)'], ['color(green)']]

Example 2

Input: process_parcel_operations(['set', 'get', 'get', 'list_attributes'], [[10, 'p2', 'size', 'large', 5], [14, 'p2', 'size'], [15, 'p2', 'size'], [15, 'p2']])

Output: [None, 'large', None, []]

Hints

Hint 1

Store each parcel as a dict mapping attribute name to a pair (value, expiry_timestamp), where expiry_timestamp is None for attributes that never expire.

Hint 2

Write one helper that decides whether an entry is live at a given timestamp: ttl 0 means always live, otherwise live only when timestamp < expiry, so the entry is dead starting exactly at expiry.

Hint 3

For listings, collect the live names, sort them, then format each as name(value); for the prefix version reuse that same code with an extra name.startswith(prefix) filter.