Logical operators in Go - Time & Space Complexity
Logical operators combine true or false values to decide outcomes.
We want to see how using these operators affects how long the program takes as inputs grow.
Analyze the time complexity of the following code snippet.
func checkValues(values []bool) bool {
result := false
for _, v := range values {
result = result || v
}
return result
}
This code checks if any value in a list is true using the logical OR operator.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each element in the list once.
- How many times: Exactly once for each item in the input list.
As the list gets longer, the program checks more values one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of items.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the list gets bigger.
[X] Wrong: "Logical operators make the code run instantly regardless of input size."
[OK] Correct: Even with logical operators, the program still checks each item one by one, so time grows with input size.
Understanding how logical operators affect loops helps you explain how your code handles data efficiently.
"What if we stop checking as soon as we find a true value? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of logical AND
The AND operator returns true only when both conditions it connects are true.Step 2: Identify Go's AND operator
In Go, the AND operator is written as &&, which matches the description.Final Answer:
&& -> Option DQuick Check:
AND = && [OK]
- Confusing & (bitwise AND) with && (logical AND)
- Mixing OR (||) with AND (&&)
- Using ! as AND operator
Solution
Step 1: Recall Go syntax for logical NOT
In Go, the logical NOT operator is a single exclamation mark placed before the variable or expression.Step 2: Match options with correct syntax
!x uses !x, which is the correct syntax for NOT in Go.Final Answer:
!x -> Option BQuick Check:
NOT = ! [OK]
- Writing 'not x' like in some other languages
- Using ~ which is bitwise NOT, not logical NOT
- Placing ! after the variable
package main
import "fmt"
func main() {
a := true
b := false
fmt.Println(a && !b || b)
}Solution
Step 1: Evaluate !b
b is false, so !b is true.Step 2: Evaluate a && !b
a is true and !b is true, so a && !b is true.Step 3: Evaluate (a && !b) || b
true || false is true.Final Answer:
true -> Option AQuick Check:
True AND NOT False OR False = True [OK]
- Evaluating OR before AND
- Forgetting to negate b
- Assuming false || true is false
package main
import "fmt"
func main() {
x := true
y := false
if x & y {
fmt.Println("Both true")
}
}Solution
Step 1: Check logical operators in if condition
The code uses & which is bitwise AND, not logical AND.Step 2: Identify correct operator for boolean logic
Logical AND in Go is &&, so & causes a type error here.Final Answer:
Using & instead of && in if condition -> Option AQuick Check:
Logical AND needs &&, not & [OK]
- Using & instead of && for booleans
- Assuming & works like && in conditions
- Ignoring compiler error messages
score (int), which condition correctly checks if score is between 50 and 100 inclusive using logical operators in Go?Solution
Step 1: Understand the range condition
We want score to be at least 50 and at most 100, so both conditions must be true.Step 2: Choose correct logical operator
AND (&&) combines conditions that both must be true; OR (||) would allow values outside the range.Step 3: Check inclusivity
Using >= and <= includes 50 and 100 themselves.Final Answer:
score >= 50 && score <= 100 -> Option CQuick Check:
Between means AND, not OR [OK]
- Using || which allows values outside range
- Using > and < excludes boundary values
- Confusing AND and OR logic
Ready to prove it?
Pass the test and this pattern moves unknown
