Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

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

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

Practice

(1/5)
1.

What is the main benefit of packing variables in blockchain smart contracts?

easy
A. It makes the code run faster on the CPU
B. It reduces gas costs by using fewer storage slots
C. It increases the contract's storage size
D. It improves the contract's security automatically

Solution

  1. Step 1: Understand storage slots in blockchain

    Each storage slot costs gas. Packing variables reduces the number of slots used.
  2. Step 2: Effect of packing variables

    By grouping smaller variables into one slot, gas usage decreases because fewer slots are needed.
  3. Final Answer:

    It reduces gas costs by using fewer storage slots -> Option B
  4. Quick Check:

    Packing = Less gas [OK]
Hint: Packing saves gas by reducing storage slots used [OK]
Common Mistakes:
  • Thinking packing speeds up CPU execution
  • Believing packing increases storage size
  • Assuming packing improves security automatically
2.

Which of the following Solidity variable declarations correctly packs two uint8 variables into one storage slot?

contract Example {
    uint8 a;
    uint8 b;
}
easy
A. Both a and b are declared consecutively as uint8
B. a is uint8 and b is uint256
C. a is uint256 and b is uint8
D. Both a and b are declared as uint256

Solution

  1. Step 1: Check variable types for packing

    Variables of the same small type declared consecutively can be packed into one slot.
  2. Step 2: Analyze given declarations

    Both a and b are uint8, so they fit together in one 256-bit slot.
  3. Final Answer:

    Both a and b are declared consecutively as uint8 -> Option A
  4. Quick Check:

    Same small types together = packed [OK]
Hint: Use same small types consecutively to pack [OK]
Common Mistakes:
  • Mixing large and small types breaks packing
  • Declaring variables far apart prevents packing
  • Assuming different types pack automatically
3.

Consider this Solidity contract snippet:

contract Test {
    uint128 a = 1;
    uint64 b = 2;
    uint64 c = 3;
}

How many storage slots does this contract use for these variables?

medium
A. 1 storage slot
B. 3 storage slots
C. 2 storage slots
D. 4 storage slots

Solution

  1. Step 1: Calculate sizes of variables

    a is 128 bits, b and c are 64 bits each.
  2. Step 2: Pack variables into 256-bit slots

    a (128 bits) starts the slot, b and c (64+64=128 bits) fit exactly in the remaining space of the same slot.
  3. Final Answer:

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

    128 + 64 + 64 = 256 bits = 1 slot [OK]
Hint: Sum consecutive vars' bits: 128+64+64=256 -> 1 slot [OK]
Common Mistakes:
  • Counting each variable as one slot
  • Ignoring packing into remaining space after uint128
  • Assuming different sizes cannot pack together
4.

Identify the error in this Solidity contract that tries to pack variables for gas savings:

contract Faulty {
    uint256 a;
    uint8 b;
    uint256 d;
    uint8 c;
}
medium
A. Variables b and c must be declared before a
B. Variables a and d should be uint8 to pack
C. All variables are packed correctly without error
D. Variables b and c cannot be packed because d breaks the sequence

Solution

  1. Step 1: Understand packing order

    Packing works best when small variables are declared consecutively without large variables interrupting.
  2. Step 2: Analyze variable order

    b and c are small but separated by large a and d, so they cannot be packed together.
  3. Final Answer:

    Variables b and c cannot be packed because d breaks the sequence -> Option D
  4. Quick Check:

    Interrupting large vars break packing [OK]
Hint: Keep small vars together without large vars interrupting [OK]
Common Mistakes:
  • Assuming all small vars pack regardless of order
  • Changing types unnecessarily
  • Thinking order does not affect packing
5.

You want to optimize this Solidity struct for gas savings by packing variables:

struct Data {
    uint8 flag;
    uint256 id;
    uint16 count;
    uint8 status;
}

Which rearrangement packs variables best into the fewest storage slots?

hard
A.
struct Data {
    uint8 flag;
    uint256 id;
    uint16 count;
    uint8 status;
}
B.
struct Data {
    uint16 count;
    uint256 id;
    uint8 flag;
    uint8 status;
}
C.
struct Data {
    uint8 flag;
    uint8 status;
    uint16 count;
    uint256 id;
}
D.
struct Data {
    uint8 flag;
    uint16 count;
    uint256 id;
    uint8 status;
}

Solution

  1. Step 1: Order variables from smallest to largest

    Place small variables (uint8, uint16) together first to pack tightly.
  2. Step 2: Place largest variable last

    uint256 id is largest and should be last to avoid breaking packing of smaller vars.
  3. Final Answer:

    struct Data { uint8 flag; uint8 status; uint16 count; uint256 id; } -> Option C
  4. Quick Check:

    Small vars first, large last = best packing [OK]
Hint: Put small vars first, big vars last for best packing [OK]
Common Mistakes:
  • Placing large vars before small ones
  • Mixing variable sizes without order
  • Ignoring packing benefits in structs
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