Program stability concepts in Go - Time & Space Complexity
Program stability means how well a program keeps working correctly as input size changes.
We want to know how the program's speed changes when it handles more data.
Analyze the time complexity of the following code snippet.
func stableProcess(data []int) int {
result := 0
for _, value := range data {
if value%2 == 0 {
result += value
}
}
return result
}
This code sums all even numbers in a list of integers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the data list.
- How many times: Once for every item in the list.
As the list gets bigger, the program checks more numbers one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the input size.
[X] Wrong: "The program runs faster because it only adds even numbers."
[OK] Correct: The program still checks every number, so the time depends on the total list size, not just the even numbers.
Understanding how a program's speed changes with input size shows you can write stable and predictable code.
"What if we changed the code to sum only numbers greater than 100? How would the time complexity change?"
Practice
recover() in Go programs?Solution
Step 1: Understand panic and recover
Panic causes a program to stop running immediately unless recovered.Step 2: Role of recover()
Recover is used inside a deferred function to catch the panic and allow the program to continue.Final Answer:
To catch a panic and prevent the program from crashing -> Option CQuick Check:
recover() catches panic = C [OK]
- Thinking recover() handles normal errors
- Using recover() outside defer
- Confusing recover() with panic()
Solution
Step 1: Recall defer syntax
In Go, defer is a keyword placed before a function call to delay its execution.Step 2: Check each option
Only defer fmt.Println("Hello") uses 'defer' correctly before the function call without extra symbols.Final Answer:
defer fmt.Println("Hello") -> Option AQuick Check:
defer + function call = D [OK]
- Adding '=' after defer
- Using ':' or '->' after defer
- Not calling a function after defer
package main
import "fmt"
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from panic:", r)
}
}()
fmt.Println("Start")
panic("Something went wrong")
fmt.Println("End")
}Solution
Step 1: Trace program execution
"Start" is printed first. Then panic is called, which triggers the deferred function.Step 2: Deferred function recovers panic
The deferred anonymous function calls recover(), catches the panic, and prints the recovery message. Execution does not continue after panic call, so "End" is not printed.Final Answer:
Start Recovered from panic: Something went wrong -> Option DQuick Check:
panic caught by recover = A [OK]
- Thinking code after panic does not run
- Missing recover() inside defer
- Confusing panic message with normal print
package main
import "fmt"
func main() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
panic("Oops")
}Solution
Step 1: Understand recover usage
recover() only works if called inside a deferred function; here it is called directly in main.Step 2: Analyze code flow
Since recover() is not deferred, it cannot catch the panic, so the program will crash.Final Answer:
recover() must be called inside a deferred function -> Option AQuick Check:
recover inside defer = B [OK]
- Calling recover() directly without defer
- Thinking recover() catches errors, not panics
- Placing panic before recover without defer
Solution
Step 1: Check defer and recover placement
func main() { defer func() { if r := recover(); r != nil { fmt.Println("Panic caught:", r) } }() panic("Crash") fmt.Println("Program continues") } correctly defers a function that calls recover() to catch panic and print a message.Step 2: Verify program flow
After panic, deferred recover runs, preventing crash and allowing program to continue, executing code after panic.Final Answer:
Uses defer func() with recover() before panic("Crash") -> Option BQuick Check:
defer + recover to catch panic = A [OK]
- Calling recover() outside defer
- Placing defer after panic
- Not handling panic at all
Ready to prove it?
Pass the test and this pattern moves unknown
