0
0
Blockchain / Solidityprogramming~20 mins

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

Choose your learning style9 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.