Challenge - 5 Problems
Gas Usage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Gas usage of simple storage update
Consider a smart contract function that updates a stored number. What is the gas usage behavior when calling this function multiple times with the same value?
Blockchain / Solidity
contract SimpleStorage {
uint256 public storedData;
function set(uint256 x) public {
storedData = x;
}
}Attempts:
2 left
💡 Hint
Think about how Ethereum refunds gas when storage is not changed.
✗ Incorrect
When the stored value changes, gas is spent to update storage. If the value is the same, less gas is used because no storage slot is modified.
❓ Predict Output
intermediate2:00remaining
Gas cost of adding vs removing elements in a mapping
Given a mapping in a smart contract, what happens to gas usage when you add a new key-value pair versus when you delete a key?
Blockchain / Solidity
contract MapExample {
mapping(address => uint256) public balances;
function addBalance(address user, uint256 amount) public {
balances[user] = amount;
}
function removeBalance(address user) public {
delete balances[user];
}
}Attempts:
2 left
💡 Hint
Consider how storage refunds work in Ethereum.
✗ Incorrect
Adding a new key writes to storage and costs gas. Deleting a key frees storage and refunds gas to the caller.
🔧 Debug
advanced3:00remaining
Unexpected high gas usage in loop
This contract function loops over an array and updates a mapping. Why does it consume unexpectedly high gas?
Blockchain / Solidity
contract LoopGas {
mapping(uint256 => uint256) public data;
uint256[] public keys;
function updateData() public {
for (uint i = 0; i < keys.length; i++) {
data[keys[i]] = i;
}
}
}Attempts:
2 left
💡 Hint
Check the loop boundary carefully.
✗ Incorrect
Using <= in the loop condition causes the loop to run one extra time, accessing keys[keys.length], which is out-of-bounds and wastes gas.
📝 Syntax
advanced2:00remaining
Gas optimization with unchecked arithmetic
Which option correctly uses unchecked arithmetic to save gas in Solidity 0.8+ without causing overflow errors?
Blockchain / Solidity
function sum(uint256 a, uint256 b) public pure returns (uint256) { // Fill in the blank }
Attempts:
2 left
💡 Hint
The unchecked block disables overflow checks inside it.
✗ Incorrect
In Solidity 0.8+, arithmetic operations check for overflow by default. Wrapping the operation in an unchecked block disables this check and saves gas.
🚀 Application
expert3:00remaining
Estimating gas cost for dynamic array resizing
You have a function that appends elements to a dynamic array in a smart contract. How does the gas cost behave as the array grows, and which option best describes this behavior?
Blockchain / Solidity
contract DynamicArray {
uint256[] public numbers;
function addNumber(uint256 num) public {
numbers.push(num);
}
}Attempts:
2 left
💡 Hint
Think about how storage slots are allocated for dynamic arrays.
✗ Incorrect
Each push writes to a new storage slot, so gas cost per push stays roughly constant regardless of array length.