Relational operators in Go - Time & Space Complexity
Relational operators compare values to decide true or false. We want to see how long these comparisons take as input grows.
How does the number of comparisons change when we check many values?
Analyze the time complexity of the following code snippet.
package main
func countGreaterThan(nums []int, threshold int) int {
count := 0
for _, num := range nums {
if num > threshold {
count++
}
}
return count
}
This code counts how many numbers in a list are greater than a given threshold.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: One comparison per number using the > operator inside a loop.
- How many times: Once for each number in the input list.
Each number in the list is checked once, so the work grows directly with the list size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: Doubling the input doubles the number of comparisons.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items checked.
[X] Wrong: "Relational operators take longer as numbers get bigger."
[OK] Correct: The comparison time is the same no matter the number size; only the count of comparisons matters.
Understanding how simple comparisons scale helps you explain efficiency clearly and shows you know how loops affect performance.
"What if we added a nested loop to compare every number with every other number? How would the time complexity change?"
Practice
Solution
Step 1: Understand the meaning of relational operators
Relational operators compare two values and return true or false based on the comparison.Step 2: Identify the operator for equality
The operator '==' checks if two values are equal in Go.Final Answer:
== -> Option CQuick Check:
Equality operator = == [OK]
- Confusing '=' (assignment) with '==' (equality)
- Using '!=' for equality check
- Thinking '>' means equal
a is not equal to variable b in Go?Solution
Step 1: Recall Go syntax for not equal operator
In Go, the not equal operator is written as '!='.Step 2: Match the syntax with the options
a != b uses 'a != b', which is correct syntax for not equal in Go.Final Answer:
a != b -> Option DQuick Check:
Not equal operator = != [OK]
- Using '<>' which is not valid in Go
- Swapping '=' and '!' to '=!'
- Using JavaScript style '!==' which is invalid in Go
package main
import "fmt"
func main() {
x := 10
y := 20
fmt.Println(x > y)
}Solution
Step 1: Understand the comparison x > y
Variable x is 10 and y is 20. The expression checks if 10 is greater than 20.Step 2: Evaluate the expression
Since 10 is not greater than 20, the expression evaluates to false.Final Answer:
false -> Option AQuick Check:
10 > 20 = false [OK]
- Assuming 10 > 20 is true
- Confusing output with variable values
- Expecting a number instead of boolean
package main
import "fmt"
func main() {
a := 5
b := 10
if a = b {
fmt.Println("Equal")
}
}Solution
Step 1: Check the if condition syntax
The condition uses 'a = b' which is an assignment, not a comparison.Step 2: Correct the operator for comparison
To compare values, '==' must be used instead of '='.Final Answer:
The '=' operator is used instead of '==' in the if condition -> Option BQuick Check:
Use '==' for comparison, not '=' [OK]
- Using '=' instead of '==' in conditions
- Thinking parentheses are mandatory in Go if
- Assuming fmt.Println syntax is wrong
package main
import "fmt"
func main() {
a := 15
b := 15
c := 20
result := (a == b) && (c > b)
fmt.Println(result)
}Solution
Step 1: Evaluate the first comparison (a == b)
Both a and b are 15, so (a == b) is true.Step 2: Evaluate the second comparison (c > b)
c is 20 and b is 15, so (c > b) is true.Step 3: Combine with logical AND
Both conditions are true, so true && true is true.Final Answer:
true -> Option AQuick Check:
(15 == 15) && (20 > 15) = true [OK]
- Confusing && with || operator
- Thinking one true means overall false
- Expecting numeric output instead of boolean
Ready to prove it?
Pass the test and this pattern moves unknown
