Problem
You’re building an offline-first notes feature for a fintech super-app with 10M+ DAUs. Users can edit notes while offline (subway, airplane mode). When connectivity returns, the client must sync local edits with the server without corrupting data—incorrect merges can cause compliance issues (e.g., audit trails) and user trust loss.
Each note is a key-value pair: note_id -> text. Both the device and server produce operations while disconnected. Your job is to implement a deterministic conflict resolver that:
- Produces the final text for every note after merging.
- Outputs the minimal set of local operations that must be uploaded (i.e., operations not already reflected by the server).
Operation model
Each operation is a dict with fields:
note_id: strtext: str(the full new text after the edit)device_id: str(who produced it)seq: int(monotonically increasing perdevice_id)ts: int(client/server timestamp; may be skewed)
The server maintains a version vector server_seen[device_id] = max seq applied from that device.
Merge rules
For each note_id, consider all operations from both sides:
- Deduplicate by causality: Any local op with
seq <= server_seen[device_id]is already on the server and must not be uploaded. - Apply remaining ops in deterministic order to compute final text:
- Sort by
(ts ASC, device_id ASC, seq ASC). - Apply in that order; the last applied op for a note determines its final
text.
- Sort by
Return:
final_state: dict mappingnote_id -> textafter merging.to_upload: list of local operations (original dicts) that are not yet on the server, sorted by the same deterministic order.
Notes
- Timestamps can be skewed; ordering is still deterministic due to tie-breakers.
server_stateis the state before applyingserver_ops; you must applyserver_opsas part of the merge.- You may assume
server_opsare consistent withserver_seen(i.e., server has applied them).
Constraints
- 1 <= len(local_ops), len(server_ops) <= 2 * 10^5
- 1 <= number of distinct note_id <= 2 * 10^5
- 1 <= seq <= 10^9
- 0 <= ts <= 10^12
- device_id and note_id are non-empty strings
- seq is monotonically increasing per device_id within each side’s log
Function Signature
def resolve_offline_sync(server_state: dict[str, str], server_seen: dict[str, int], server_ops: list[dict], local_ops: list[dict]) -> tuple[dict[str, str], list[dict]]:
You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.
