Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Staking mechanisms in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Staking 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 staking reward calculation?

Consider a simple staking contract where rewards are calculated as 5% of the staked amount per epoch. What will be the total reward after 3 epochs if a user stakes 100 tokens?

Blockchain / Solidity
def calculate_rewards(staked_amount, epochs):
    reward_rate = 0.05
    total_reward = 0
    for _ in range(epochs):
        total_reward += staked_amount * reward_rate
    return total_reward

print(calculate_rewards(100, 3))
A15
B15.0
C10.5
D5
Attempts:
2 left
💡 Hint

Multiply the staked amount by the reward rate for each epoch and sum it up.

🧠 Conceptual
intermediate
1:30remaining
Which staking mechanism prevents double-spending by locking tokens?

In blockchain staking, which mechanism ensures tokens cannot be used for other transactions while staked?

ADelegation
BSlashing
CToken Locking
DYield Farming
Attempts:
2 left
💡 Hint

Think about what stops tokens from moving during staking.

🔧 Debug
advanced
2:00remaining
Identify the error in this staking reward function

The function below is intended to calculate compounded staking rewards per epoch. What error will it cause when run?

Blockchain / Solidity
def compounded_rewards(staked_amount, epochs, rate):
    total = staked_amount
    for _ in range(epochs):
        total = total + total * rate
    return total

print(compounded_rewards(100, 3, 0.05))
ATypeError because of wrong operator
BOutputs 115.7625 but should be 115.7625
CSyntaxError due to missing colon
DOutputs 115.7625
Attempts:
2 left
💡 Hint

Check if the formula correctly compounds the reward.

Predict Output
advanced
2:00remaining
What is the output of this delegation mapping code?

This code maps delegators to validators and sums total delegated tokens per validator. What is the output?

Blockchain / Solidity
delegations = [
    {'delegator': 'Alice', 'validator': 'Val1', 'amount': 50},
    {'delegator': 'Bob', 'validator': 'Val2', 'amount': 30},
    {'delegator': 'Charlie', 'validator': 'Val1', 'amount': 20},
    {'delegator': 'Dave', 'validator': 'Val3', 'amount': 40}
]

validator_totals = {}
for d in delegations:
    validator_totals[d['validator']] = validator_totals.get(d['validator'], 0) + d['amount']

print(validator_totals)
A{'Val1': 70, 'Val2': 30, 'Val3': 40}
B{'Val1': 50, 'Val2': 30, 'Val3': 40}
C{'Val1': 20, 'Val2': 30, 'Val3': 40}
D{'Val1': 70, 'Val2': 40, 'Val3': 30}
Attempts:
2 left
💡 Hint

Sum amounts for each validator key.

🧠 Conceptual
expert
1:30remaining
Which staking mechanism best balances security and liquidity?

Among these staking mechanisms, which one allows users to earn rewards while maintaining token liquidity?

ALiquid Staking
BCold Staking
CSlashing Mechanism
DLock-up Period Staking
Attempts:
2 left
💡 Hint

Think about staking that lets you trade or use tokens while still earning rewards.

Practice

(1/5)
1. What is the main purpose of staking tokens in a blockchain network?
easy
A. To create new tokens instantly
B. To transfer tokens to another user
C. To help secure the network and earn rewards
D. To delete tokens from the blockchain

Solution

  1. Step 1: Understand staking concept

    Staking means locking tokens to support blockchain security.
  2. Step 2: Identify staking benefits

    Users earn rewards for staking, helping network stability.
  3. Final Answer:

    To help secure the network and earn rewards -> Option C
  4. Quick Check:

    Staking = Security + Rewards [OK]
Hint: Staking locks tokens to secure network and gain rewards [OK]
Common Mistakes:
  • Confusing staking with token transfer
  • Thinking staking creates new tokens
  • Believing staking deletes tokens
2. Which of the following is the correct way to represent staking tokens in a smart contract pseudocode?
easy
A. stakeAmount = userBalance - lockedTokens
B. lockedTokens = stakeAmount + userBalance
C. userBalance = stakeAmount + lockedTokens
D. lockedTokens = stakeAmount

Solution

  1. Step 1: Understand staking variables

    Locked tokens represent the amount staked by user.
  2. Step 2: Match correct assignment

    lockedTokens should equal stakeAmount to show tokens locked.
  3. Final Answer:

    lockedTokens = stakeAmount -> Option D
  4. Quick Check:

    Locked tokens = stake amount [OK]
Hint: Locked tokens equal the stake amount directly [OK]
Common Mistakes:
  • Adding stakeAmount to userBalance incorrectly
  • Subtracting lockedTokens from userBalance wrongly
  • Mixing variable roles in assignment
3. Consider this pseudocode for calculating staking rewards:
reward = stakedAmount * rewardRate * stakingDuration
print(reward)
If stakedAmount = 100, rewardRate = 0.05, and stakingDuration = 10, what is the output?
medium
A. 50
B. 5
C. 0.5
D. 500

Solution

  1. Step 1: Substitute values into formula

    reward = 100 * 0.05 * 10
  2. Step 2: Calculate reward

    100 * 0.05 = 5; then 5 * 10 = 50
  3. Final Answer:

    50 -> Option A
  4. Quick Check:

    100 * 0.05 * 10 = 50 [OK]
Hint: Multiply all values stepwise: amount * rate * duration [OK]
Common Mistakes:
  • Multiplying only two values
  • Confusing rewardRate as 5 instead of 0.05
  • Adding values instead of multiplying
4. The following pseudocode has an error. What is the problem?
function stakeTokens(userBalance, stakeAmount) {
  if (stakeAmount > userBalance) {
    return "Error: Not enough balance";
  }
  lockedTokens = stakeAmount;
  userBalance = userBalance - stakeAmount;
  return lockedTokens;
}
medium
A. lockedTokens is not declared before assignment
B. The function does not return userBalance
C. The if condition should be stakeAmount < userBalance
D. The subtraction should add stakeAmount instead

Solution

  1. Step 1: Check variable declarations

    lockedTokens is assigned without declaration, causing error in strict languages.
  2. Step 2: Understand variable scope

    lockedTokens should be declared (e.g., let or var) before use.
  3. Final Answer:

    lockedTokens is not declared before assignment -> Option A
  4. Quick Check:

    Undeclared variable causes error [OK]
Hint: Always declare variables before assigning [OK]
Common Mistakes:
  • Ignoring variable declaration errors
  • Misreading the if condition logic
  • Thinking subtraction should be addition
5. You want to write a function that calculates total rewards for multiple users staking different amounts for different durations. Which approach correctly applies staking mechanisms?
hard
A. Add all staked amounts first, then multiply by rewardRate and total duration
B. Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards
C. Calculate reward only for the user with the highest stake
D. Multiply rewardRate by duration only once, ignoring staked amounts

Solution

  1. Step 1: Understand reward calculation per user

    Each user's reward depends on their own stake and duration.
  2. Step 2: Sum individual rewards for total

    Calculate each reward separately, then add for total rewards.
  3. Final Answer:

    Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards -> Option B
  4. Quick Check:

    Calculate per user, then sum [OK]
Hint: Calculate rewards individually, then add for total [OK]
Common Mistakes:
  • Summing stakes before multiplying
  • Ignoring individual durations
  • Calculating reward for only one user