Enum with properties and methods in Kotlin - Time & Space Complexity
Enums with properties and methods let us group related values with extra details and actions.
We want to see how the time to run code with enums changes as we use more enum values or call their methods.
Analyze the time complexity of the following code snippet.
enum class Color(val rgb: Int) {
RED(0xFF0000) {
override fun printName() = println("Red")
},
GREEN(0x00FF00) {
override fun printName() = println("Green")
},
BLUE(0x0000FF) {
override fun printName() = println("Blue")
};
abstract fun printName()
}
fun printAllColors() {
for (color in Color.values()) {
color.printName()
}
}
This code defines an enum with three colors, each having a property and a method. Then it loops over all colors and calls their method.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over all enum values with
Color.values()and callingprintName(). - How many times: Once for each enum constant (3 times here, but can be more if enum grows).
As the number of enum constants grows, the loop runs once per constant.
| Input Size (n) | Approx. Operations |
|---|---|
| 3 | 3 calls to printName() |
| 10 | 10 calls to printName() |
| 100 | 100 calls to printName() |
Pattern observation: The work grows directly with the number of enum values. More values mean more method calls.
Time Complexity: O(n)
This means the time to run the loop and call methods grows linearly with the number of enum constants.
[X] Wrong: "Enums are fixed and small, so their methods always run in constant time no matter what."
[OK] Correct: While enums often have few values, if you loop over all values or add many constants, the time grows with the number of constants, not always constant.
Understanding how enum methods run helps you explain how your code scales when using enums with many values or complex methods.
"What if the printName() method called another function that loops over a list inside each enum constant? How would the time complexity change?"
Practice
enum with properties and methods in Kotlin?Solution
Step 1: Understand enum basics
Enums group fixed related values, like days or colors.Step 2: Recognize added properties and methods
Adding properties and methods lets each enum value hold extra data and behavior.Final Answer:
To group related fixed values with additional data and behavior -> Option AQuick Check:
Enums = fixed values + extra data [OK]
- Thinking enums are for unrelated variables
- Confusing enums with regular classes
- Assuming enums can't have methods
color of type String in Kotlin?Solution
Step 1: Check enum class declaration with property
In Kotlin, enum classes can have constructor parameters likeval color: String.Step 2: Verify enum values initialization
Each enum value must pass the property value in parentheses, e.g.,APPLE("Red").Final Answer:
enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } -> Option CQuick Check:
Enum with constructor and values = enum class Fruit(val color: String) { APPLE("Red"), BANANA("Yellow") } [OK]
- Using colon syntax instead of constructor parameters
- Trying to assign properties inside enum body incorrectly
- Omitting parentheses for enum values
enum class Size(val label: String) {
SMALL("S"), MEDIUM("M"), LARGE("L");
fun description() = "Size is $label"
}
fun main() {
println(Size.MEDIUM.description())
}Solution
Step 1: Understand enum property and method
Each enum value has alabelproperty: MEDIUM has "M".Step 2: Evaluate the
The method returns "Size is $label", so for MEDIUM it returns "Size is M".description()method callFinal Answer:
Size is M -> Option AQuick Check:
Method uses label property = "Size is M" [OK]
- Printing enum name instead of property
- Confusing property label with enum constant name
- Ignoring method call output
enum class Direction(val degrees: Int) {
NORTH(0),
EAST(90),
SOUTH(180),
WEST
}Solution
Step 1: Check enum constructor parameters
The enum class requires anIntparameterdegreesfor each constant.Step 2: Verify each enum constant initialization
All constants exceptWESTprovide a value;WESTis missing(value).Final Answer:
Missing property value for WEST enum constant -> Option DQuick Check:
All enum constants must pass constructor values [OK]
- Forgetting to pass constructor arguments for all constants
- Assuming semicolon is mandatory after constants
- Thinking enum constants must be lowercase
enum class TrafficLight(val duration: Int) {
RED(30) {
override fun signal() = "Stop"
},
YELLOW(5) {
override fun signal() = "Caution"
},
GREEN(25) {
override fun signal() = "Go"
};
abstract fun signal(): String
}
fun main() {
val lights = TrafficLight.values()
val signals = lights.map { it.signal() }
println(signals)
}What is the output when running
main()?Solution
Step 1: Understand enum with abstract method
Each enum constant overridessignal()to return a specific string.Step 2: Map enum values to their signals
Themapcallssignal()on each constant, producing ["Stop", "Caution", "Go"].Final Answer:
["Stop", "Caution", "Go"] -> Option BQuick Check:
Abstract method overridden per constant = correct signals [OK]
- Printing enum names instead of method results
- Confusing property values with method outputs
- Ignoring abstract method override requirement
Ready to prove it?
Pass the test and this pattern moves unknown
