0
0
Blockchain / Solidityprogramming~10 mins

Efficient data structures in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a mapping in Solidity that links addresses to balances.

Blockchain / Solidity
mapping(address => uint) public [1];
Drag options to blanks, or click blank then click option'
Ausers
Bbalance
Caccounts
Dbalances
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular form like 'balance' which is less common for mappings.
Using unrelated names like 'users' which do not describe balances.
2fill in blank
medium

Complete the code to add a new element to a dynamic array in Solidity.

Blockchain / Solidity
uint[] public numbers;

function addNumber(uint num) public {
    numbers.[1](num);
}
Drag options to blanks, or click blank then click option'
Aappend
Binsert
Cpush
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'append' which is a Python method, not Solidity.
Using 'insert' which requires an index in Solidity.
3fill in blank
hard

Fix the error in the code to correctly check if a key exists in a mapping.

Blockchain / Solidity
mapping(address => uint) public balances;

function hasBalance(address user) public view returns (bool) {
    return balances[[1]] != 0;
}
Drag options to blanks, or click blank then click option'
Amsg.sender
Buser
Caddress
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using msg.sender which checks the caller, not the input address.
Using address which is a type, not a variable.
4fill in blank
hard

Fill both blanks to create a struct and declare a mapping from address to that struct.

Blockchain / Solidity
struct [1] {
    uint balance;
    uint lastUpdated;
}

mapping(address => [2]) public accounts;
Drag options to blanks, or click blank then click option'
AAccount
BUser
DBalance
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the struct and mapping type.
Using unrelated names like 'Balance' which is a field, not a struct.
5fill in blank
hard

Fill all three blanks to create a mapping with a nested mapping for token balances.

Blockchain / Solidity
mapping(address => mapping([1] => uint)) public [2];

function getBalance(address user, [3] token) public view returns (uint) {
    return [2][user][token];
}
Drag options to blanks, or click blank then click option'
Aaddress
Bbalances
Dtokens
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect key types like 'uint' for tokens.
Using inconsistent names for the mapping and function parameters.