Data types (uint, int, bool, address, string) in Blockchain / Solidity - Time & Space Complexity
When working with blockchain data types, it's important to know how operations on them grow as data size changes.
We want to see how the time to handle these types changes when the input gets bigger.
Analyze the time complexity of the following code snippet.
contract Example {
string public name;
uint[] public numbers;
function addNumber(uint num) public {
numbers.push(num);
}
function getNameLength() public view returns (uint) {
return bytes(name).length;
}
}
This code stores a string and a list of numbers, adds numbers to the list, and gets the length of the string.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the length of the string by converting it to bytes.
- How many times: The length operation checks each byte once, so it depends on the string size.
As the string gets longer, counting its length takes more steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps to count bytes |
| 100 | 100 steps to count bytes |
| 1000 | 1000 steps to count bytes |
Pattern observation: The time grows directly with the string length.
Time Complexity: O(n)
This means the time to get the string length grows linearly with the string size.
[X] Wrong: "Getting the length of a string is always instant and does not depend on size."
[OK] Correct: In blockchain, strings are stored as byte arrays, so counting length requires checking each byte, which takes more time for longer strings.
Understanding how data types affect operation time helps you write efficient smart contracts and explain your reasoning clearly.
"What if we changed the string to a fixed-size byte array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the difference between
uintandintuintstands for unsigned integer, meaning it can only hold zero or positive numbers.intcan hold both positive and negative numbers.Step 2: Identify the correct type for only zero or positive numbers
Since the question asks for numbers that are zero or positive only,uintis the correct choice.Final Answer:
uint-> Option BQuick Check:
Positive numbers =uint[OK]
uint for non-negative numbers only [OK]- Choosing
intfor positive-only numbers - Confusing
boolwith numeric types - Using
addressfor numbers
isActive in Solidity?Solution
Step 1: Recall the correct boolean values in Solidity
Boolean variables can only be assignedtrueorfalse, not numbers or strings.Step 2: Check each option for correct boolean assignment
bool isActive = true; assignstruecorrectly. Options B and D use numbers, and C uses a string, which are invalid forbool.Final Answer:
bool isActive = true; -> Option DQuick Check:
Boolean = true or false [OK]
true or false to bool variables [OK]- Using 1 or 0 instead of true/false
- Assigning strings like "true"
- Missing semicolon at the end
int x = -5; uint y = 10; bool flag = (x < 0); string memory text = "Hello"; address addr = 0x1234567890123456789012345678901234567890; // Output values x flag text addr
Solution
Step 1: Analyze each variable's value
xis-5(signed int),flagistruebecausex < 0,textis "Hello", andaddris a valid address.Step 2: Confirm output matches variable values
-5, true, Hello, 0x1234567890123456789012345678901234567890 correctly lists all values as expected. 5, false, Hello, 0x1234567890123456789012345678901234567890 incorrectly changesxandflag. -5, false, "Hello", 0x0 has wrongflagandaddr. Compilation error due to address format is wrong because the address format is valid.Final Answer:
-5, true, Hello, 0x1234567890123456789012345678901234567890 -> Option CQuick Check:
Variable values printed as is [OK]
- Assuming uint can hold negative values
- Confusing string quotes in output
- Thinking address format is invalid
address myAddress = "0xAbC1234567890";
Solution
Step 1: Understand address assignment rules
In Solidity, addresses are assigned as hexadecimal literals without quotes, not as strings.Step 2: Identify the error in the code
The code uses quotes around the address, making it a string, which is invalid foraddresstype.Final Answer:
Address must be assigned without quotes -> Option AQuick Check:
Address literals have no quotes [OK]
- Using quotes around addresses
- Declaring address as string
- Removing 0x prefix from address
Solution
Step 1: Match each field to the correct data type
User ID is a positive number, souintfits. Username is text, sostring. Active status is true/false, sobool. Wallet address is a blockchain address, soaddress.Step 2: Verify options against these matches
uintfor user ID,stringfor username,boolfor active status,addressfor wallet matches all fields correctly. Other options mix types incorrectly, like usingintfor user ID or swappingstringandaddress.Final Answer:
uintfor user ID,stringfor username,boolfor active status,addressfor wallet -> Option AQuick Check:
Match field types carefully [OK]
- Using
intfor positive-only IDs - Confusing
stringandaddress - Using
uintfor boolean values
Ready to prove it?
Pass the test and this pattern moves unknown
