0
0
Blockchain / Solidityprogramming~20 mins

Packing variables for gas savings in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Gas Packing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the gas cost difference when packing variables?

Consider the following two Solidity structs. Which one will generally consume less gas when stored on-chain?

struct A {
    uint256 a;
    uint256 b;
    bool c;
}

struct B {
    uint128 a;
    uint128 b;
    bool c;
}
AStruct A uses less gas because uint256 variables are native size and faster to access.
BBoth structs use the same gas because bool is stored separately regardless of packing.
CStruct A uses less gas because bool variables increase storage slots in struct B.
DStruct B uses less gas because smaller variables are packed into fewer storage slots.
Attempts:
2 left
💡 Hint

Think about how Solidity packs variables into 32-byte storage slots.

Predict Output
intermediate
2:00remaining
How many storage slots does this struct use?

Given this Solidity struct, how many 32-byte storage slots will it occupy?

struct Data {
    uint8 a;
    uint16 b;
    uint256 c;
    bool d;
}
A1 storage slot
B2 storage slots
C3 storage slots
D4 storage slots
Attempts:
2 left
💡 Hint

Remember that Solidity packs variables tightly if they fit in the same 32-byte slot.

🔧 Debug
advanced
2:00remaining
Why does this packed struct cause unexpected gas usage?

Analyze the following Solidity code snippet. Why does the gas cost remain high despite packing variables?

struct Packed {
    uint128 a;
    uint256 b;
    uint128 c;
}
ABecause uint256 b breaks the packing, causing each uint128 to use separate slots.
BBecause uint128 variables cannot be packed with uint256 variables in the same struct.
CBecause bool variables are missing, packing is ineffective.
DBecause the compiler ignores packing for structs with mixed variable sizes.
Attempts:
2 left
💡 Hint

Consider how storage slots are allocated in order.

🧠 Conceptual
advanced
2:00remaining
What is the best order to pack these variables for minimal gas?

You have these variables to store in a struct: uint64 x;, bool y;, uint256 z;, uint32 w;. What order should you declare them to minimize gas?

ADeclare <code>x</code>, <code>w</code>, <code>y</code> first, then <code>z</code> last.
BDeclare <code>z</code> first, then <code>x</code>, <code>w</code>, and <code>y</code> last.
CDeclare <code>y</code>, <code>z</code>, <code>x</code>, <code>w</code> in that order.
DDeclare <code>w</code>, <code>x</code>, <code>z</code>, <code>y</code> in that order.
Attempts:
2 left
💡 Hint

Group smaller variables together before larger ones.

Predict Output
expert
2:00remaining
What is the output of this Solidity storage slot calculation?

Given this Solidity contract snippet, what is the value of slot after execution?

contract Test {
    struct S {
        uint128 a;
        uint128 b;
        uint256 c;
    }
    S public s;
    function getSlot() public pure returns (uint) {
        uint slot;
        assembly {
            slot := s.slot
        }
        return slot;
    }
}
A1
B0
CSlot number depends on compiler version and cannot be determined.
DCompilation error due to invalid assembly.
Attempts:
2 left
💡 Hint

State variables are assigned storage slots starting from 0 in order.