Arrays (fixed and dynamic) in Blockchain / Solidity - Time & Space Complexity
When working with arrays in blockchain, it's important to know how the time to process them changes as they grow.
We want to understand how operations on fixed and dynamic arrays take longer or stay the same as the array size increases.
Analyze the time complexity of the following code snippet.
contract ArrayExample {
uint[] dynamicArray;
uint[5] fixedArray;
function addToDynamic(uint value) public {
dynamicArray.push(value);
}
function readFixed(uint index) public view returns (uint) {
return fixedArray[index];
}
}
This code shows adding an element to a dynamic array and reading an element from a fixed-size array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding an element to the dynamic array and accessing an element by index.
- How many times: Each add or read happens once per call, no loops here.
Adding to a dynamic array usually takes the same time regardless of size, but sometimes it takes longer when resizing happens.
| Input Size (n) | Approx. Operations for addToDynamic |
|---|---|
| 10 | ~1 operation per add |
| 100 | Mostly 1 operation, occasional more for resize |
| 1000 | Mostly 1 operation, occasional more for resize |
Reading from fixed array always takes the same time no matter the size.
Pattern observation: Adding is usually quick but sometimes slower; reading is always quick.
Time Complexity: O(1)
This means adding or reading an element takes about the same time no matter how big the array is.
[X] Wrong: "Adding to a dynamic array always takes longer as it grows because it has to move all elements every time."
[OK] Correct: Dynamic arrays resize only occasionally, so most adds are quick. The costly resize happens rarely, spreading its cost over many adds.
Understanding how arrays work in blockchain helps you write efficient smart contracts and answer questions about data handling in interviews.
"What if we changed the dynamic array to a fixed-size array? How would the time complexity of adding elements change?"
Practice
What is the main difference between a fixed array and a dynamic array in blockchain smart contracts?
Solution
Step 1: Understand fixed array size
Fixed arrays have a size defined when declared and cannot change during contract execution.Step 2: Understand dynamic array size
Dynamic arrays can grow or shrink by adding or removing elements during contract execution.Final Answer:
Fixed arrays have a set size, dynamic arrays can change size during execution. -> Option AQuick Check:
Fixed vs dynamic size = A [OK]
- Thinking fixed arrays can change size
- Confusing data types with array size
- Assuming dynamic arrays are slower to access
Which of the following is the correct syntax to declare a dynamic array of uint in Solidity?
?
Solution
Step 1: Recall Solidity array syntax
Dynamic arrays are declared with the type followed by empty square brackets:uint[].Step 2: Check each option
uint[] numbers; uses correct syntax:uint[] numbers;. uint numbers[]; places brackets after variable name, which is invalid. uint numbers(); uses parentheses, which is wrong. uint[] numbers = new uint[]; tries to initialize without size, which is invalid syntax.Final Answer:
uint[] numbers; -> Option BQuick Check:
Dynamic array syntax = A [OK]
- Placing brackets after variable name
- Using parentheses instead of brackets
- Trying to initialize without size incorrectly
What will be the output of the following Solidity code snippet?
pragma solidity ^0.8.0;
contract Test {
uint[] public numbers;
function addNumbers() public {
numbers.push(10);
numbers.push(20);
numbers.push(30);
}
function getSecond() public view returns (uint) {
return numbers[1];
}
}Assuming addNumbers() is called once before getSecond(), what does getSecond() return?
Solution
Step 1: Understand array after push calls
After callingaddNumbers(), the arraynumberscontains [10, 20, 30].Step 2: Access the element at index 1
Arrays in Solidity are zero-indexed, sonumbers[1]is the second element, which is 20.Final Answer:
20 -> Option DQuick Check:
numbers[1] = 20 [OK]
- Confusing index 1 with first element
- Expecting an error for valid index
- Mixing up push order
Identify the error in this Solidity code snippet that tries to remove the last element from a dynamic array:
pragma solidity ^0.8.0;
contract RemoveLast {
uint[] public data = [1, 2, 3];
function removeLast() public {
data.pop(1);
}
}Solution
Step 1: Check pop() method usage
In Solidity,pop()removes the last element and takes no arguments.Step 2: Identify incorrect call
The code callsdata.pop(1);which is invalid syntax and causes a compilation error.Final Answer:
pop() does not take any arguments; calling pop(1) causes an error. -> Option CQuick Check:
pop() no args = C [OK]
- Passing arguments to pop()
- Thinking pop() removes first element
- Believing pop() is unavailable for dynamic arrays
Given a fixed array uint[3] fixedArr = [5, 10, 15]; and a dynamic array uint[] dynamicArr;, which code snippet correctly copies all elements from fixedArr to dynamicArr in Solidity?
Solution
Step 1: Understand array assignment rules
In Solidity, fixed-size arrays cannot be directly assigned to dynamic arrays.Step 2: Copy elements using a loop
Using a for loop to push each element fromfixedArrintodynamicArris the correct way.Final Answer:
for (uint i = 0; i < fixedArr.length; i++) { dynamicArr.push(fixedArr[i]); } -> Option AQuick Check:
Copy fixed to dynamic with loop = D [OK]
- Trying to assign fixed array directly to dynamic array
- Using push with whole array instead of elements
- Reinitializing dynamic array incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
