You’re building an offline-first note-taking feature for a fintech app with 10M+ mobile DAUs. Users can edit notes while commuting in tunnels or on spotty Wi‑Fi. To prevent data loss and reduce bandwidth costs, the app stores edits locally and later flushes them to the server when connectivity returns.
Each local edit is represented as an operation:
("set", note_id, value) — set the note’s value to value("delete", note_id) — delete the noteWhen the device is offline, operations are appended to a local queue. When the device comes online, the app flushes the queue to the server.
To minimize network usage, before flushing you must deduplicate operations so that only the final operation per note_id is sent, while preserving the relative order of first appearance among the notes that remain.
Example: if note_id=7 is edited 5 times offline, only the last edit (or delete) should be sent.
Implement flush_offline_ops(ops) that returns the list of operations to send to the server after deduplication.
note_id, keep only the last operation affecting that note_id.note_id appears in the offline queue among those that survive.
A and B both survive, and A first appeared before B in ops, then A’s final operation must appear before B’s final operation.Example 1
ops = [("set", 1, "a"), ("set", 2, "x"), ("set", 1, "b"), ("delete", 2)][("set", 1, "b"), ("delete", 2)]set to "b". For note 2, the last operation is delete. Note 1 first appeared before note 2, so it stays first.Example 2
ops = [("delete", 5), ("set", 5, "revive"), ("set", 6, "z"), ("set", 5, "final")][("set", 5, "final"), ("set", 6, "z")]set to "final" (delete is superseded). Note 5 first appeared before note 6.note_id as an integer identifier.1 <= len(ops) <= 2 * 10^51 <= note_id <= 10^9value is a non-empty string of length <= 100("set", note_id, value) or ("delete", note_id)