← All Exercises

Flush an Offline Queue with Deduplication

intermediatejavascript~25min

Background

Mobile clients queue writes offline. Replaying every duplicate write produces conflicting server state and wasted bandwidth after a long disconnection.

Problem

When connectivity returns, flush queued operations in order while collapsing repeated writes to the same entity into the latest one.

Input

JSON object: { queued: [{ id, entity, op, ts }] }

Output

JSON object: { flushed: string[], collapsed: string[] }

Sample tests

Sample 1

Input: {"queued":[{"id":"q1","entity":"note-1","op":"create","ts":1},{"id":"q2","entity":"note-1","op":"update","ts":2},{"id":"q3","entity":"note-2","op":"create","ts":3}]}
Expected: {"flushed":["q2","q3"],"collapsed":["q1"]}

Sample 2

Input: {"queued":[{"id":"q4","entity":"a","op":"create","ts":1},{"id":"q5","entity":"b","op":"create","ts":2}]}
Expected: {"flushed":["q4","q5"],"collapsed":[]}

Your solution

Learning objective

Make offline sync converge to the same state as online edits.