Dataford
Interview Guides
Upgrade
All questions/Coding/Offline Sync Queue With Dedup

Offline Sync Queue With Dedup

Medium
Coding

Problem

Problem

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 note

When the device is offline, operations are appended to a local queue. When the device comes online, the app flushes the queue to the server.

Key offline requirement (deduplication)

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.

Task

Implement flush_offline_ops(ops) that returns the list of operations to send to the server after deduplication.

Deduplication rules

  1. For each note_id, keep only the last operation affecting that note_id.
  2. The output order is determined by the first time each note_id appears in the offline queue among those that survive.
    • In other words, if notes 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.

Examples

Example 1

  • Input: ops = [("set", 1, "a"), ("set", 2, "x"), ("set", 1, "b"), ("delete", 2)]
  • Output: [("set", 1, "b"), ("delete", 2)]
  • Explanation: For note 1, the last operation is set to "b". For note 2, the last operation is delete. Note 1 first appeared before note 2, so it stays first.

Example 2

  • Input: ops = [("delete", 5), ("set", 5, "revive"), ("set", 6, "z"), ("set", 5, "final")]
  • Output: [("set", 5, "final"), ("set", 6, "z")]
  • Explanation: Note 5’s last operation is set to "final" (delete is superseded). Note 5 first appeared before note 6.

Notes

  • Treat note_id as an integer identifier.
  • You do not need to validate operation semantics (e.g., deleting a non-existent note is allowed).

Constraints

  • 1 <= len(ops) <= 2 * 10^5
  • 1 <= note_id <= 10^9
  • value is a non-empty string of length <= 100
  • Each operation is either ("set", note_id, value) or ("delete", note_id)

Examples

Example 1
Inputops = [("set", 1, "a"), ("set", 2, "x"), ("set", 1, "b"), ("delete", 2)]Output[("set", 1, "b"), ("delete", 2)]WhyLast op for note 1 is set to "b"; last op for note 2 is delete. Note 1 first appears before note 2.
Example 2
Inputops = [("delete", 5), ("set", 5, "revive"), ("set", 6, "z"), ("set", 5, "final")]Output[("set", 5, "final"), ("set", 6, "z")]WhyNote 5’s last op is set to "final" (supersedes delete). Note 6 appears later and remains second.

Constraints

  • 1 <= len(ops) <= 2 * 10^5
  • 1 <= note_id <= 10^9
  • Each op is either ("set", note_id, value) or ("delete", note_id)
  • value is a non-empty string with length <= 100

Function Signature

def flush_offline_ops(ops: list[tuple]) -> list[tuple]:

Problem

Problem

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 note

When the device is offline, operations are appended to a local queue. When the device comes online, the app flushes the queue to the server.

Key offline requirement (deduplication)

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.

Task

Implement flush_offline_ops(ops) that returns the list of operations to send to the server after deduplication.

Deduplication rules

  1. For each note_id, keep only the last operation affecting that note_id.
  2. The output order is determined by the first time each note_id appears in the offline queue among those that survive.
    • In other words, if notes 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.

Examples

Example 1

  • Input: ops = [("set", 1, "a"), ("set", 2, "x"), ("set", 1, "b"), ("delete", 2)]
  • Output: [("set", 1, "b"), ("delete", 2)]
  • Explanation: For note 1, the last operation is set to "b". For note 2, the last operation is delete. Note 1 first appeared before note 2, so it stays first.

Example 2

  • Input: ops = [("delete", 5), ("set", 5, "revive"), ("set", 6, "z"), ("set", 5, "final")]
  • Output: [("set", 5, "final"), ("set", 6, "z")]
  • Explanation: Note 5’s last operation is set to "final" (delete is superseded). Note 5 first appeared before note 6.

Notes

  • Treat note_id as an integer identifier.
  • You do not need to validate operation semantics (e.g., deleting a non-existent note is allowed).

Constraints

  • 1 <= len(ops) <= 2 * 10^5
  • 1 <= note_id <= 10^9
  • value is a non-empty string of length <= 100
  • Each operation is either ("set", note_id, value) or ("delete", note_id)

Examples

Example 1
Inputops = [("set", 1, "a"), ("set", 2, "x"), ("set", 1, "b"), ("delete", 2)]Output[("set", 1, "b"), ("delete", 2)]WhyLast op for note 1 is set to "b"; last op for note 2 is delete. Note 1 first appears before note 2.
Example 2
Inputops = [("delete", 5), ("set", 5, "revive"), ("set", 6, "z"), ("set", 5, "final")]Output[("set", 5, "final"), ("set", 6, "z")]WhyNote 5’s last op is set to "final" (supersedes delete). Note 6 appears later and remains second.

Constraints

  • 1 <= len(ops) <= 2 * 10^5
  • 1 <= note_id <= 10^9
  • Each op is either ("set", note_id, value) or ("delete", note_id)
  • value is a non-empty string with length <= 100

Function Signature

def flush_offline_ops(ops: list[tuple]) -> list[tuple]:
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.
Up next
Offline-First Sync Conflict ResolverMediumZero-Downtime Deduplication at ScaleHardReal-Time Chat Event DeduplicatorMedium
Next question