Consensus Algorithms Comparison Interview Questions
Consensus algorithm comparison and general implementation interview questions.
Q1: Compare different consensus algorithms.
Answer:
Comparison Table:
| Algorithm | Type | Finality | Block Time | Energy | Fault Tolerance |
|---|---|---|---|---|---|
| Paxos | Classic | Immediate | N/A | Low | (n-1)/2 failures |
| BFT | Byzantine | Immediate | Fast | Low | f Byzantine (n=3f+1) |
| Tendermint | BFT | Immediate | 1-6s | Low | 1/3 Byzantine |
| Ouroboros | PoS | Probabilistic | 1s | Very Low | 51% stake attack |
| Bitcoin | PoW | Probabilistic | 10min | Very High | 51% hash power |
| Ethereum PoS | PoS | Checkpoint | 12s | Very Low | 1/3 stake attack |
| Polkadot | NPoS | Fast | 6s | Very Low | 1/3 stake attack |
| Solana | PoH + PoS | Probabilistic | 0.4s | Very Low | 1/3 stake attack |
Trade-offs:
Safety vs Liveness:
- Paxos/BFT/Tendermint: Strong safety, requires synchrony
- PoW/PoS: Probabilistic safety, works asynchronously
Finality:
- Immediate: Paxos, BFT, Tendermint
- Probabilistic: Bitcoin, Ouroboros, Solana
- Checkpoint: Ethereum PoS
- Fast: Polkadot (GRANDPA)
Energy Efficiency:
- Low: Paxos, BFT, Tendermint
- Very Low: All PoS algorithms
- Very High: PoW (Bitcoin)
Use Case Selection:
- Distributed Systems: Paxos, BFT
- Public Blockchains: PoW, PoS variants
- High Throughput: Tendermint, Polkadot, Solana
- Ultra-High Throughput: Solana (65K+ TPS)
- Formal Verification: Ouroboros
- Shared Security: Polkadot
Q2: How do you implement a simple consensus algorithm?
Answer:
Simple BFT Implementation:
1class SimpleBFT:
2 def __init__(self, node_id, total_nodes):
3 self.node_id = node_id
4 self.total_nodes = total_nodes
5 self.f = (total_nodes - 1) // 3 # Max Byzantine nodes
6 self.quorum = 2 * self.f + 1
7 self.is_primary = (node_id == 0)
8 self.log = {}
9 self.sequence = 0
10
11 def propose(self, request):
12 if not self.is_primary:
13 return None
14
15 self.sequence += 1
16 proposal = {
17 'sequence': self.sequence,
18 'request': request,
19 'prepares': {self.node_id},
20 'commits': set()
21 }
22 self.log[self.sequence] = proposal
23
24 # Broadcast pre-prepare
25 return {
26 'type': 'pre_prepare',
27 'sequence': self.sequence,
28 'request': request
29 }
30
31 def handle_pre_prepare(self, message):
32 if self.is_primary:
33 return None
34
35 sequence = message['sequence']
36 request = message['request']
37
38 # Validate
39 if not self.validate_request(request):
40 return None
41
42 # Store and broadcast prepare
43 self.log[sequence] = {
44 'request': request,
45 'prepares': {self.node_id},
46 'commits': set()
47 }
48
49 return {
50 'type': 'prepare',
51 'sequence': sequence,
52 'node_id': self.node_id
53 }
54
55 def handle_prepare(self, message):
56 sequence = message['sequence']
57 node_id = message['node_id']
58
59 if sequence not in self.log:
60 return None
61
62 self.log[sequence]['prepares'].add(node_id)
63
64 # Check if we have quorum
65 if len(self.log[sequence]['prepares']) >= self.quorum:
66 # Broadcast commit
67 return {
68 'type': 'commit',
69 'sequence': sequence,
70 'node_id': self.node_id
71 }
72
73 return None
74
75 def handle_commit(self, message):
76 sequence = message['sequence']
77 node_id = message['node_id']
78
79 if sequence not in self.log:
80 return None
81
82 self.log[sequence]['commits'].add(node_id)
83
84 # Check if we have quorum
85 if len(self.log[sequence]['commits']) >= self.quorum:
86 # Execute request
87 result = self.execute(self.log[sequence]['request'])
88 return {
89 'type': 'reply',
90 'sequence': sequence,
91 'result': result
92 }
93
94 return None
Q3: What are the security properties of consensus algorithms?
Answer:
Safety Properties:
- Agreement: All honest nodes agree on same value
- Validity: Only valid values are chosen
- Integrity: Values cannot be tampered with
Liveness Properties:
- Termination: Algorithm eventually terminates
- Progress: System continues to make progress
- Responsiveness: Responds within bounded time
Fault Tolerance:
- Crash Faults: Nodes stop responding
- Byzantine Faults: Nodes behave arbitrarily
- Network Faults: Messages delayed or lost
Attack Vectors:
- 51% Attack: Control majority of resources
- Sybil Attack: Create many fake identities
- Eclipse Attack: Isolate node from network
- Nothing-at-Stake: Vote on multiple chains (PoS)
- Long-Range Attack: Rewrite history (PoS)
Mitigation Strategies:
- Checkpointing: Lock in finalized blocks
- Slashing: Penalize malicious behavior
- Time Locks: Delay withdrawals
- Validator Rotation: Change validator set
- Finality Gadgets: Separate finality mechanism
Related Snippets
- Bitcoin (Nakamoto) Consensus Interview Questions
Bitcoin consensus algorithm interview questions covering Proof-of-Work (PoW) and … - Byzantine Fault Tolerance (BFT) Consensus Interview Questions
Byzantine Fault Tolerance (BFT) consensus algorithm interview questions covering … - Cardano Interview Questions - Easy
Easy-level Cardano interview questions covering blockchain basics, Plutus, and … - Cardano Interview Questions - Hard
Hard-level Cardano interview questions covering advanced optimization and formal … - Cardano Interview Questions - Medium
Medium-level Cardano interview questions covering advanced Plutus development … - Cosmos Chain Operations Interview Questions - Easy
Easy-level Cosmos chain operation interview questions covering chain operations, … - Cosmos Chain Operations Interview Questions - Hard
Hard-level Cosmos chain operation questions covering advanced algorithms, … - Cosmos Chain Operations Interview Questions - Medium
Medium-level Cosmos chain operation questions covering advanced chain … - Cosmos SDK Interview Questions - Easy
Easy-level Cosmos SDK interview questions covering chain code, SDK basics, and … - Cosmos SDK Interview Questions - Hard
Hard-level Cosmos SDK interview questions covering advanced SDK internals, … - Cosmos SDK Interview Questions - Medium
Medium-level Cosmos SDK interview questions covering advanced module … - Ethereum Proof-of-Stake Consensus Interview Questions
Ethereum Proof-of-Stake consensus algorithm interview questions covering Casper … - Ouroboros (Cardano) Consensus Interview Questions
Ouroboros consensus algorithm interview questions covering Cardano's … - Paxos Consensus Interview Questions
Paxos consensus algorithm interview questions covering the classic distributed … - Polkadot (NPoS) Consensus Interview Questions
Polkadot consensus algorithm interview questions covering Nominated … - Polkadot Interview Questions - Easy
Easy-level Polkadot interview questions covering blockchain basics, Substrate, … - Polkadot Interview Questions - Hard
Hard-level Polkadot interview questions covering advanced optimization and … - Polkadot Interview Questions - Medium
Medium-level Polkadot interview questions covering advanced Substrate … - Solana Interview Questions - Easy
Easy-level Solana interview questions covering blockchain basics, programs, and … - Solana Interview Questions - Hard
Hard-level Solana interview questions covering advanced optimization, security, … - Solana Interview Questions - Medium
Medium-level Solana interview questions covering advanced program development, … - Solana Proof of History Consensus Interview Questions
Solana consensus algorithm interview questions covering Proof of History (PoH) … - Tendermint Consensus Interview Questions
Tendermint consensus algorithm interview questions covering the Byzantine Fault … - Web3 Interview Questions - Easy
Easy-level Web3 interview questions covering blockchain fundamentals, Ethereum, … - Web3 Interview Questions - Hard
Hard-level Web3 interview questions covering MEV, zero-knowledge proofs, … - Web3 Interview Questions - Medium
Medium-level Web3 interview questions covering DeFi, advanced Solidity, …