Enum class declaration in Kotlin - Time & Space Complexity
Let's see how the time it takes to run code changes when we use an enum class in Kotlin.
We want to know how the program's work grows as we use more enum values.
Analyze the time complexity of the following code snippet.
enum class Direction {
NORTH, SOUTH, EAST, WEST
}
fun printDirections() {
for (dir in Direction.values()) {
println(dir)
}
}
This code defines an enum with four directions and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all enum values using
Direction.values(). - How many times: Once for each enum constant (4 times here).
As the number of enum values grows, the loop runs once for each value.
| Input Size (n) | Approx. Operations |
|---|---|
| 4 | 4 (one print per direction) |
| 10 | 10 |
| 100 | 100 |
Pattern observation: The work grows directly with the number of enum values.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of enum entries.
[X] Wrong: "Enum classes run in constant time no matter how many values they have."
[OK] Correct: Because looping through all enum values takes time proportional to how many values exist.
Understanding how enum classes work helps you explain simple loops and collections clearly, a skill useful in many coding discussions.
"What if we replaced the loop with a function that looks up a single enum value by name? How would the time complexity change?"
Practice
enum class in Kotlin?Solution
Step 1: Understand enum class purpose
An enum class is designed to hold a fixed set of constants that belong together logically.Step 2: Compare options with enum usage
Only To group a fixed set of related constants under one type correctly describes grouping related constants under one type, which is the main use of enum classes.Final Answer:
To group a fixed set of related constants under one type -> Option BQuick Check:
Enum class purpose = group constants [OK]
- Thinking enums are for creating multiple instances
- Confusing enums with regular classes
- Believing enums can hold any data type variable
Direction with constants NORTH, SOUTH, EAST, and WEST?Solution
Step 1: Recall Kotlin enum class syntax
Kotlin enum classes start withenum classfollowed by the name and curly braces containing constants separated by commas.Step 2: Check each option's syntax
enum class Direction { NORTH, SOUTH, EAST, WEST } matches the correct syntax. Options A, C, and D have incorrect keywords, punctuation, or structure.Final Answer:
enum class Direction { NORTH, SOUTH, EAST, WEST } -> Option AQuick Check:
Correct enum syntax = enum class Direction { NORTH, SOUTH, EAST, WEST } [OK]
- Using 'class enum' instead of 'enum class'
- Separating constants with semicolons instead of commas
- Assigning enum constants with '=' instead of listing
enum class Color { RED, GREEN, BLUE }
fun main() {
val c = Color.GREEN
println(c.name)
}Solution
Step 1: Understand enum constant property 'name'
Each enum constant has a built-innameproperty that returns its exact name as a string.Step 2: Analyze the code output
The code printsc.namewherecisColor.GREEN, so it prints "GREEN" exactly as declared.Final Answer:
GREEN -> Option DQuick Check:
Enum constant name property = GREEN [OK]
- Expecting lowercase output
- Printing enum constant directly without .name
- Confusing ordinal number with name
enum class Size {
SMALL,
MEDIUM
LARGE
}Solution
Step 1: Check enum constants separation
Enum constants must be separated by commas except the last one. Here,MEDIUMis missing a comma after it.Step 2: Validate other options
Constants can be uppercase, no constructor is required, and no minimum count is needed. So only missing comma is the error.Final Answer:
Missing comma after MEDIUM -> Option CQuick Check:
Enum constants separated by commas [OK]
- Omitting commas between constants
- Using lowercase for constants (not required)
- Thinking constructor is mandatory
rgb to an enum class Color that stores the color's RGB integer value, and access it for Color.RED? Choose the correct code snippet.Solution
Step 1: Understand enum property declaration
To add a property, declare a constructor parameter in the enum class and pass values to each constant.Step 2: Check each option for correct syntax
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } correctly declaresval rgb: Intin constructor and assigns values. enum class Color { RED = 0xFF0000, GREEN = 0x00FF00, BLUE = 0x0000FF } fun main() { println(Color.RED.rgb) } uses invalid assignment syntax. enum class Color { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } misses constructor declaration. enum class Color { RED, GREEN, BLUE } fun main() { println(Color.RED.rgb) } has no property defined.Final Answer:
enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } -> Option AQuick Check:
Enum property via constructor = enum class Color(val rgb: Int) { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) } fun main() { println(Color.RED.rgb) } [OK]
- Trying to assign values with '=' inside enum constants
- Not declaring constructor parameters in enum class
- Accessing property that was never declared
Ready to prove it?
Pass the test and this pattern moves unknown
