Return values in Go - Time & Space Complexity
We want to understand how the time it takes to run code with return values changes as the input grows.
Specifically, how does returning values affect the work done by the program?
Analyze the time complexity of the following code snippet.
func sum(numbers []int) int {
total := 0
for _, num := range numbers {
total += num
}
return total
}
This function adds up all numbers in a list and returns the total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each number in the list once.
- How many times: Exactly once for each item in the input list.
As the list gets bigger, the function does more additions, one for each number.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the size of the input list.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input list gets bigger.
[X] Wrong: "Returning a value makes the function run slower by a lot."
[OK] Correct: Returning a single value at the end is very fast and does not add extra loops or big work.
Understanding how return values fit into time complexity helps you explain your code clearly and confidently in interviews.
"What if the function returned a list of all partial sums instead of a single total? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of return values
Return values are used to send results from a function back to the caller.Step 2: Identify the correct description
Only It sends a result back to the place where the function was called. correctly describes this purpose.Final Answer:
It sends a result back to the place where the function was called. -> Option DQuick Check:
Return value = sends result back [OK]
- Confusing return values with variable declarations
- Thinking return values name the function
- Assuming return values are comments
Solution
Step 1: Check function syntax for return type
In Go, the return type is specified after parentheses: func name() type { ... }Step 2: Verify each option
func add() int { return 5 } correctly declares a function returning int. Others have syntax errors.Final Answer:
func add() int { return 5 } -> Option AQuick Check:
Return type after () = int [OK]
- Placing return type before function name
- Omitting return type when returning a value
- Incorrect function syntax
func multiply(a, b int) int {
return a * b
}
func main() {
result := multiply(3, 4)
fmt.Println(result)
}Solution
Step 1: Check for required imports
The code uses fmt.Println(result) but lacks import "fmt", causing a compile error: undefined fmt.Step 2: Verify function logic
The multiply function correctly computes 3*4=12, but the program won't compile or run without the import.Final Answer:
Error: missing import -> Option CQuick Check:
fmt.Println needs import "fmt" [OK]
- Forgetting import "fmt"
- Computing 3*4=12 but ignoring missing import
- Assuming code runs without imports
func divide(a, b int) (int, int) {
return a / b
}Solution
Step 1: Check return type declaration
The function declares two return values (int, int) but returns only one value.Step 2: Identify mismatch in return statement
Returning a single value causes a compile error because two values are expected.Final Answer:
Function returns only one value but declares two return types. -> Option BQuick Check:
Return count must match declared types [OK]
- Returning fewer values than declared
- Wrong operator usage confusion
- Ignoring function signature
Solution
Step 1: Define function with two return types
The function must declare two return types (int, int) for sum and product.Step 2: Return sum and product as two values
Return both values separated by a comma: return a+b, a*b.Final Answer:
func calc(a, b int) (int, int) { return a+b, a*b } -> Option AQuick Check:
Two return values with matching types [OK]
- Returning multiple values with single return type
- Returning combined expression instead of two values
- Incorrect return type syntax
Ready to prove it?
Pass the test and this pattern moves unknown
