Enums in Blockchain / Solidity - Time & Space Complexity
Enums help organize related values in blockchain code. Understanding their time complexity shows how fast operations with enums run as the program grows.
We want to know how the time to use enums changes when we work with more data.
Analyze the time complexity of the following code snippet.
enum Status {
Pending,
Confirmed,
Failed
}
function checkStatus(Status current) public pure returns (string memory) {
if (current == Status.Pending) {
return "Waiting";
} else if (current == Status.Confirmed) {
return "Success";
} else {
return "Error";
}
}
This code defines an enum with three states and a function that checks the current state to return a message.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Simple if-else checks comparing enum values.
- How many times: Each call runs these checks once, no loops or recursion.
Checking an enum value takes the same steps no matter how many enum options exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 (enum states) | 3 checks max |
| 10 (if more states) | 10 checks max |
| 100 (many states) | 100 checks max |
Pattern observation: The number of checks grows directly with the number of enum states.
Time Complexity: O(n)
This means the time to check an enum grows linearly with the number of enum options.
[X] Wrong: "Enums always take constant time to check because they are simple values."
[OK] Correct: If you check enums with many if-else statements, the time grows with the number of options, not constant.
Knowing how enum checks scale helps you write clear and efficient blockchain code. It shows you understand how small design choices affect speed.
"What if we replaced the if-else chain with a mapping from enum to string? How would the time complexity change?"
Practice
enum in blockchain programming?Solution
Step 1: Understand enum purpose
Enums group related named values, making code easier to read and safer.Step 2: Compare options
Only To group related named values together for clarity and safety correctly describes enums' main purpose in blockchain code.Final Answer:
To group related named values together for clarity and safety -> Option DQuick Check:
Enums group named values = A [OK]
- Confusing enums with random number generators
- Thinking enums store large data
- Believing enums speed up contract execution
Status with values Pending, Active, and Closed in Solidity?Solution
Step 1: Recall Solidity enum syntax
In Solidity, enums are declared withenum Name { Value1, Value2, ... }without equals or colons.Step 2: Check each option
enum Status { Pending, Active, Closed } matches correct syntax; others use invalid symbols or quotes.Final Answer:
enum Status { Pending, Active, Closed } -> Option CQuick Check:
Solidity enum syntax uses braces and commas = C [OK]
- Using equals sign instead of braces
- Adding quotes around enum values
- Using colons instead of braces
enum Role { User, Admin, Guest }and the code:
Role userRole = Role.Admin;
if (userRole == Role.Guest) {
return "Guest Access";
} else {
return "Not Guest";
}
What will be the output?
Solution
Step 1: Understand enum values and assignment
Role.Admin is assigned to userRole, which is not equal to Role.Guest.Step 2: Evaluate the if condition
The conditionuserRole == Role.Guestis false, so the else branch runs returning "Not Guest".Final Answer:
"Not Guest" -> Option AQuick Check:
Role.Admin != Role.Guest, so output = "Not Guest" [OK]
- Assuming Admin equals Guest
- Expecting compilation or runtime errors
- Confusing enum names with strings
enum State { Created, Locked, Inactive }
State public state;
function setState(uint _state) public {
state = _state;
}Solution
Step 1: Analyze enum assignment
Enums require assignment from enum values, not raw uint integers.Step 2: Check function assignment
Assigning_state(uint) directly tostate(enum) causes a type mismatch error.Final Answer:
Cannot assign uint directly to enum variable -> Option BQuick Check:
Enum variables need enum values, not uints [OK]
- Assigning integers directly to enums
- Thinking enum syntax is wrong
- Confusing function visibility with errors
Admin or Moderator role to allow special access. Given:enum Role { User, Admin, Moderator, Guest }Which of the following Solidity code snippets correctly checks for this condition?
Solution
Step 1: Understand logical conditions for enums
To allow access if userRole is Admin OR Moderator, use logical OR (||) between equality checks.Step 2: Evaluate each option
if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } uses correct OR condition; B uses AND which is impossible; C uses invalid syntax; A uses non-existent methodequals.Final Answer:
if (userRole == Role.Admin || userRole == Role.Moderator) { /* allow access */ } -> Option AQuick Check:
Use || for OR between enum comparisons [OK]
- Using && instead of || for OR condition
- Trying to use 'in' keyword for enums
- Calling non-existent methods on enums
Ready to prove it?
Pass the test and this pattern moves unknown
