EVM and Gas
1. How does Solidity lay out state variables in storage?
State variables go into 32-byte slots in declaration order, starting at slot 0, and consecutive variables smaller than 32 bytes share a slot when they fit.
The storage layout docs add the rules a candidate should know: structs and fixed arrays always start a new slot, a mapping takes one slot that stays empty while its values live at keccak256(key . slot), and a dynamic array stores its length in its slot and its data from keccak256(slot).
contract Packed {
uint128 a; // slot 0, low 16 bytes
uint128 b; // slot 0, high 16 bytes
uint256 c; // slot 1
address owner; // slot 2, 20 bytes
bool paused; // slot 2, packed next to owner
}
Packing saves gas only when the packed values are read or written together. Writing one uint128 on its own costs a read-modify-write of the whole slot.
A strong candidate also says that the layout is a public contract for upgradeable code, which questions 20 and 21 come back to.
2. When do you use calldata, memory and storage?
Use calldata for read-only external inputs, memory for temporary values you build or modify, and storage for references to state.
Calldata is read-only and avoids the copy into memory, so an external function that only reads an array or bytes parameter should take it as calldata.
The trap is assignment semantics. Assigning a storage struct to a memory variable copies it, so changes to the copy are lost. Assigning it to a storage local creates a reference, so changes write to state.
Interviewers like to show a loop that "updates" a memory copy of a user record and ask why nothing changed.
3. Why does the first read of a storage slot cost more than the second?
Since EIP-2929, the first access to a slot in a transaction is cold and costs 2,100 gas, and later reads of the same slot are warm and cost 100.
The EIP-2929 spec also charges 2,600 for the first touch of an account, and adds the 2,100 cold surcharge to an SSTORE on a cold slot.
Practical consequences a candidate should name:
- Caching a state variable in a local inside a loop saves the warm reads, about 100 gas each, not 2,100.
- Access lists (EIP-2930 transactions) can pre-warm slots and addresses a call will touch.
- Gas numbers in tests depend on whether earlier calls in the same transaction already warmed a slot, so a benchmark that calls a function twice measures the warm path.
4. What is transient storage, and where does it go wrong?
Transient storage (EIP-1153) is a per-contract key-value store that is discarded at the end of the transaction, read with TLOAD and written with TSTORE at 100 gas each.
Per the EIP, writes made inside a frame that reverts are rolled back, and TSTORE inside a STATICCALL throws.
Solidity 0.8.24 exposed it in inline assembly, and 0.8.28 added the transient data location for state variables.
The language docs still limit it to value types: no mappings, arrays or structs, no locals, and no initializer on the declaration.
contract Guarded {
bool transient locked;
modifier nonReentrant() {
require(!locked, "reentrant call");
locked = true;
_;
locked = false;
}
}
Two pitfalls separate senior answers.
First, a value left set leaks into later calls in the same transaction, which breaks composability when a router or multicall calls the contract twice; the docs recommend clearing transient storage at the end of every call.
Second, transient values are not a replacement for memory, because they survive across inner calls and revert differently.
5. What is the difference between call, delegatecall and staticcall?
call runs the target's code in the target's context, delegatecall runs the target's code in the caller's context, and staticcall is a call that reverts on any state change.
With delegatecall, storage writes land in the caller's storage, and msg.sender and msg.value stay those of the original call.
That is the whole basis of proxies and libraries with external functions, and also the source of their worst bugs. The target's storage layout is applied to the caller's slots, so a mismatch overwrites unrelated variables (question 25).
Calling delegatecall on a user-supplied address hands that user full control of the contract's storage and balance.