When without argument in Kotlin - Time & Space Complexity
We want to understand how the time it takes to run a Kotlin when expression without an argument changes as we add more conditions.
How does the number of checks grow when we add more cases?
Analyze the time complexity of the following code snippet.
fun checkNumber(num: Int) {
when {
num < 0 -> println("Negative")
num == 0 -> println("Zero")
num > 0 -> println("Positive")
}
}
This code checks a number against several conditions without using a direct argument in when.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whenexpression checks each condition one by one. - How many times: It checks conditions until one matches or all are checked.
As you add more conditions, the program checks more cases in order until it finds a match.
| Number of Conditions (n) | Approx. Checks |
|---|---|
| 3 | Up to 3 checks |
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
Pattern observation: The number of checks grows directly with the number of conditions.
Time Complexity: O(n)
This means the time to find a matching condition grows linearly as you add more conditions.
[X] Wrong: "The when without argument checks all conditions at once, so it is constant time."
[OK] Correct: Actually, it checks conditions one by one until it finds a match, so more conditions mean more checks.
Understanding how when without argument works helps you reason about condition checks and efficiency in Kotlin code, a useful skill for clear and efficient programming.
"What if the conditions were reordered so the most common case is first? How would the time complexity be affected in practice?"
Practice
when without argument do in Kotlin?Solution
Step 1: Understand
When used without an argument,whenwithout argumentwhenevaluates each condition as a boolean expression.Step 2: Compare with other uses
Unlikewhenwith argument, it does not match values but checks conditions one by one.Final Answer:
It checks boolean conditions one by one until one is true. -> Option BQuick Check:
whenwithout argument = boolean checks [OK]
- Thinking it matches values like switch-case
- Confusing it with loops
- Assuming it declares variables
when without argument in Kotlin?Solution
Step 1: Identify
The correct syntax omits the argument and uses boolean conditions directly inside curly braces.whenwithout argument syntaxStep 2: Check options
when { condition1 -> action1 condition2 -> action2 else -> defaultAction } matches this pattern with conditions and an else branch; others use an argument or incorrect structure.Final Answer:
when { condition1 -> action1 condition2 -> action2 else -> defaultAction } -> Option AQuick Check:
Correct syntax = when { condition1 -> action1 condition2 -> action2 else -> defaultAction } [OK]
- Adding argument inside parentheses
- Missing else branch
- Using values instead of conditions
val x = 10
when {
x < 5 -> println("Less than 5")
x in 5..10 -> println("Between 5 and 10")
else -> println("More than 10")
}Solution
Step 1: Evaluate conditions in order
First condition: x < 5 is false (10 < 5 is false). Second condition: x in 5..10 is true (10 is in 5 to 10).Step 2: Execute matching branch
Since second condition is true, it prints "Between 5 and 10" and stops checking further.Final Answer:
Between 5 and 10 -> Option CQuick Check:
10 in 5..10 = true [OK]
- Assuming x < 5 is true
- Ignoring range check
- Expecting else branch output
when without argument:val y = 3
val result = when {
y > 5 -> println("Greater than 5")
y < 5 -> println("Less than 5")
}Solution
Step 1: Check
The code has two conditions but no else branch to handle all other cases.whenbranchesStep 2: Understand Kotlin requirement
When used without argument, Kotlin requires an else branch to cover all possibilities or the compiler errors about non-exhaustive when.Final Answer:
Missing else branch for unmatched cases -> Option AQuick Check:
Always add else branch [OK]
- Ignoring else branch requirement
- Adding parentheses after when
- Thinking conditions must be constants
val temp = 25
when {
temp <= 0 -> println("Freezing")
temp in 1..15 -> println("Cold")
temp in 16..25 -> println("Warm")
temp > 25 -> println("Hot")
else -> println("Unknown")
}What will be printed and why is this
when usage correct?Solution
Step 1: Check which condition matches temp = 25
temp <= 0 is false; temp in 1..15 is false; temp in 16..25 is true (25 is included).Step 2: Confirm correctness of when usage
All possible values are covered by conditions and else branch ensures fallback; no overlap causes error.Final Answer:
"Warm"; because conditions cover all ranges with else as fallback -> Option DQuick Check:
25 in 16..25 = true, else covers all [OK]
- Misreading range boundaries
- Assuming 25 > 25 is true
- Thinking else is optional
Ready to prove it?
Pass the test and this pattern moves unknown
