Switch with compound cases in Swift - Time & Space Complexity
Let's see how the time it takes to run a switch statement with multiple cases changes as we add more cases.
We want to know how the program's work grows when checking several options in a switch.
Analyze the time complexity of the following code snippet.
let value = 3
switch value {
case 1, 2:
print("One or Two")
case 3, 4, 5:
print("Three to Five")
default:
print("Other")
}
This code checks the value against groups of numbers using compound cases in a switch statement.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch statement checks each case group one by one.
- How many times: It compares the value to each case group until it finds a match or reaches default.
As the number of case groups grows, the program checks more groups before finding a match.
| Input Size (number of case groups) | Approx. Operations (comparisons) |
|---|---|
| 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 case groups.
Time Complexity: O(n)
This means the time to find a matching case grows in a straight line as you add more case groups.
[X] Wrong: "Switch statements always run in constant time no matter how many cases there are."
[OK] Correct: Actually, 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 work helps you explain how your code handles many options efficiently and shows you think about performance clearly.
"What if we replaced the switch with a dictionary lookup? How would the time complexity change?"
Practice
What does a compound case in a Swift switch statement allow you to do?
Solution
Step 1: Understand compound cases
Compound cases let you list several values separated by commas in one case.Step 2: Recognize the benefit
This makes the code cleaner by avoiding repeated code for similar cases.Final Answer:
Match multiple values in a single case using commas -> Option DQuick Check:
Compound case = multiple values in one case [OK]
- Thinking compound cases combine multiple switches
- Believing compound cases skip default
- Confusing compound cases with if-else
Which of the following is the correct syntax for a compound case in Swift?
switch value {
case 1, 2, 3:
print("One to three")
default:
print("Other")
}Solution
Step 1: Recall Swift compound case syntax
Swift uses commas to separate multiple values in one case.Step 2: Check each option
case 1, 2, 3: uses commas correctly; others use semicolons, parentheses, or brackets incorrectly.Final Answer:
case 1, 2, 3: -> Option AQuick Check:
Commas separate values in compound case [OK]
- Using semicolons instead of commas
- Putting values inside parentheses or brackets
- Missing the colon after case
What will be printed by this Swift code?
let letter = "b"
switch letter {
case "a", "e", "i", "o", "u":
print("Vowel")
case "b", "c", "d":
print("Consonant")
default:
print("Other")
}Solution
Step 1: Identify the value of letter
The variable letter is "b".Step 2: Match letter in switch cases
"b" matches the second case with "b", "c", "d" which prints "Consonant".Final Answer:
Consonant -> Option CQuick Check:
"b" in second case = Consonant [OK]
- Confusing vowels and consonants
- Forgetting to include default case
- Assuming no output if no exact match
Find the error in this Swift switch statement:
let number = 3
switch number {
case 1, 2
print("One or Two")
case 3:
print("Three")
default:
print("Other")
}Solution
Step 1: Check syntax of each case
Each case must end with a colon (:). The first case is missing it.Step 2: Verify other parts
Default case is present; compound cases with integers are allowed; parentheses are not needed.Final Answer:
Missing colon after case 1, 2 -> Option AQuick Check:
Case lines must end with colon [OK]
- Forgetting colon after case
- Thinking parentheses are required
- Believing compound cases disallow integers
Given this Swift code, what will be the output?
let grade = "B"
switch grade {
case "A", "B", "C":
print("Pass")
case "D", "F":
print("Fail")
case "I", "W":
print("Incomplete or Withdrawn")
default:
print("Unknown grade")
}Solution
Step 1: Identify the value of grade
The variable grade is "B".Step 2: Match grade in compound cases
"B" matches the first case "A", "B", "C" which prints "Pass".Final Answer:
Pass -> Option BQuick Check:
"B" in first case = Pass [OK]
- Assuming "B" is a fail grade
- Ignoring default case
- Confusing case groups
Ready to prove it?
Pass the test and this pattern moves unknown
