0
0
Blockchain / Solidityprogramming~10 mins

Efficient data structures in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Efficient data structures
Start
Choose data structure
Check operation needs
Fast lookup?
YesUse Hash Map
Use Linked List
Fast insertion?
YesUse Linked List
No
Use Array
Implement in blockchain
Test performance
End
This flow shows how to pick and use efficient data structures in blockchain by checking operation needs like lookup and insertion speed.
Execution Sample
Blockchain / Solidity
mapping(address => uint) balances;

function updateBalance(address user, uint amount) public {
  balances[user] = amount;
}

function getBalance(address user) public view returns (uint) {
  return balances[user];
}
This Solidity code uses a mapping (hash map) to efficiently store and retrieve user balances by address.
Execution Table
StepActionData Structure StateOperationResult
1Initialize empty mappingbalances = {}N/AEmpty mapping created
2Call updateBalance(user1, 100)balances = {user1: 100}Insert/updateBalance for user1 set to 100
3Call updateBalance(user2, 50)balances = {user1: 100, user2: 50}Insert/updateBalance for user2 set to 50
4Call getBalance(user1)balances unchangedLookupReturns 100
5Call getBalance(user3)balances unchangedLookupReturns 0 (default)
💡 No more operations; mapping stores balances efficiently with O(1) lookup.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
balances{}{user1: 100}{user1: 100, user2: 50}{user1: 100, user2: 50}{user1: 100, user2: 50}
return value (getBalance user1)N/AN/AN/A100100
return value (getBalance user3)N/AN/AN/AN/A0
Key Moments - 2 Insights
Why does getBalance(user3) return 0 even though user3 was never added?
In the execution_table row 5, the mapping returns 0 by default for keys not present, which is Solidity's default behavior for uint values.
How does the mapping allow fast lookup compared to an array?
As shown in steps 4 and 5, the mapping provides O(1) lookup time by directly accessing the value via the key, unlike arrays which require searching.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the state of balances?
A{user1: 100, user2: 50}
B{user1: 50, user2: 100}
C{}
D{user1: 100}
💡 Hint
Check the 'Data Structure State' column at step 3 in the execution_table.
At which step does the mapping first contain two users?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Data Structure State' column to see when two keys appear.
If updateBalance(user1, 200) is called after step 3, what will balances[user1] be?
A100
B200
C50
D0
💡 Hint
Updating a key in mapping overwrites the previous value, see step 2 and 3 for similar insert/update.
Concept Snapshot
Efficient data structures in blockchain:
- Use mappings for fast key-value lookup (O(1))
- Use arrays or linked lists for ordered data
- Choose based on operation needs: lookup, insertion, iteration
- Mappings return default values for missing keys
- Efficient structures save gas and improve performance
Full Transcript
This visual execution shows how efficient data structures like mappings work in blockchain programming. We start with an empty mapping and add user balances. Each update inserts or updates a key-value pair. Lookups return stored values or default zero if missing. This approach provides fast access and efficient storage, important for blockchain smart contracts. The execution table tracks each step, showing how balances change and how lookups behave. Key moments clarify default values and lookup speed. The quiz tests understanding of mapping state and updates. The snapshot summarizes key points for choosing and using efficient data structures in blockchain.