Technical Senior Level

What techniques do you use to reduce gas costs in Solidity? Explain storage versus memory costs and how data layout affects gas.

Quick Tip

Lead with the biggest wins: "Packing structs into fewer storage slots saves thousands of gas per transaction. After that, caching storage reads in memory variables and using calldata for read-only function parameters."

What good answers include

Storage is the most expensive operation (~20,000 gas for SSTORE). Optimisations: pack struct variables to fill 32-byte storage slots, use memory/calldata instead of storage for temporary data, cache storage reads in local variables, use events instead of storage for data only needed off-chain, use mappings instead of arrays when possible, and batch operations. Strong candidates discuss EIP-2929 cold/warm storage access costs, SSTORE refunds, and when to use immutable/constant.

What interviewers are looking for

Critical for production smart contracts where users pay gas. Candidates who write readable but gas-wasteful code need to understand the real cost. Ask them to estimate gas savings from a specific optimisation.

← All Solidity questions