Raft Consensus Simulator - Problem

Implement a Raft consensus protocol simulator that handles the three core components of the algorithm:

1. Leader Election: When nodes start or detect leader failure, they transition through states (Follower → Candidate → Leader) using randomized timeouts and majority voting.

2. Log Replication: The leader receives client requests, appends entries to its log, and replicates them to followers using AppendEntries RPCs with consistency checks.

3. Commit Handling: Once a majority of nodes have replicated an entry, the leader commits it and notifies followers to apply it to their state machines.

Your simulator should process a sequence of events ('election', 'append', 'commit') and return the final state of all nodes including their roles, terms, logs, and commit indices.

Input & Output

Example 1 — Basic Election and Log Replication
$ Input: numNodes = 3, events = [{"type":"election","nodeId":0},{"type":"append","entry":"cmd1"},{"type":"commit","index":0}]
Output: [{"id":0,"state":"leader","term":1,"log":["cmd1"],"commitIndex":0},{"id":1,"state":"follower","term":1,"log":["cmd1"],"commitIndex":0},{"id":2,"state":"follower","term":1,"log":["cmd1"],"commitIndex":0}]
💡 Note: Node 0 becomes leader, appends entry 'cmd1', replicates to all nodes, then commits when majority (all 3) have the entry.
Example 2 — Leadership Change
$ Input: numNodes = 3, events = [{"type":"election","nodeId":1},{"type":"append","entry":"cmd2"},{"type":"election","nodeId":2}]
Output: [{"id":0,"state":"follower","term":2,"log":["cmd2"],"commitIndex":-1},{"id":1,"state":"follower","term":2,"log":["cmd2"],"commitIndex":-1},{"id":2,"state":"leader","term":2,"log":["cmd2"],"commitIndex":-1}]
💡 Note: Node 1 becomes leader and adds entry, then Node 2 takes over leadership in a new term. All nodes maintain the log but entry isn't committed.
Example 3 — Multiple Operations
$ Input: numNodes = 5, events = [{"type":"election","nodeId":2},{"type":"append","entry":"op1"},{"type":"append","entry":"op2"},{"type":"commit","index":0},{"type":"commit","index":1}]
Output: [{"id":0,"state":"follower","term":1,"log":["op1","op2"],"commitIndex":1},{"id":1,"state":"follower","term":1,"log":["op1","op2"],"commitIndex":1},{"id":2,"state":"leader","term":1,"log":["op1","op2"],"commitIndex":1},{"id":3,"state":"follower","term":1,"log":["op1","op2"],"commitIndex":1},{"id":4,"state":"follower","term":1,"log":["op1","op2"],"commitIndex":1}]
💡 Note: Node 2 becomes leader, adds two operations, and both get committed since all 5 nodes replicate them (majority achieved).

Constraints

  • 1 ≤ numNodes ≤ 10
  • 1 ≤ events.length ≤ 50
  • Event types: 'election', 'append', 'commit'
  • Entry strings have length ≤ 20

Visualization

Tap to expand
INPUTALGORITHMRESULTNode 0LeaderNode 1FollowerNode 2Follower3 nodes, events:election(0), append(cmd1), commit(0)1Election ProcessNode 0 becomes leader (Term 1)2Log ReplicationLeader appends and replicates cmd13Majority CommitAll 3 nodes have entry, commit index 04State UpdateUpdate commit indices on all nodesNode 0Leader T:1Log:[cmd1] C:0Node 1Follower T:1Log:[cmd1] C:0Node 2Follower T:1Log:[cmd1] C:0Consensus AchievedAll nodes consistentKey Insight:Raft consensus ensures distributed systems maintain consistent state throughleader election, systematic log replication, and majority-based commit decisions.TutorialsPoint - Raft Consensus Simulator | Distributed Systems Protocol
Asked in
Google 15 Amazon 12 Microsoft 10 Meta 8
8.9K Views
Medium Frequency
~45 min Avg. Time
234 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen