Solidity Smackdown: Can You Answer These 7 Tricky Interview Questions?

Srinivas Joshi
CoinsBench
Published in
8 min readMar 29, 2023

--

Photo by Sebastian Herrmann on Unsplash

In this blog post, I’ll be sharing my tips on how to ace your Solidity interviews. When I myself searched for “Solidity interview questions”, I found a lot of websites listing some basic questions and then promoting their own services☹️

But that’s not what you’ll find here. Instead, I’m going to share 7 challenging questions for you to try and answer on your own, and then I’ll provide simplified answers and helpful resources to guide you further. So, get ready to level up your Solidity knowledge and impress your future employers!🚀

Questions

  1. What is the difference between pure and view functions in solidity ?
  2. What is the difference between calldata and memory in solidity?
  3. What is the difference between abi.encode() and abi.encodePacked()?
  4. Explain and immutable and constant and their difference in solidity .
  5. Which opcode in solidity consumes the most gas ?
  6. How do you bypass the extcodesize() used to see if the address is a smart contract ?
  7. Explain the difference between call() ,delegatecall() and staticcall() .

Solutions

I hope you all gave those questions your best shot! Let me know in the comments how many you were able to answer.

These questions were arranged in order of increasing difficulty, so don’t worry if you weren’t able to answer them all. The goal is to challenge yourself and learn something new in the process🤝

Solution 1 : What is the difference between pure and view functions in solidity?

In Solidity, pure and view are two different function modifiers used to indicate the type of function and how it interacts with the Ethereum network.

pure: A function declared as pure indicates that it doesn't read or modify the state of the contract or any external contracts. It only uses the input parameters to perform some calculations and returns the result. Pure functions can be called without sending a transaction to the network, and their execution doesn't consume any gas.

For example, the following function add is pure as it doesn't access any state variable or read data from any external contract:

function add(uint x, uint y) pure returns (uint) {
return x + y;
}

view: A function declared as view indicates that it only reads the state of the contract or any external contracts, but doesn't modify it. View functions can be called without sending a transaction to the network, and their execution doesn't consume any gas.

For example, the following function getBalance is view as it only reads the balance of the contract address:

function getBalance() view returns (uint) {
return address(this).balance;
}

Note that a view function can't modify the state of the contract, but it can modify the state of any external contracts it calls. If a view function tries to modify the state of the contract or any external contract, it will result in a runtime exception.

Solution 2 : What is the difference between calldata and memory in solidity?

This question tests your knowledge on Ethereum Virtual Machine(EVM) . In EVM, there are three main data locations: storage, memory, and calldata.

  • storage: The storage location is a persistent and expensive data storage area that is used to store the contract's state variables. It's similar to a database in traditional programming languages and is used to store values that need to persist across multiple transactions.
  • memory: The memory data location is used to store temporary variables that are only needed during the execution of a function. memory is a dynamic area of memory, which means that its size can be allocated and deallocated at runtime. The memory data location is used for variables that are too large to fit in the stack, or that need to be dynamically resized during execution.

Note : memory variables are initialised to their default values and are cleaned up after the function execution is complete, and their data is not persisted to the blockchain.

For example, the following function takes an array of integers as an argument and returns their sum:

function arraySum(uint[] memory arr) public pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
  • calldata: The calldata data location is used to store the function arguments passed to a contract function call. calldata is a read-only area of memory where the function arguments are stored when the function is called. It's similar to the stack in a traditional programming language, but with the additional constraint that it's read-only.

For example, the following function takes an array of uint as an argument passed as calldata and returns the sum of its elements.This is commonly used when passing large arrays to a contract function, as it avoids copying the entire array to the memory data location, which can be expensive in terms of gas cost.

function arraySum(uint[] calldata arr) external pure returns (uint) {
uint sum = 0;
for (uint i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}

Solution 3 : What is the difference between abi.encode() and abi.encodePacked()?

abi.encode() and abi.encodePacked() are both Solidity functions that are used to encode Solidity function arguments or data into a format that can be passed to other Solidity functions or contracts. However, there is a key difference between the two functions.

abi.encode() is used to encode data in a way that includes the data type and size(save necessary metadata and is compatible with the Solidity ABI specification)

abi.encodePacked() takes a list of values and encodes them in a tightly packed format, without adding any additional metadata or padding. This means that the resulting encoded data is shorter and more efficient than the data encoded by abi.encode()

Common uses :

abi.encode() : When you want to call a function in another contract, you need to encode the function arguments using abi.encode() and pass the resulting encoded data to the call() function or use a higher-level function like sendTransaction().

abi.encodePacked() : If you need to hash data for use in cryptographic operations, you can use abi.encodePacked() to create a tightly packed byte array representation of the data, which can then be hashed using a hash function like keccak256.

How are you guys doing so far ? If you like this blog post : give me a follow😁 and share this one

Solution 4 : Explain and immutable and constant and their difference in solidity

immutable and constant are both keywords in Solidity that are used to declare variables with a fixed value.

  • Both constant and immutable variables are stored in the contract's bytecode and are available to all functions in the contract.
  • constant variables are automatically made public , while immutable variables can be declared as public or private.
  • constant variables can be array and mappings(with some limitations and restrictions😬) while immutable are not allowed to be arrays, mappings or have a dynamic size.

The key difference between constant and immutable is when their value is set. constant variables have a value that is known at compile-time, while immutable variables have a value that is set at contract deployment time. Additionally, immutable variables are allowed to be declared with constructor parameters, allowing them to have a value that is not known until the contract is deployed.

Solution 5 : Which opcode in solidity consumes the most gas ?

The opcode that consumes the most gas in Solidity is usually the SSTORE opcode, which is used to store a value in the contract's storage.

The exact amount of gas consumed by the SSTORE opcode depends on several factors, including whether the storage slot being updated is currently empty or contains a non-zero value

Other opcodes that can consume a significant amount of gas include DELEGATECALL, CALL, and CREATE .

The gas cost of a CREATE operation depends on several factors, including the size of the bytecode, the complexity of the code, and the gas price set by the network. If the bytecode is large or the code is complex, the deployment process can consume a significant amount of gas, which can make the CREATE opcode more expensive than SSTORE.

The gas cost of a CALL or DELEGATECALL operation can depend on several factors, including the amount of data being passed between the contracts, the depth of the call stack, and the gas price set by the network. If the function being called is complex or the data being passed is large, the gas cost can increase significantly.

Note : It’s worth noting that gas costs can vary depending on the version of the Ethereum Virtual Machine (EVM) being used and the current gas price on the network. As a result, it’s important to carefully consider gas costs when designing and testing your Solidity contracts, and to optimise your code to minimise gas usage wherever possible.

Solution 6 : How do you bypass the extcodesize() used to see if the address is a smart contract ?

You can determine if an address is a Solidity smart contract by checking the size of the code stored at the address. Assembly extcodesize is used in Solidity functions to determine the size of the code at a particular address. If the code size at the address is greater than 0 then the address is a smart contract.

Consider a target contract using this method and you need to write a smart contract to bypass this(with ethics in mind!)

To bypass this simply put a function in the attacking contract’s constructor. During contract creation when the constructor is executed there is no code yet so the code size will be 0. The constructor will run the function and bypass the target contract’s extcodesize check.

I learnt this trick from a puzzle in Ethernaut😉

Solution 7 : Explain the difference between call() ,delegatecall() and staticcall() .

In Solidity, there are three ways to invoke a function on an external contract: call(), delegatecall(), and staticcall(). Each has its own unique features and use cases.

  1. call(): The call() function is the most commonly used way to invoke a function on an external contract. When you call a function using call(), a new message is created and sent to the called contract. The called contract executes the function and returns a boolean value to indicate whether the function was successful or not. call() can also return a value if the called function returns a value. However, call() does not modify the state of the calling contract.Used in factory pattern to deploy new contracts.
  2. delegatecall(): The delegatecall() function is similar to call(), but with one important difference: it preserves the calling contract's storage context. When you use delegatecall(), the called contract executes in the same storage context as the calling contract. This means that the called contract can read and write to the calling contract's storage. However, the called contract's storage is not affected by the function call.Used in implementing upgradeable contracts by using a proxy contract and a separate implementation contract.
  3. staticcall(): The staticcall() function is used to call a function on an external contract without modifying the state of either contract. When you use staticcall(), the called function can only read data from the called contract, but cannot modify it. The calling contract's storage is also protected, so the called function cannot read or modify the calling contract's storage.Used in implementing read-only functions that return data from other contracts.

References

  1. Pure and View in solidity
  2. Data locations on EVM
  3. ABI encoding functions in Solidity
  4. Constant and immutable variables in solidity
  5. Understand EVM and Opcodes
  6. Learning assembly and extcodesize
  7. Calling other smart contracts

I really hope this post was informative and helped you understand some of the key concepts in Solidity, as covered by these questions. Thank you so much for taking the time to read it! If you found this post useful, please consider sharing it with your friends and colleagues. Your support means a lot to me 🥳.

--

--

Frontend Developer | Learning and sharing solidity,EVM knowledge 🚀