Switch statement in Go - Time & Space Complexity
We want to understand how the time it takes to run a switch statement changes as the input changes.
Specifically, we ask: does the number of cases affect how long the switch takes?
Analyze the time complexity of the following code snippet.
func checkNumber(n int) string {
switch n {
case 1:
return "One"
case 2:
return "Two"
case 3:
return "Three"
default:
return "Other"
}
}
This code checks a number and returns a word based on which case matches.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The switch compares the input value to each case until it finds a match.
- How many times: At most, it checks each case once in order until a match is found.
As the number of cases grows, the switch may need to 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 checks grow roughly in direct proportion to 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: "A switch statement always runs in constant time no matter how many cases it has."
[OK] Correct: The switch checks cases one by one until it finds a match, so more cases can mean more checks and longer time.
Understanding how switch statements scale helps you explain code efficiency clearly and shows you think about how programs behave as they grow.
"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 the purpose of switch
A switch statement chooses one path among many based on a value.Step 2: Compare with other options
Loops repeat code, functions define behavior, variables store data. Switch picks cases.Final Answer:
It selects one case to run based on a value. -> Option AQuick Check:
Switch = Select case [OK]
- Confusing switch with loops
- Thinking switch defines functions
- Believing switch declares variables
Solution
Step 1: Recall Go switch syntax
Go usesswitch variable { case value: ... default: ... }without parentheses.Step 2: Check each option
switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } matches correct syntax. switch (x) { case 1: fmt.Println("One") default: fmt.Println("Other") } wrongly uses parentheses. switch x: case 1: fmt.Println("One") default: fmt.Println("Other") uses colon after switch. switch x { case 1 => fmt.Println("One") default => fmt.Println("Other") } uses => which is invalid.Final Answer:
switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } -> Option BQuick Check:
Correct syntax = switch x { case 1: fmt.Println("One") default: fmt.Println("Other") } [OK]
- Adding parentheses around switch variable
- Using colon after switch keyword
- Using => instead of colon in cases
package main
import "fmt"
func main() {
x := 2
switch x {
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("Three")
default:
fmt.Println("Other")
}
}Solution
Step 1: Identify the switch variable and value
The variablexis 2.Step 2: Match the case with value 2
The case2matches and prints "Two".Final Answer:
Two -> Option AQuick Check:
Switch x=2 prints "Two" [OK]
- Choosing default instead of matching case
- Confusing case numbers
- Ignoring switch variable value
package main
import "fmt"
func main() {
x := 3
switch x {
case 1
fmt.Println("One")
case 2:
fmt.Println("Two")
default:
fmt.Println("Other")
}
}Solution
Step 1: Check case syntax
Each case must end with a colon (:). Here,case 1misses the colon.Step 2: Verify other parts
Default case exists, switch variable can be int, fmt.Println is correct.Final Answer:
Missing colon after case 1 -> Option DQuick Check:
Case syntax requires colon [OK]
- Omitting colon after case
- Thinking default is mandatory
- Assuming switch variable must be string
Solution
Step 1: Group weekend days in one case
Usingcase 6, 7:groups Saturday and Sunday together.Step 2: Use default for other days
Default prints "Weekday" for days 1 to 5.Final Answer:
switch day { case 6, 7: fmt.Println("Weekend") default: fmt.Println("Weekday") } -> Option CQuick Check:
Multiple values in one case = switch day { case 6, 7: fmt.Println("Weekend") default: fmt.Println("Weekday") } [OK]
- Separating weekend days into different cases with wrong output
- Using logical OR inside switch expression incorrectly
- Missing default case for weekdays
Ready to prove it?
Pass the test and this pattern moves unknown
