A queue delivers messages at least once and possibly out of order. Each message is
{"msg_id", "key", "seq", "op": "add" | "set", "value"} with seq starting at 1 per key. Apply each
key's operations exactly once, in seq order, to a value that starts at 0:
msg_id was already seen, or whose seq was already applied for its key, is a
duplicate (count it, ignore it).msg_ids with the same key and seq: the first one received wins; later ones are
duplicates.Return {"state": {key: value}, "applied": [msg_ids in apply order], "duplicates": n, "pending": {key: [buffered seqs, ascending]}} (keys with nothing pending omitted; state includes every
key that had at least one message applied).
Implement process. Input: messages: list[dict]. Returns: dict.
def process(messages: list[dict]) -> dict:
state = {}
for m in messages:
state[m["key"]] = state.get(m["key"], 0) + m["value"]
return {"state": state, "applied": [m["msg_id"] for m in messages], "duplicates": 0, "pending": {}}
Run executes the public tests. Submit grades against public and hidden tests.
A queue delivers messages at least once and possibly out of order. Each message is
{"msg_id", "key", "seq", "op": "add" | "set", "value"} with seq starting at 1 per key. Apply each
key's operations exactly once, in seq order, to a value that starts at 0:
msg_id was already seen, or whose seq was already applied for its key, is a
duplicate (count it, ignore it).msg_ids with the same key and seq: the first one received wins; later ones are
duplicates.Return {"state": {key: value}, "applied": [msg_ids in apply order], "duplicates": n, "pending": {key: [buffered seqs, ascending]}} (keys with nothing pending omitted; state includes every
key that had at least one message applied).
Implement process. Input: messages: list[dict]. Returns: dict.
def process(messages: list[dict]) -> dict:
state = {}
for m in messages:
state[m["key"]] = state.get(m["key"], 0) + m["value"]
return {"state": state, "applied": [m["msg_id"] for m in messages], "duplicates": 0, "pending": {}}
Run executes the public tests. Submit grades against public and hidden tests.