The old receipt that deletes a new delivery
A slow worker can acknowledge a newer delivery, and visibility changes use the wrong deadline.
What’s going wrong
Original Amazon-style interview practice. This is not an actual Amazon interview question. Bugger does not provide an AI assistant.
A queue simulator issues a fresh receipt whenever a message is delivered again. A slow worker holding an old receipt can nevertheless delete the message after another worker receives it. Changing visibility can also hide a message longer than the requested interval. Repair lease ownership and deadline updates across the event history.
Input is {"messages":["a","b"],"visibility":5,"events":[...]}. Message IDs are distinct. All messages initially exist and are visible at time 0. Process events in array order; at values are nondecreasing integers. A receive event is {"op":"receive","at":0}. It takes the first nondeleted message in original message order whose visible_at is at most the event time. A successful receive issues the next global receipt "r1", "r2", and so on, sets that message’s latest receipt, and hides it until at + visibility. An unavailable receive returns null and does not consume a receipt number. Every redelivery uses the configured visibility value again.
An ack event is {"op":"ack","at":1,"receipt":"r1"}. A change event is {"op":"change","at":1,"receipt":"r1","duration":3}. Either operation is accepted only for the message’s latest receipt while the message exists and the event time is strictly before its visibility deadline. Accepted ack deletes the message. Accepted change replaces the deadline with event time + duration, rather than adding duration to the previous deadline. Duration zero releases the message immediately, so that receipt is no longer live even for a later event at the same time. Unknown, superseded, expired or deleted receipts return false and change nothing.
Print {"results":[...],"remaining":[...],"visible":[...]}. Each receive result is {"id":"a","receipt":"r1"} or null; each ack/change result is true when accepted or false otherwise. Remaining contains every nondeleted message; visible contains nondeleted messages available at the final event time, or time 0 when events is empty. Both lists preserve initial message order. Valid input has at most 50 messages and 100 events; IDs and receipt strings are nonempty ASCII strings up to 32 characters. Times are integers from 0 to 1000000, visibility is from 1 to 20, and duration is from 0 to 20. This is a strict deterministic lease model inspired by SQS; booleans describe simulated state changes, not AWS HTTP responses.
Expected behavior
Only a live current receipt may acknowledge or change its delivery; expiry is exclusive, visibility changes start at the event time, and stale workers must not alter a newer lease.
Reproduction
{"messages":["a"],"visibility":5,"events":[{"op":"receive","at":0},{"op":"receive","at":5},{"op":"ack","at":6,"receipt":"r1"},{"op":"receive","at":6},{"op":"ack","at":6,"receipt":"r2"}]}{"results":[{"id":"a","receipt":"r1"},{"id":"a","receipt":"r2"},false,null,true],"remaining":[],"visible":[]}{"messages":["a"],"visibility":5,"events":[{"op":"receive","at":0},{"op":"ack","at":4,"receipt":"r1"},{"op":"receive","at":5}]}{"results":[{"id":"a","receipt":"r1"},true,null],"remaining":[],"visible":[]}{"messages":["a"],"visibility":5,"events":[{"op":"receive","at":0},{"op":"change","at":4,"receipt":"r1","duration":3},{"op":"receive","at":7}]}{"results":[{"id":"a","receipt":"r1"},true,{"id":"a","receipt":"r2"}],"remaining":["a"],"visible":[]}Before you begin
- Versioned ownership tokens
- Visibility deadlines
- Tracing ordered event histories
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.