Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Offline-First Sync Conflict Resolver

Medium
MediumCoding

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:

  1. Produces the final text for every note after merging.
  2. 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: str
  • text: str (the full new text after the edit)
  • device_id: str (who produced it)
  • seq: int (monotonically increasing per device_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:

  1. Deduplicate by causality: Any local op with seq <= server_seen[device_id] is already on the server and must not be uploaded.
  2. 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.

Return:

  • final_state: dict mapping note_id -> text after 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_state is the state before applying server_ops; you must apply server_ops as part of the merge.
  • You may assume server_ops are consistent with server_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]]:
Take this as a live interview session →

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Next questions
Offline-First Sync With ConflictsHardOffline-First Sync with Conflict ResolutionHardSuperhumanOffline-First Sync With Conflict ResolutionHard