Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Efficient data structures in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Blockchain Data Structures 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 example?
Consider the following Solidity code snippet. What will be the value of balances[0x123] after execution?
Blockchain / Solidity
pragma solidity ^0.8.0;

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

    function deposit() public {
        balances[msg.sender] += 100;
    }

    function test() public {
        deposit();
    }
}
A100
B0
CCompilation error
DUndefined
Attempts:
2 left
💡 Hint
Think about what happens when deposit() is called by the sender.
🧠 Conceptual
intermediate
1:30remaining
Which data structure is best for constant time membership checks in smart contracts?
In blockchain smart contracts, which data structure allows you to check if an element exists in constant time (O(1))?
AMapping
BArray
CLinked List
DBinary Tree
Attempts:
2 left
💡 Hint
Think about how mappings work in Solidity.
🔧 Debug
advanced
2:30remaining
Why does this Solidity code cause a runtime error?
Examine the following Solidity code. Why does calling getValue(5) cause a runtime error?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract Test {
    uint[] public values;

    constructor() {
        values.push(10);
        values.push(20);
    }

    function getValue(uint index) public view returns (uint) {
        return values[index];
    }
}
AType error because values is not an array
BCompilation error due to missing return statement
CIndex out of bounds error because index 5 does not exist
DNo error, returns 0
Attempts:
2 left
💡 Hint
Check the size of the array and the index used.
📝 Syntax
advanced
1:30remaining
Which option correctly declares a fixed-size byte array in Solidity?
Select the correct syntax to declare a fixed-size byte array of length 4 in Solidity.
Abyte[4] data;
Bfixed bytes data = 4;
Cbytes data[4];
Dbytes4 data;
Attempts:
2 left
💡 Hint
Remember Solidity has a special type for fixed-size byte arrays.
🚀 Application
expert
3:00remaining
How many items are stored after this Solidity struct array operation?
Given the following Solidity code, how many Person structs are stored in the people array after execution?
Blockchain / Solidity
pragma solidity ^0.8.0;

contract PeopleList {
    struct Person {
        string name;
        uint age;
    }

    Person[] public people;

    function addPeople() public {
        people.push(Person("Alice", 30));
        people.push(Person("Bob", 25));
        people.pop();
        people.push(Person("Charlie", 20));
    }
}
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
Remember what the pop() function does to the array.

Practice

(1/5)
1. Which data structure is best for quickly finding a user's balance by their blockchain address?
easy
A. Array
B. Mapping (key-value pairs)
C. Struct
D. Linked list

Solution

  1. Step 1: Understand the need for quick lookup

    We want to find a balance by address fast, so we need a structure that supports direct access by key.
  2. Step 2: Identify the best data structure

    Mappings provide key-value pairs allowing O(1) access by address, unlike arrays or structs which require searching.
  3. Final Answer:

    Mapping (key-value pairs) -> Option B
  4. Quick Check:

    Fast key-based lookup = Mapping [OK]
Hint: Use mappings for direct key lookups in blockchain [OK]
Common Mistakes:
  • Choosing arrays which require looping to find an address
  • Using structs alone without a key for lookup
  • Thinking linked lists are efficient for random access
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) balances;
C. mapping[address] uint balances;
D. mapping{address: uint} balances;

Solution

  1. Step 1: Recall Solidity mapping syntax

    Mappings use the syntax mapping(keyType => valueType) variableName;
  2. Step 2: Match the correct syntax

    mapping(address => uint) balances; matches this exactly: mapping(address => uint) balances;
  3. Final Answer:

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

    Correct mapping syntax uses '=>' [OK]
Hint: Remember mapping uses '=>' between key and value types [OK]
Common Mistakes:
  • Using commas instead of '=>' in mapping
  • Using square brackets or curly braces incorrectly
  • Omitting the semicolon at the end
3. What will be the output of this Solidity code snippet?
struct User { uint id; string name; }
User[] users;
users.push(User(1, "Alice"));
users.push(User(2, "Bob"));
string memory name = users[1].name;
medium
A. "Alice"
B. Empty string
C. Compilation error
D. "Bob"

Solution

  1. Step 1: Understand array indexing

    Arrays start at index 0, so users[0] is Alice, users[1] is Bob.
  2. Step 2: Identify the accessed element

    The code accesses users[1].name, which is "Bob".
  3. Final Answer:

    "Bob" -> Option D
  4. Quick Check:

    Index 1 in array = "Bob" [OK]
Hint: Remember arrays start at zero index [OK]
Common Mistakes:
  • Confusing index 1 with index 0
  • Assuming structs print as variable names
  • Expecting compilation error due to string usage
4. Identify the error in this Solidity code snippet:
mapping(address => uint) balances;
function addBalance(address user, uint amount) public {
balances[user] += amount;
}
medium
A. Cannot use '+=' on mapping values
B. Function lacks visibility modifier
C. No initialization needed for mapping values
D. Mapping keys must be uint, not address

Solution

  1. Step 1: Check mapping usage

    Mappings default to zero for uint values if key not set, so no initialization needed.
  2. Step 2: Verify function and operation

    Using '+=' on balances[user] is valid; function has public visibility.
  3. Final Answer:

    No initialization needed for mapping values -> Option C
  4. Quick Check:

    Mapping uint defaults to 0, so '+=' works [OK]
Hint: Mapping values default to zero, no init needed [OK]
Common Mistakes:
  • Thinking mapping values must be initialized before use
  • Confusing visibility modifiers
  • Assuming keys must be uint instead of address
5. You want to store user profiles with id, name, and balance, and quickly find a profile by id. Which data structure combination is most efficient in Solidity?
hard
A. Mapping from id to struct
B. Array of structs only
C. Struct with embedded array
D. Linked list of structs

Solution

  1. Step 1: Analyze the need for quick lookup by id

    Quick lookup by id requires direct access, which arrays or linked lists cannot provide efficiently.
  2. Step 2: Choose the best data structure

    Mapping from id to struct allows O(1) access to user profiles by id, combining grouping (struct) and fast lookup (mapping).
  3. Final Answer:

    Mapping from id to struct -> Option A
  4. Quick Check:

    Fast key access + grouped data = mapping to struct [OK]
Hint: Use mapping from id to struct for fast profile lookup [OK]
Common Mistakes:
  • Using arrays which require looping to find id
  • Using linked lists which are slow for random access
  • Embedding arrays inside structs without mapping