When with multiple conditions in Kotlin - Time & Space Complexity
We want to see how the time it takes to run a Kotlin when statement changes as we add more conditions.
How does checking multiple conditions affect the program's speed?
Analyze the time complexity of the following code snippet.
fun checkNumber(num: Int): String {
return when (num) {
1, 2, 3 -> "Small number"
4, 5, 6 -> "Medium number"
7, 8, 9 -> "Large number"
else -> "Unknown"
}
}
This code checks a number against several groups of values using multiple conditions in each when branch.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking the input number against each condition group in the
whenstatement. - How many times: The program compares the input to each condition until it finds a match or reaches
else.
As the number of condition groups grows, the program checks more groups one by one until it finds a match.
| Input Size (number of condition groups) | Approx. Operations (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 condition groups.
Time Complexity: O(n)
This means the time to find the matching condition grows in a straight line as you add more condition groups.
[X] Wrong: "The when statement checks all conditions at once, so time stays the same no matter how many conditions there are."
[OK] Correct: The when checks conditions one by one until it finds a match, so more conditions mean more checks in the worst case.
Understanding how multiple conditions affect time helps you explain code efficiency clearly and shows you can think about how programs grow with input size.
"What if we replaced the multiple conditions in each when branch with a data structure lookup? How would the time complexity change?"
Practice
What does the when expression do when multiple conditions are separated by commas in Kotlin?
Solution
Step 1: Understand how
In Kotlin, when multiple conditions are separated by commas, it means thewhenhandles multiple conditionswhenexpression checks if the value matches any one of those conditions.Step 2: Interpret the behavior of multiple conditions
If the value matches any condition in the list, the corresponding block runs. It does not require all conditions to be true simultaneously.Final Answer:
It checks if the value matches any of the listed conditions and executes the corresponding block. -> Option BQuick Check:
Multiple conditions = any match triggers block [OK]
- Thinking all conditions must be true at once
- Believing only the first condition is checked
- Assuming multiple conditions cause errors
Which of the following is the correct syntax to check if a variable num is either 1, 2, or 3 using when with multiple conditions?
when (num) {
? -> println("Number is 1, 2, or 3")
else -> println("Other number")
}Solution
Step 1: Recall Kotlin
In Kotlin, multiple conditions in awhensyntax for multiple conditionswhencase are separated by commas without operators.Step 2: Match the correct syntax
1, 2, 3 uses commas to separate values:1, 2, 3, which is correct. Other options use logical operators or list syntax, which are invalid here.Final Answer:
1, 2, 3 -> Option AQuick Check:
Multiple conditions use commas [OK]
- Using || or && inside
whenconditions - Trying to use list syntax like [1, 2, 3]
- Missing commas between values
What will be the output of the following Kotlin code?
val x = 5
when (x) {
1, 2, 3 -> println("Low")
4, 5, 6 -> println("Medium")
else -> println("High")
}Solution
Step 1: Identify the value of
The variablexxis assigned the value 5.Step 2: Match
The second case checks ifxwithwhenconditionsxis 4, 5, or 6. Sincexis 5, this condition matches and prints "Medium".Final Answer:
Medium -> Option AQuick Check:
5 in 4,5,6 prints Medium [OK]
- Choosing the first matching group incorrectly
- Confusing else with matching values
- Assuming no output if multiple conditions exist
Find the error in this Kotlin when expression:
val grade = 'B'
when (grade) {
'A', 'B' -> println("Excellent")
'C' || 'D' -> println("Good")
else -> println("Try again")
}Solution
Step 1: Review the
In Kotlin, multiple conditions in awhencondition syntaxwhencase must be separated by commas, not logical operators like '||'.Step 2: Identify the incorrect usage
The line'C' || 'D'uses '||', which is invalid syntax inwhen. It should be'C', 'D'.Final Answer:
Using '||' insidewhenconditions is invalid syntax. -> Option DQuick Check:
Use commas, not logical operators [OK]
when conditions [OK]- Using logical operators instead of commas
- Thinking commas are wrong separators
- Confusing variable types with syntax errors
You want to write a Kotlin when expression that prints "Weekend" if the day is either "Saturday" or "Sunday", "Weekday" if the day is any other day of the week, and "Invalid" for any other input. Which code correctly implements this?
Solution
Step 1: Check correct multiple condition syntax for weekend days
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } correctly uses commas to separate "Saturday" and "Sunday" to match either day.Step 2: Verify handling of other days and else case
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } lists all weekdays explicitly and useselsefor invalid inputs, matching the problem requirements.Final Answer:
when(day) { "Saturday", "Sunday" -> println("Weekend") "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> println("Weekday") else -> println("Invalid") } -> Option CQuick Check:
Use commas for multiple matches and else for fallback [OK]
- Using || or && instead of commas
- Missing else for invalid inputs
- Not listing all weekdays explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
