Comparison operators in Ruby - Time & Space Complexity
When we use comparison operators, the computer checks values to decide something.
We want to see how the time to do these checks grows as we compare more items.
Analyze the time complexity of the following code snippet.
def count_greater_than(numbers, threshold)
count = 0
numbers.each do |num|
count += 1 if num > threshold
end
count
end
This code counts how many numbers in a list are bigger than a given threshold.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing each number to the threshold using > operator.
- How many times: Once for every number in the list.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons 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: "Comparison operators take the same time no matter how many items we check."
[OK] Correct: Each comparison happens for every item, so more items mean more comparisons and more time.
Understanding how comparisons add up helps you explain how your code handles bigger data smoothly.
"What if we changed the code to stop checking as soon as we find one number greater than the threshold? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of each operator
==checks equality,=assigns values,!=checks inequality, and>checks if left is greater than right.Step 2: Identify the equality operator
The operator that compares two values for equality is==.Final Answer:
==-> Option AQuick Check:
Equality operator = == [OK]
- Confusing assignment (=) with equality (==)
- Using != instead of ==
- Thinking > checks equality
a is not equal to variable b in Ruby?Solution
Step 1: Recall Ruby's not equal operator
Ruby uses!=to check if two values are not equal.Step 2: Check each option's syntax
a != bis correct.a =! bis invalid syntax.a <> bis not Ruby syntax.a !== bis JavaScript syntax, not Ruby.Final Answer:
a != b-> Option DQuick Check:
Not equal operator = != [OK]
- Using =! instead of !=
- Using !== which is JavaScript, not Ruby
- Using <> which is invalid in Ruby
a = 5
b = 10
puts a < b
Solution
Step 1: Understand the comparison
The code compares ifa(5) is less thanb(10).Step 2: Evaluate the comparison
Since 5 is less than 10, the expressiona < bis true.Final Answer:
true -> Option AQuick Check:
5 < 10 = true [OK]
- Confusing output with variable values
- Expecting numeric output instead of boolean
- Mixing up < and > operators
if x = 10
puts "x is 10"
end
Solution
Step 1: Analyze the if condition
The code uses=which assigns 10 tox, not compares.Step 2: Correct the comparison operator
To check ifxequals 10, use==instead of=.Final Answer:
Should use == to compare, not = -> Option CQuick Check:
Comparison needs ==, = is assignment [OK]
- Using = instead of ==
- Thinking missing end keyword causes error
- Assuming variable must be declared before use
n is between 10 and 20 inclusive in Ruby. Which expression correctly uses comparison operators to do this?Solution
Step 1: Understand the range check
We wantnto be at least 10 and at most 20, so both conditions must be true.Step 2: Evaluate each option
n >= 10 && n <= 20usesn >= 10 && n <= 20, which correctly checks both limits.10 <= n <= 20uses chained comparison which is invalid in Ruby.n > 10 || n < 20uses OR (||), which allows values outside the range.n == 10 && n == 20checks ifnequals both 10 and 20 simultaneously, which is impossible.Final Answer:
n >= 10 && n <= 20 -> Option BQuick Check:
Use && to combine two comparisons [OK]
- Trying chained comparisons like 10 <= n <= 20
- Using || instead of && for range
- Checking equality to both ends simultaneously
Ready to prove it?
Pass the test and this pattern moves unknown
