Balance checking in Blockchain / Solidity - Time & Space Complexity
When checking a balance on a blockchain, it is important to know how the time to get that balance changes as the number of accounts grows.
We want to understand how long it takes to find the balance for one account when many accounts exist.
Analyze the time complexity of the following code snippet.
function getBalance(user) {
for (let i = 0; i < accounts.length; i++) {
if (accounts[i].address === user) {
return accounts[i].balance;
}
}
return 0;
}
This code looks through a list of accounts to find the balance of the given user address.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A loop that checks each account's address one by one.
- How many times: Up to the total number of accounts in the list.
As the number of accounts grows, the time to find a balance grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the number of accounts.
Time Complexity: O(n)
This means the time to find a balance grows in a straight line with the number of accounts.
[X] Wrong: "Checking a balance always takes the same time no matter how many accounts there are."
[OK] Correct: Because this code looks through each account one by one, more accounts mean more checks and more time.
Understanding how balance checking scales helps you explain how blockchain systems handle many users efficiently.
"What if we stored accounts in a map (key-value store) instead of a list? How would the time complexity change?"
Practice
What does balance checking in blockchain primarily tell you?
Solution
Step 1: Understand the purpose of balance checking
Balance checking is used to find out the amount of cryptocurrency held by a blockchain address.Step 2: Differentiate from other blockchain data
Transaction history, private keys, and mining difficulty are different concepts unrelated to balance checking.Final Answer:
How much cryptocurrency an address currently holds -> Option CQuick Check:
Balance checking = cryptocurrency amount [OK]
- Confusing balance with transaction history
- Thinking balance reveals private keys
- Mixing balance with blockchain mining info
Which of the following is the correct way to call a function getBalance for an address addr in a blockchain library?
?
Solution
Step 1: Identify correct function call syntax
In most programming languages, calling a function with an argument uses parentheses: functionName(argument).Step 2: Check each option
getBalance(addr) uses correct syntax: getBalance(addr). Options A, C, and D use invalid or incorrect syntax.Final Answer:
getBalance(addr) -> Option AQuick Check:
Function call syntax = getBalance(addr) [OK]
- Using assignment instead of function call
- Using dot notation incorrectly for function calls
- Using arrow notation which is not valid here
Given this JavaScript code snippet using a blockchain API:
async function check() {
const balance = await blockchainAPI.getBalance('0x123');
console.log(balance);
}
check();What will be printed if the address has 500 tokens?
Solution
Step 1: Understand async/await usage
The function uses await to get the balance asynchronously, so balance will hold the returned value.Step 2: Check what is logged
console.log(balance) will print the balance value, which is 500 tokens for the given address.Final Answer:
500 -> Option BQuick Check:
Async balance fetch = 500 [OK]
- Ignoring async and expecting immediate value
- Assuming undefined if await is missing
- Confusing error messages with correct output
Find the error in this Python code that tries to get a balance:
def get_balance(address):
balance = blockchain.getBalance(address)
return balance
print(get_balance)Solution
Step 1: Check how get_balance is used in print
The code prints get_balance without parentheses, so it prints the function object, not the result.Step 2: Correct usage to call the function
Adding parentheses and an argument like print(get_balance('0xabc')) calls the function and prints the balance.Final Answer:
Missing parentheses when calling get_balance in print -> Option DQuick Check:
Function call needs () [OK]
- Printing function name instead of calling it
- Assuming function runs without parentheses
- Ignoring required function arguments
You want to check balances of multiple addresses and store them in a dictionary in Python. Which code correctly does this?
addresses = ['0x1', '0x2', '0x3']
balances = {}
for addr in addresses:
balances[addr] = blockchain.getBalance(addr)
print(balances)Solution
Step 1: Analyze the loop and dictionary usage
The for loop iterates over each address and assigns the balance to balances[addr], storing all balances.Step 2: Check syntax and function call
Syntax is correct with colon and indentation; getBalance is called properly with each address.Final Answer:
This code correctly stores balances for each address in a dictionary -> Option AQuick Check:
Loop + dict assignment = correct balances [OK]
- Forgetting colon after for statement
- Overwriting dictionary instead of adding keys
- Calling getBalance without argument
Ready to prove it?
Pass the test and this pattern moves unknown
