Staking mechanisms in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we look at staking mechanisms in blockchain, we want to know how the time to process grows as more users stake tokens.
We ask: How does the system handle more stakers efficiently?
Analyze the time complexity of the following code snippet.
function distributeRewards(stakers) {
let totalStake = 0;
for (let i = 0; i < stakers.length; i++) {
totalStake += stakers[i].amount;
}
for (let i = 0; i < stakers.length; i++) {
stakers[i].reward = (stakers[i].amount / totalStake) * 1000;
}
}
This code calculates the total tokens staked and then distributes rewards proportionally to each staker.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two separate loops over the stakers array.
- How many times: Each loop runs once over all stakers, so twice in total.
As the number of stakers grows, the code does more work by looping through all stakers twice.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (2 loops x 10) |
| 100 | About 200 operations (2 loops x 100) |
| 1000 | About 2000 operations (2 loops x 1000) |
Pattern observation: The operations grow roughly in direct proportion to the number of stakers.
Time Complexity: O(n)
This means the time to distribute rewards grows linearly as more people stake tokens.
[X] Wrong: "Because there are two loops, the time complexity is O(n²)."
[OK] Correct: The loops run one after another, not nested, so their times add up, not multiply.
Understanding how staking reward calculations scale helps you explain efficient blockchain designs clearly and confidently.
What if we combined the two loops into one? How would the time complexity change?
Practice
Solution
Step 1: Understand staking concept
Staking means locking tokens to support blockchain security.Step 2: Identify staking benefits
Users earn rewards for staking, helping network stability.Final Answer:
To help secure the network and earn rewards -> Option CQuick Check:
Staking = Security + Rewards [OK]
- Confusing staking with token transfer
- Thinking staking creates new tokens
- Believing staking deletes tokens
Solution
Step 1: Understand staking variables
Locked tokens represent the amount staked by user.Step 2: Match correct assignment
lockedTokens should equal stakeAmount to show tokens locked.Final Answer:
lockedTokens = stakeAmount -> Option DQuick Check:
Locked tokens = stake amount [OK]
- Adding stakeAmount to userBalance incorrectly
- Subtracting lockedTokens from userBalance wrongly
- Mixing variable roles in assignment
reward = stakedAmount * rewardRate * stakingDuration print(reward)If stakedAmount = 100, rewardRate = 0.05, and stakingDuration = 10, what is the output?
Solution
Step 1: Substitute values into formula
reward = 100 * 0.05 * 10Step 2: Calculate reward
100 * 0.05 = 5; then 5 * 10 = 50Final Answer:
50 -> Option AQuick Check:
100 * 0.05 * 10 = 50 [OK]
- Multiplying only two values
- Confusing rewardRate as 5 instead of 0.05
- Adding values instead of multiplying
function stakeTokens(userBalance, stakeAmount) {
if (stakeAmount > userBalance) {
return "Error: Not enough balance";
}
lockedTokens = stakeAmount;
userBalance = userBalance - stakeAmount;
return lockedTokens;
}Solution
Step 1: Check variable declarations
lockedTokens is assigned without declaration, causing error in strict languages.Step 2: Understand variable scope
lockedTokens should be declared (e.g., let or var) before use.Final Answer:
lockedTokens is not declared before assignment -> Option AQuick Check:
Undeclared variable causes error [OK]
- Ignoring variable declaration errors
- Misreading the if condition logic
- Thinking subtraction should be addition
Solution
Step 1: Understand reward calculation per user
Each user's reward depends on their own stake and duration.Step 2: Sum individual rewards for total
Calculate each reward separately, then add for total rewards.Final Answer:
Loop through each user, calculate reward = stakedAmount * rewardRate * duration, then sum all rewards -> Option BQuick Check:
Calculate per user, then sum [OK]
- Summing stakes before multiplying
- Ignoring individual durations
- Calculating reward for only one user
