Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Gas optimization with storage in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Gas Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Gas cost difference between storage and memory

Consider the following Solidity function that copies an array from storage to memory and modifies it. What is the expected gas cost behavior when modifying the storage array directly versus modifying the memory copy?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract GasTest {
    uint256[] public data;

    constructor() {
        data.push(1);
        data.push(2);
        data.push(3);
    }

    function modifyStorage() public {
        data[0] = 10;
    }

    function modifyMemory() public view returns (uint256[] memory) {
        uint256[] memory temp = data;
        temp[0] = 10;
        return temp;
    }
}
AmodifyStorage uses more gas because it writes to storage; modifyMemory uses less gas as it only modifies memory.
BmodifyMemory uses more gas because copying to memory is expensive; modifyStorage uses less gas as it modifies storage directly.
CBoth functions use the same amount of gas because they modify the same data values.
DmodifyStorage uses less gas because storage writes are cheaper than memory writes.
Attempts:
2 left
💡 Hint

Think about the cost difference between writing to storage and writing to memory in Solidity.

🧠 Conceptual
intermediate
1:30remaining
Why pack variables to save gas?

In Solidity, why does packing multiple smaller variables into a single storage slot reduce gas costs?

ABecause packed variables execute faster in the EVM bytecode, reducing gas.
BBecause each storage slot costs gas, packing reduces the number of slots used, lowering gas consumption.
CBecause packing variables increases the contract size, which reduces gas costs.
DBecause packed variables are stored off-chain, saving gas.
Attempts:
2 left
💡 Hint

Think about how storage slots are charged in Solidity.

🔧 Debug
advanced
2:00remaining
Identify the gas inefficiency in this contract

Look at the following Solidity contract. Which line causes unnecessary gas usage due to inefficient storage access?

Blockchain / Solidity
pragma solidity ^0.8.0;

contract Inefficient {
    uint256 public count;

    function increment() public {
        count = count + 1;
        count = count + 1;
    }
}
AUsing 'count++' would cause more gas usage than 'count = count + 1;'.
BThe contract lacks a constructor, causing gas inefficiency.
CThe function should be marked view to save gas.
DLine 'count = count + 1;' repeated twice causes two storage writes, increasing gas.
Attempts:
2 left
💡 Hint

Consider how many times the storage variable is written to.

📝 Syntax
advanced
1:30remaining
Which code snippet correctly uses 'unchecked' to save gas?

In Solidity, using 'unchecked' can save gas by skipping overflow checks. Which snippet correctly applies 'unchecked' to increment a storage variable?

Aunchecked { count += 1; }
Bcount += unchecked(1);
Cunchecked count += 1;
Dcount = unchecked(count + 1);
Attempts:
2 left
💡 Hint

Remember the syntax for the 'unchecked' block in Solidity.

🚀 Application
expert
3:00remaining
Optimize storage usage in a struct array

You have a struct with multiple small variables stored in an array. How can you optimize gas usage when updating one field of a struct in storage?

ADelete the struct from storage and recreate it with updated values.
BUpdate the field directly in storage multiple times to ensure correctness.
CLoad the struct into memory, update the field, then write the entire struct back to storage once.
DUse a mapping instead of an array to avoid storage costs.
Attempts:
2 left
💡 Hint

Think about minimizing the number of storage writes.

Practice

(1/5)
1. Why is it important to optimize storage usage in blockchain smart contracts?
easy
A. Because storage is unlimited and free to use
B. Because storage variables are faster to access than memory variables
C. Because storage operations consume more gas, increasing transaction costs
D. Because storage variables do not affect gas costs

Solution

  1. Step 1: Understand gas cost of storage

    Storage operations in blockchain smart contracts consume significant gas compared to memory or stack operations.
  2. Step 2: Impact on transaction cost

    Higher gas consumption means higher transaction fees for users interacting with the contract.
  3. Final Answer:

    Because storage operations consume more gas, increasing transaction costs -> Option C
  4. Quick Check:

    Storage gas cost = higher fees [OK]
Hint: Remember: storage costs gas, so optimize to save money [OK]
Common Mistakes:
  • Thinking storage is free
  • Confusing storage with memory gas costs
  • Assuming storage is faster than memory
2. Which of the following Solidity declarations helps reduce gas by optimizing storage usage?
easy
A. uint8 public smallNumber; uint8 public anotherSmallNumber;
B. constant uint256 public MAX_VALUE = 1000;
C. uint256 public data;
D. string public name;

Solution

  1. Step 1: Identify constant usage

    Declaring variables as constant stores their value in bytecode, not storage, saving gas.
  2. Step 2: Compare with other declarations

    Other options use storage variables which consume gas on read/write, unlike constants.
  3. Final Answer:

    constant uint256 public MAX_VALUE = 1000; -> Option B
  4. Quick Check:

    Constants reduce storage gas [OK]
Hint: Use constants to save storage gas costs [OK]
Common Mistakes:
  • Assuming all variables cost the same gas
  • Not knowing constants are stored differently
  • Ignoring variable size impact
3. Consider this Solidity snippet:
contract Test {
    uint8 a = 1;
    uint8 b = 2;
    uint16 c = 3;
}

How many storage slots does this contract use?
medium
A. 1 storage slot
B. 2 storage slots
C. 3 storage slots
D. 4 storage slots

Solution

  1. Step 1: Understand storage slot packing

    Solidity packs variables into 32-byte slots if their sizes fit together.
  2. Step 2: Calculate combined size

    uint8 + uint8 + uint16 = 1 + 1 + 2 = 4 bytes, which fits into one 32-byte slot.
  3. Final Answer:

    1 storage slot -> Option A
  4. Quick Check:

    Small types packed = 1 slot [OK]
Hint: Pack small variables to fit one storage slot [OK]
Common Mistakes:
  • Assuming each variable uses one slot
  • Ignoring variable size packing
  • Confusing bytes with slots
4. Identify the gas optimization mistake in this Solidity code:
contract Example {
    uint256 public count;
    function increment() public {
        count = count + 1;
    }
}
medium
A. Using uint256 instead of uint8 for small counters
B. Not marking count as immutable
C. Using count = count + 1; instead of ++count;
D. Not using constant for count

Solution

  1. Step 1: Analyze variable type choice

    Using uint256 for a small counter wastes storage space and gas.
  2. Step 2: Suggest smaller type

    Using uint8 or smaller type packs better and reduces gas cost.
  3. Final Answer:

    Using uint256 instead of uint8 for small counters -> Option A
  4. Quick Check:

    Smaller types save gas [OK]
Hint: Use smallest uint type needed for counters [OK]
Common Mistakes:
  • Thinking increment syntax affects gas
  • Confusing constant and immutable usage
  • Ignoring variable size impact
5. You want to store multiple boolean flags in a contract efficiently. Which approach optimizes gas usage best?
hard
A. Use separate bool variables for each flag
B. Use multiple uint256 variables for each flag
C. Use an array of bool variables
D. Use a single uint8 variable and bitwise operations to store flags

Solution

  1. Step 1: Understand storage cost of booleans

    Each bool uses a full storage slot if declared separately, wasting gas.
  2. Step 2: Use bit packing with uint8

    Storing multiple flags as bits in a single uint8 packs them efficiently, reducing storage slots and gas.
  3. Step 3: Compare other options

    Arrays and multiple uint256 variables consume more storage and gas.
  4. Final Answer:

    Use a single uint8 variable and bitwise operations to store flags -> Option D
  5. Quick Check:

    Bit packing booleans saves gas [OK]
Hint: Pack booleans into bits using uint types [OK]
Common Mistakes:
  • Using separate bools wastes storage
  • Assuming arrays are cheaper
  • Ignoring bitwise operations benefits
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