Enum entries iteration in Kotlin - Time & Space Complexity
When we loop through all entries of an enum, we want to know how the time needed changes as the number of entries grows.
We ask: How does the work grow when the enum has more items?
Analyze the time complexity of the following code snippet.
enum class Color {
RED, GREEN, BLUE, YELLOW
}
fun printColors() {
for (color in Color.values()) {
println(color)
}
}
This code loops through all enum entries and prints each one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all enum entries using
Color.values(). - How many times: Once for each enum entry, so as many times as there are entries.
As the number of enum entries grows, the loop runs once per entry, so the work grows directly with the number of entries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 print operations |
| 100 | About 100 print operations |
| 1000 | About 1000 print operations |
Pattern observation: The work increases evenly as the number of enum entries increases.
Time Complexity: O(n)
This means the time grows in a straight line with the number of enum entries.
[X] Wrong: "Looping over enum entries is constant time because enums are fixed."
[OK] Correct: Even though enums have fixed entries, the loop still runs once per entry, so time grows with the number of entries.
Understanding how looping over enum entries scales helps you explain performance clearly and shows you know how to analyze simple loops well.
"What if we nested another loop inside to compare each enum entry with every other? How would the time complexity change?"
Practice
EnumName.values() return in Kotlin?Solution
Step 1: Understand the purpose of
Thevalues()values()function returns all enum entries as an array in the order they are declared.Step 2: Compare options with this behavior
Only An array of all enum entries in the order they are declared correctly describes this behavior; others describe different data types or partial data.Final Answer:
An array of all enum entries in the order they are declared -> Option AQuick Check:
EnumName.values()returns all entries [OK]
- Thinking values() returns strings instead of enum objects
- Assuming values() returns only one entry
- Confusing values() with valueOf()
Color in Kotlin?Solution
Step 1: Recall the correct method to get enum entries
In Kotlin,values()returns all enum entries as an array.Step 2: Check each option's syntax
The correct syntax isfor (c in Color.values()) { println(c) }, which usesColor.values()correctly in a for loop. The other options use invalid syntax: iterating directly overColor, or non-existent methods likeentries()orall().Final Answer:
for (c in Color.values()) { println(c) } -> Option AQuick Check:
Usevalues()to loop enums [OK]
EnumName.values() to loop enums [OK]- Trying to loop directly over enum class without values()
- Using non-existent methods like entries() or all()
- Missing parentheses after values()
enum class Direction { NORTH, EAST, SOUTH, WEST }What is the output of this code?
for (d in Direction.values()) {
print(d.name.first())
}Solution
Step 1: Understand the loop and print statement
The loop goes through all enum entries: NORTH, EAST, SOUTH, WEST. For each, it prints the first character of the name.Step 2: Determine printed characters
First letters are 'N', 'E', 'S', 'W'. They are printed without spaces or newlines.Final Answer:
NESW -> Option CQuick Check:
First letters combined = NESW [OK]
- Assuming spaces or newlines are printed
- Printing full names instead of first letters
- Confusing enum name with enum value
enum class Status { STARTED, RUNNING, FINISHED }
for (s in Status) {
println(s)
}Solution
Step 1: Identify how to iterate enums in Kotlin
Enums cannot be iterated directly; you must useStatus.values()to get all entries.Step 2: Check the code for missing
The code tries to loop overvalues()Statusdirectly, which is invalid syntax.Final Answer:
Missing call tovalues()to get enum entries -> Option DQuick Check:
UseEnum.values()to iterate enums [OK]
values() to loop enums [OK]- Trying to loop enum class directly without values()
- Thinking enums are iterable by default
- Adding unnecessary val in loop declaration
enum class Level(val difficulty: Int) {
EASY(1), MEDIUM(2), HARD(3)
}How do you create a map from each enum entry to its difficulty using iteration?
Solution
Step 1: Understand the goal
We want a map where keys are enum entries and values are their difficulty.Step 2: Use
values()andassociateWithLevel.values()gives all entries;associateWithmaps each entry to a value.Step 3: Check options
val map = Level.values().associateWith { it.difficulty } correctly usesassociateWith { it.difficulty }. val map = Level.associate { it to it.difficulty } is invalid becauseLevelis a class, not a collection. val map = Level.values().map { it.difficulty } returns a list, not a map. val map = Level.values().associateBy { it.difficulty } maps difficulty to enum, reversing keys and values.Final Answer:
val map = Level.values().associateWith { it.difficulty } -> Option BQuick Check:
Usevalues()+associateWithfor map [OK]
values().associateWith { } to map enums [OK]- Trying to call associate on enum class directly
- Using map() instead of associateWith() for key-value pairs
- Confusing associateBy() which reverses keys and values
Ready to prove it?
Pass the test and this pattern moves unknown
