Switch expression behavior in Go - Time & Space Complexity
We want to understand how the time it takes to run a switch expression changes as the number of cases grows.
How does adding more cases affect the work the program does?
Analyze the time complexity of the following code snippet.
func checkValue(x int) string {
switch x {
case 1:
return "One"
case 2:
return "Two"
case 3:
return "Three"
default:
return "Other"
}
}
This function uses a switch to return a string based on the input number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch checks each case one by one until it finds a match.
- How many times: In the worst case, it checks all cases once.
As the number of cases grows, the program may check more cases before finding a match.
| Number of Cases (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 roughly in a straight line with the number of cases.
Time Complexity: O(n)
This means the time to find the matching case grows linearly with the number of cases.
[X] Wrong: "Switch statements always run in constant time no matter how many cases there are."
[OK] Correct: The switch checks cases one by one, so more cases mean more checks in the worst case.
Understanding how switch statements scale helps you explain code efficiency clearly and shows you think about how programs work under the hood.
"What if the switch used a map lookup instead of cases? How would the time complexity change?"
Practice
switch statement do in Go?Solution
Step 1: Understand switch purpose
A switch statement selects one matching case based on a value or condition.Step 2: Confirm behavior
It stops after the first matching case, so only one case runs.Final Answer:
It chooses one case to run based on a value or condition. -> Option BQuick Check:
Switch picks one matching case [OK]
- Thinking switch runs all cases
- Believing switch only works with numbers
- Assuming switch repeats cases automatically
Solution
Step 1: Recall Go switch syntax
Go usesswitch value { case ... }without parentheses or extra keywords.Step 2: Check options
switch value { case 1: doSomething() default: doDefault() } matches correct syntax; others add invalid keywords or parentheses.Final Answer:
switch value { case 1: doSomething() default: doDefault() } -> Option DQuick Check:
Go switch uses braces, no parentheses [OK]
- Adding parentheses around switch value
- Using 'then' or 'do' keywords incorrectly
- Misplacing braces or colons
package main
import "fmt"
func main() {
x := 3
switch x {
case 1:
fmt.Println("One")
case 2, 3:
fmt.Println("Two or Three")
default:
fmt.Println("Other")
}
}Solution
Step 1: Identify switch value and cases
Variable x is 3. The switch checks cases 1, then 2 or 3, then default.Step 2: Match case
Case 2, 3 matches because x is 3, so it prints "Two or Three".Final Answer:
Two or Three -> Option CQuick Check:
3 matches case 2,3 [OK]
- Ignoring multiple values in one case
- Choosing default instead of matching case
- Assuming case 3 is separate from case 2
package main
import "fmt"
func main() {
x := 5
switch {
case x == 1:
fmt.Println("One")
case x == 5:
fmt.Println("Five")
case x == 10:
fmt.Println("Ten")
}
}Solution
Step 1: Understand switch without expression
Switch without a value allows cases to be conditions (boolean expressions).Step 2: Check cases and output
x is 5, so case x == 5 is true, printing "Five". No error occurs.Final Answer:
No error; code runs and prints "Five" -> Option AQuick Check:
Switch without value uses conditions [OK]
- Thinking default is mandatory
- Assuming switch must have a value
- Believing case conditions are invalid
package main
import "fmt"
func main() {
score := 75
switch {
case score >= 90:
fmt.Println("Grade A")
case score >= 80:
fmt.Println("Grade B")
case score >= 70:
fmt.Println("Grade C")
default:
fmt.Println("Fail")
}
}Solution
Step 1: Analyze switch without expression
Switch uses conditions in cases, evaluated top to bottom.Step 2: Evaluate conditions for score = 75
score >= 90 is false, score >= 80 is false, score >= 70 is true, so prints "Grade C" and stops.Final Answer:
Grade C -> Option AQuick Check:
First true case runs, here score >= 70 [OK]
- Ignoring order of cases
- Assuming all true cases print
- Confusing >= with >
Ready to prove it?
Pass the test and this pattern moves unknown
