0
0
Blockchain / Solidityprogramming~20 mins

Consensus mechanisms overview in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Consensus Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this simplified Proof of Work simulation?

This Python code simulates a simple Proof of Work by finding a nonce that makes the hash start with '00'. What will it print?

Blockchain / Solidity
import hashlib

def proof_of_work(data):
    nonce = 0
    while True:
        text = f'{data}{nonce}'
        hash_result = hashlib.sha256(text.encode()).hexdigest()
        if hash_result.startswith('00'):
            return nonce
        nonce += 1

nonce_found = proof_of_work('blockchain')
print(nonce_found)
AIt runs forever without finding a nonce
B0
C1024
D523
Attempts:
2 left
💡 Hint

Try running the code or understand that the nonce is the smallest number making the hash start with '00'.

🧠 Conceptual
intermediate
1:30remaining
Which consensus mechanism uses voting among a fixed set of validators?

In blockchain, which consensus mechanism relies on a fixed group of trusted validators voting to agree on the next block?

APractical Byzantine Fault Tolerance (PBFT)
BProof of Stake (PoS)
CProof of Work (PoW)
DProof of Authority (PoA)
Attempts:
2 left
💡 Hint

Think about consensus designed for permissioned blockchains with known validators.

Predict Output
advanced
2:00remaining
What error does this Proof of Stake validator selection code raise?

Consider this Python code that selects a validator weighted by stake. What error will it raise when run?

Blockchain / Solidity
import random

validators = {'Alice': 50, 'Bob': 30, 'Charlie': 20}

weights = list(validators.values())
selected = random.choices(list(validators.keys()), weights=weights, k=1)
print(selected[0])

# Now test with an empty validators dict
validators = {}
weights = list(validators.values())
selected = random.choices(list(validators.keys()), weights=weights, k=1)
print(selected[0])
AIndexError: list index out of range
BValueError: weights must be non-negative and not all zero
CTypeError: 'dict_keys' object is not subscriptable
DNo error, prints a random validator
Attempts:
2 left
💡 Hint

What happens if you try to pick weighted choices from an empty list?

🧠 Conceptual
advanced
1:30remaining
Which consensus mechanism is most energy efficient and uses token ownership to decide block creation?

Among these consensus methods, which one is known for low energy use and selects block creators based on how many tokens they own?

AProof of Stake (PoS)
BProof of Work (PoW)
CDelegated Proof of Stake (DPoS)
DProof of Authority (PoA)
Attempts:
2 left
💡 Hint

Think about the method that replaces mining with stake-based selection.

Predict Output
expert
2:30remaining
What is the length of the blockchain list after running this simplified block addition code?

This Python code simulates adding blocks with a simple consensus check. How many blocks will the blockchain contain at the end?

Blockchain / Solidity
blockchain = ['Genesis']

class Block:
    def __init__(self, data, prev_hash):
        self.data = data
        self.prev_hash = prev_hash

    def hash(self):
        return hash((self.data, self.prev_hash))

# Simulate adding blocks only if prev_hash matches last block's hash
for i in range(1, 6):
    last_block = blockchain[-1]
    last_hash = hash(last_block) if isinstance(last_block, str) else last_block.hash()
    new_block = Block(f'Data {i}', last_hash)
    # Consensus check: accept block only if prev_hash matches last_hash
    if new_block.prev_hash == last_hash:
        blockchain.append(new_block)

print(len(blockchain))
A5
B1
C6
D0
Attempts:
2 left
💡 Hint

Count the genesis block plus all accepted blocks.