No implicit fallthrough in switch in Swift - Time & Space Complexity
We want to understand how the time it takes to run a switch statement changes as we add more cases.
Specifically, does adding more cases make the program slower, and how much slower?
Analyze the time complexity of the following code snippet.
let value = 3
switch value {
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
case 4:
print("Four")
default:
print("Other")
}
This code checks the value and prints a message depending on which case matches.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement compares the input value to each case until it finds a match.
- How many times: In the worst case, it checks each case once until it finds the right one or reaches default.
As the number of cases increases, the number of comparisons grows roughly in a straight line.
| Input Size (number of cases) | Approx. Comparisons |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The time to find the matching case grows linearly with the number of cases.
Time Complexity: O(n)
This means the time to run the switch grows in direct proportion to the number of cases.
[X] Wrong: "Switch statements always run in constant time no matter how many cases there are."
[OK] Correct: In Swift, the switch checks cases one by one until it finds a match, so more cases mean more checks and longer time.
Understanding how switch statements scale helps you write efficient code and explain your choices clearly in interviews.
"What if the switch used pattern matching with ranges instead of individual cases? How would the time complexity change?"
Practice
fallthrough in a Swift switch statement?Solution
Step 1: Understand Swift switch behavior
Swift switch statements do not automatically continue to the next case after a match.Step 2: Role of fallthrough keyword
To continue to the next case, you must explicitly use thefallthroughkeyword.Final Answer:
Only the matched case runs; no automatic fallthrough to the next case. -> Option AQuick Check:
No implicit fallthrough = matched case only [OK]
- Assuming all cases run after a match like in C
- Forgetting to add fallthrough when needed
- Expecting runtime errors without fallthrough
- Thinking switch ignores matched cases
switch?Solution
Step 1: Identify the keyword for fallthrough
Swift uses the keywordfallthroughto continue to the next case.Step 2: Eliminate incorrect options
continueis for loops, commas separate cases but don't cause fallthrough, and Swift does not do it automatically.Final Answer:
Use the keywordfallthroughinside the case. -> Option CQuick Check:
Explicit fallthrough keyword = fallthrough [OK]
- Confusing fallthrough with continue
- Using commas to join cases expecting fallthrough
- Assuming automatic fallthrough without keyword
- Using break to continue to next case
let number = 2
switch number {
case 1:
print("One")
case 2:
print("Two")
fallthrough
case 3:
print("Three")
default:
print("Other")
}Solution
Step 1: Identify matched case
The value 2 matchescase 2, so it prints "Two".Step 2: Check for fallthrough
Sincefallthroughis used, execution continues tocase 3, printing "Three".Final Answer:
Two Three -> Option BQuick Check:
fallthrough causes next case to run = Two and Three [OK]
- Ignoring fallthrough and printing only "Two"
- Printing "Three" only without "Two"
- Expecting default case to run
- Assuming all cases print
switch statement:let value = 3
switch value {
case 1:
print("One")
case 2:
print("Two")
case 3:
print("Three")
fallthrough
case 4:
print("Four")
fallthrough
case 5:
print("Five")
}Solution
Step 1: Exhaustiveness requirement
SwiftswitchonIntmust be exhaustive: cover all possible values or adddefault. Cases 1-5 don't cover allInts, nodefault: compile error.Step 2: Fallthrough usage is valid
Fallthroughs in cases 3 and 4 are allowed; no fallthrough in case 5 (correct); multiples OK, no runtime issues.Final Answer:
Missing default case causes a compile error. -> Option AQuick Check:
Non-exhaustive switch on Int = compile error [OK]
- Picking A: no fallthrough in last case
- Picking B: multiple fallthroughs allowed
- Picking C: ignores compile-time exhaustiveness error
- Thinking value=3 makes it exhaustive
switch. Which code correctly uses fallthrough to achieve this?let num = 3
switch num {
case 1:
print(1)
fallthrough
case 2:
print(2)
fallthrough
case 3:
print(3)
fallthrough
case 4:
print(4)
fallthrough
case 5:
print(5)
default:
print("Done")
}Solution
Step 1: Identify matched case and fallthrough chain
When num is 3,case 3matches and prints 3, then fallthrough continues to cases 4 and 5 printing 4 and 5.Step 2: Confirm output and default usage
Default is not reached because cases 3 to 5 cover the fallthrough chain; no error occurs.Final Answer:
This code prints 3, 4, 5 when num is 3. -> Option DQuick Check:
Fallthrough chains print from matched case to end [OK]
- Expecting only matched case to print
- Thinking default runs after fallthrough chain
- Assuming compile error from fallthrough in default
- Believing all cases print regardless of match
Ready to prove it?
Pass the test and this pattern moves unknown
