iCloud Photo Metadata Sync with Vector Clock Conflict Resolution
Company: Apple
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a client-side photo metadata sync manager that resolves conflicts when several devices edit the same photo's metadata while offline.
Each device keeps a local copy of photo metadata: `title`, `tags` and `favorite` status. When devices sync, conflicts are resolved deterministically using vector clocks. If two edits are concurrent (neither vector clock dominates the other), a last-writer-wins strategy is applied using logical timestamps, with the lexicographically smaller device ID as the final tiebreaker.
The system must support registering devices, adding photos, editing metadata fields on individual devices, performing bidirectional sync between any two devices, and querying the current state of photo metadata or the vector clock for any field.
The clocks follow these rules, and the expected outputs depend on them:
- One logical clock is shared by the whole system. It starts at 0, and each `addPhoto` or `editPhoto` call increments it by 1. The new value is that write's timestamp `t`.
- `addPhoto` on device `d` gives each of the three fields the vector clock `{d: t}`. `editPhoto` on device `d` sets entry `d` of the edited field's vector clock to `t` and leaves the other entries unchanged.
- Clock X dominates clock Y when every entry of X is greater than or equal to the matching entry of Y and at least one is strictly greater. A device missing from a clock counts as 0.
- A sync resolves each photo that both devices hold one field at a time. Both devices receive the winning value, the winning write's timestamp, and the merged clock (the element-wise maximum of the two clocks). A photo that only one device holds is copied to the other device along with its clocks and timestamps.
Implement a class `PhotoSyncManager` with these methods:
- `addDevice(deviceId: str)`: register a new device in the sync system.
- `addPhoto(deviceId: str, photoId: str, title: str, tags: str, favorite: str)`: add a new photo with initial metadata (title, tags, favorite) on a specific device.
- `editPhoto(deviceId: str, photoId: str, field: str, value: str)`: edit one metadata field (`title`, `tags` or `favorite`) of a photo on a device.
- `syncDevices(deviceA: str, deviceB: str)`: perform a bidirectional sync between two devices, resolving conflicts with vector clocks and the last-writer-wins tiebreaker.
- `getPhoto(deviceId: str, photoId: str) -> str`: return the photo's current metadata on that device as `title=X,tags=Y,favorite=Z`, or `NOT_FOUND` if the device does not have the photo.
- `getConflictLog(deviceId: str, photoId: str, field: str) -> str`: return that field's vector clock on that device as semicolon-separated `deviceId:time` pairs sorted by device ID, `NOT_FOUND` if the device does not have the photo, or `EMPTY` if the field has no clock entries.
Example 1, a concurrent edit of the same field:
```text
addDevice("iphone")
addDevice("mac")
addPhoto("iphone", "p1", "beach", "summer", "false") # t = 1
syncDevices("iphone", "mac")
editPhoto("iphone", "p1", "title", "sunset") # t = 2
editPhoto("mac", "p1", "title", "dusk") # t = 3
syncDevices("iphone", "mac")
getPhoto("iphone", "p1") -> "title=dusk,tags=summer,favorite=false"
getConflictLog("mac", "p1", "title") -> "iphone:2;mac:3"
getConflictLog("mac", "p1", "tags") -> "iphone:1"
```
The first sync copies `p1` to the mac with clock `{iphone: 1}` on every field. After the two offline edits the title clocks are `{iphone: 2}` and `{iphone: 1, mac: 3}`. Each is ahead on one device, so the edits are concurrent and the later write (`t = 3`, from the mac) wins. Both devices end with the merged clock `{iphone: 2, mac: 3}`. The tags clock has not changed since the photo was created.
Example 2, a one-sided copy and causal updates:
```text
addDevice("a1")
addDevice("b2")
addPhoto("a1", "p1", "cat", "pets", "false") # t = 1
getPhoto("b2", "p1") -> "NOT_FOUND"
editPhoto("a1", "p1", "favorite", "true") # t = 2
syncDevices("b2", "a1")
getPhoto("b2", "p1") -> "title=cat,tags=pets,favorite=true"
editPhoto("b2", "p1", "tags", "indoor") # t = 3
editPhoto("a1", "p1", "title", "kitten") # t = 4
syncDevices("a1", "b2")
getPhoto("a1", "p1") -> "title=kitten,tags=indoor,favorite=true"
getConflictLog("b2", "p1", "tags") -> "a1:1;b2:3"
getConflictLog("a1", "p2", "title") -> "NOT_FOUND"
```
The first sync copies `p1` from `a1` to `b2`. In the second sync the title clock on `a1` (`{a1: 4}`) dominates the one on `b2` (`{a1: 1}`), so `kitten` wins, and the tags clock on `b2` (`{a1: 1, b2: 3}`) dominates the one on `a1` (`{a1: 1}`), so `indoor` wins. The `b2` entry is 3 because clock entries store the shared timestamp of the write.
```hint What to store per field
A sync needs more than the current value of a field. Think about what else each field must carry so that a later sync can tell whether one copy has already seen the other's edits, and which write produced the value.
```
```hint Comparing two clocks
The two clocks can list different devices. Compare over every device that appears in either one, and check what your dominance test returns when the clocks are equal.
```
### Constraints & Assumptions
- 1 to 10 devices.
- Photo IDs have 1 to 20 characters, and field values have 1 to 100 characters.
- Device IDs and photo IDs use lowercase letters and digits only.
- At most 10,000 operations in total.
- `field` is always `"title"`, `"tags"` or `"favorite"`, and `favorite` values are `"true"` or `"false"`.
- Every call names a registered device, and `editPhoto` only targets a photo that already exists on that device.
### Clarifying Questions to Ask
- What should `addPhoto` do when the photo ID already exists on that device?
- When can `getConflictLog` return `EMPTY`, given that `addPhoto` puts a clock on every field?
- Are photo deletions in scope, or only edits to the three fields?
- Can `syncDevices` be called with the same device on both sides?
### What a Strong Answer Covers
- A data model keyed by device, photo and field that keeps each field's value, vector clock and winning write stamp together.
- A dominance check that handles devices missing from one clock and treats equal clocks as non-dominating, plus the element-wise maximum merge.
- A sync that writes the same winner, merged clock and stamp to both devices, and copies one-sided photos as independent deep copies.
- The exact output formats, including sorted clock entries and the `NOT_FOUND` and `EMPTY` cases.
- The time and space cost of edits, syncs and queries.
### Follow-up Questions
- How would the design change if each device kept its own logical counter?
- How would you support deleting a photo so that a later sync does not bring it back?
- `tags` behaves like a set. How would you merge concurrent tag edits so that neither device's additions are lost?
- Vector clocks grow with every device that ever edited a field. How would you keep them bounded as devices are retired?
Overview: Implement a photo metadata sync manager where offline devices edit title, tags and favorite fields and then sync in pairs, resolving conflicts with per-field vector clocks and a last-writer-wins tiebreaker. It tests vector clock dominance and merging, deterministic conflict resolution, and careful low-level class design.