Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Mappings in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Mapping Master
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 Solidity mapping access?

Consider the following Solidity code snippet:

pragma solidity ^0.8.0;

contract Test {
    mapping(address => uint) public balances;

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

    function getBalance(address user) public view returns (uint) {
        return balances[user];
    }
}

If no balance has been set for address 0x1234..., what will getBalance(0x1234...) return?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    mapping(address => uint) public balances;

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

    function getBalance(address user) public view returns (uint) {
        return balances[user];
    }
}
AReturns a random value
BThrows an error because the key does not exist
C0
DReturns the maximum uint value
Attempts:
2 left
💡 Hint

Think about what default values Solidity returns for mappings when a key is not set.

🧠 Conceptual
intermediate
1:30remaining
Why can't you iterate over a Solidity mapping?

Mappings in Solidity do not support iteration. Why is this the case?

ABecause mappings do not store keys, only values
BBecause Solidity does not allow loops inside contracts
CBecause mappings are only temporary and get deleted after function execution
DBecause mappings are stored in a way that does not keep track of keys, making iteration impossible
Attempts:
2 left
💡 Hint

Think about how mappings are stored in the Ethereum storage.

🔧 Debug
advanced
2:00remaining
What error does this Solidity code raise?

Examine this Solidity code snippet:

pragma solidity ^0.8.0;

contract Example {
    mapping(uint => string) public data;

    function addData(uint key, string memory value) public {
        data[key] = value;
    }

    function getData(uint key) public view returns (string memory) {
        return data[key];
    }

    function deleteData(uint key) public {
        delete data[key];
    }

    function test() public {
        string memory val = data[1];
        val = "new value";
    }
}

What error will occur when compiling or running this contract?

ANo error, the code compiles and runs fine
BTypeError: Cannot assign to a memory variable that is not a storage pointer
CCompilation error: Mappings cannot have string as value type
DRuntime error: Cannot delete mapping elements
Attempts:
2 left
💡 Hint

Check if the operations on mapping and string memory variables are valid.

❓ Predict Output
advanced
1:30remaining
What is the value of the mapping after these operations?

Given this Solidity contract snippet:

pragma solidity ^0.8.0;

contract Counter {
    mapping(address => uint) public counts;

    function increment() public {
        counts[msg.sender]++;
    }

    function reset(address user) public {
        counts[user] = 0;
    }
}

If msg.sender calls increment() three times, then reset(msg.sender) once, what is the value of counts[msg.sender]?

A3
B0
C1
DUndefined
Attempts:
2 left
💡 Hint

Think about what the reset function does to the mapping value.

📝 Syntax
expert
2:00remaining
Which option will cause a compilation error in this mapping declaration?

Consider these Solidity mapping declarations. Which one will cause a compilation error?

Amapping(uint => function()) public callbacks;
Bmapping(address => uint) public balances;
Cmapping(bytes32 => uint) public nameToAge;
Dmapping(uint => mapping(address => bool)) public approvals;
Attempts:
2 left
💡 Hint

Think about what types are allowed as mapping values in Solidity.

Practice

(1/5)
1. What is the primary purpose of mapping in blockchain smart contracts?
easy
A. To create user interfaces
B. To store a list of all keys used
C. To perform complex mathematical calculations
D. To connect keys to values for fast data lookup

Solution

  1. Step 1: Understand the role of mapping

    Mappings are used to link a key to a value, allowing quick access to data based on the key.
  2. Step 2: Compare options

    Only To connect keys to values for fast data lookup correctly describes this purpose; others describe unrelated functions.
  3. Final Answer:

    To connect keys to values for fast data lookup -> Option D
  4. Quick Check:

    Mapping = key-value fast lookup [OK]
Hint: Mappings link keys to values quickly [OK]
Common Mistakes:
  • Thinking mappings store all keys explicitly
  • Confusing mappings with arrays or lists
  • Assuming mappings perform calculations
2. Which of the following is the correct syntax to declare a mapping from address to uint in Solidity?
easy
A. mapping(address, uint) balances;
B. mapping(address => uint) public balances;
C. mapping[address => uint] balances;
D. mapping(address => uint) balances()

Solution

  1. Step 1: Recall Solidity mapping syntax

    The correct syntax uses parentheses with key type, arrow, and value type: mapping(keyType => valueType) visibility name;
  2. Step 2: Check each option

    mapping(address => uint) public balances; matches the correct syntax. Options A, C, and D have syntax errors like using commas instead of =>, wrong brackets, or function-like declaration.
  3. Final Answer:

    mapping(address => uint) public balances; -> Option B
  4. Quick Check:

    Correct mapping syntax uses (key => value) [OK]
Hint: Use parentheses and arrow for mapping syntax [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Missing visibility keyword like public
  • Using commas instead of =>
3. Consider the Solidity code:
mapping(address => uint) public balances;

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

function getBalance(address user) public view returns (uint) {
  return balances[user];
}

// After calling setBalance(0x1234..., 50), what does getBalance(0x1234...) return?
medium
A. 50
B. Error
C. Undefined
D. 0

Solution

  1. Step 1: Understand setBalance effect

    The function sets the balance of the given address to the specified amount, here 50.
  2. Step 2: Understand getBalance output

    Calling getBalance with the same address returns the stored value, which is 50.
  3. Final Answer:

    50 -> Option A
  4. Quick Check:

    Set then get returns stored value [OK]
Hint: Set value then get returns that value [OK]
Common Mistakes:
  • Assuming default zero is returned after setting
  • Thinking mapping keys are enumerable
  • Expecting an error for unset keys
4. What is wrong with this Solidity code snippet?
mapping(address => uint) balances;

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

function getBalance(address user) public view returns (uint) {
  return balances[user];
}
medium
A. The mapping is missing initialization before use
B. The mapping keys are not stored, so balances[user] always returns zero
C. There is no error; the code works as expected
D. The mapping is not declared public, so balances cannot be read

Solution

  1. Step 1: Check mapping declaration

    Mapping does not need explicit initialization; default values are zero.
  2. Step 2: Check function logic

    addBalance adds amount to the current balance; getBalance returns it. Not declaring public means no automatic getter, but manual getter exists.
  3. Final Answer:

    There is no error; the code works as expected -> Option C
  4. Quick Check:

    Mapping defaults and manual getter work fine [OK]
Hint: Manual getter works even if mapping not public [OK]
Common Mistakes:
  • Assuming mapping must be public to read values
  • Thinking mappings need explicit initialization
  • Confusing mapping keys storage with values
5. You want to track ownership of unique items by users using mappings in Solidity. Which approach correctly allows you to check if a user owns a specific item ID with a direct lookup starting from the user address?
hard
A. mapping(address => mapping(uint => bool)) ownsItem; // nested mapping user to item ID to ownership
B. mapping(uint => address) itemToOwner; // maps item ID to owner address
C. mapping(address => uint) ownerToItem; // stores one item per user
D. mapping(address => uint[]) userItems; // mapping to array of item IDs

Solution

  1. Step 1: Understand ownership tracking needs

    We need to check if a user owns a specific item ID, so a direct boolean check is best.
  2. Step 2: Analyze options

    Only mapping(address => mapping(uint => bool)) ownsItem; // nested mapping user to item ID to ownership provides a nested mapping(address => mapping(uint => bool)) ownsItem, allowing direct ownsItem[user][itemID] lookup from the user address. A limits to one item per user; B requires starting from item ID; D requires searching an array.
  3. Final Answer:

    mapping(address => mapping(uint => bool)) ownsItem; // nested mapping user to item ID to ownership -> Option A
  4. Quick Check:

    Nested mapping allows direct ownership check [OK]
Hint: Use nested mapping for user-item ownership check [OK]
Common Mistakes:
  • Using single mapping limiting one item per user
  • Using arrays which are not mappings
  • Confusing item-to-owner with user-to-item ownership check
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard