Build a Real-Time Collaborative Todo List with Conflict Resolution
Create an advanced real-time collaborative todo list application backend function that merges concurrent updates from multiple users while resolving conflicts using operational transformation or CRDT principles in JavaScript.
Challenge prompt
You are tasked with creating a JavaScript function that takes multiple concurrent change logs from different users editing the same todo list and merges them into a consistent, conflict-free document state. Each change log contains additions, deletions, and updates to todo items, identified by unique IDs. Your function must apply operational transformations (OT) or Conflict-free Replicated Data Types (CRDTs) principles to ensure all changes are integrated without overwriting others improperly, maintaining the final todo list consistency regardless of merge order. Input is an array of change log objects. Your function should return the merged todo list array reflecting all users' changes correctly integrated. Design your solution to handle: - Insertions of new todo items with unique IDs - Updates to todo item content or completion status - Deletions of todo items - Resolution of simultaneous conflicting updates on the same todo item Provide the implementation of the mergeTodos(changeLogs) function that returns the final todo list array.
Guidance
- • Represent each todo item with a unique ID and track timestamps or version counters to resolve conflicting updates.
- • Implement a method to merge inserts, updates, and deletes that can occur concurrently, ensuring operation commutativity.
- • Consider using simple CRDT data structures like a Last-Write-Wins (LWW) element set or similar operational transformation logic.
- • Ensure your solution scales with multiple users and preserves all non-conflicting changes.
Hints
- • Use timestamps or logical clocks to compare conflicting operations applying the latest one.
- • Model deletions explicitly as tombstones or versioned flags rather than removing immediately to avoid losing updates.
- • Focus on designing idempotent and commutative merge steps so the final state is consistent regardless of merge order.
Starter code
function mergeTodos(changeLogs) {
// changeLogs is an array where each element is an array of todo item operations
// Each operation can be: { id, content, completed, opType: 'add'|'update'|'delete', timestamp }
// Implement your OT or CRDT logic here to merge all logs into one consistent todo list
// Example return format: [{ id, content, completed }]
return [];
}Expected output
[ { id: '1', content: 'Buy groceries', completed: false }, { id: '2', content: 'Fix bug in app', completed: true } ]
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.