Arithmetic operators in Ruby - Time & Space Complexity
When we use arithmetic operators in code, it is helpful to know how the time to run the code changes as the input size grows.
We want to find out how the number of calculations grows when we do arithmetic operations on different amounts of data.
Analyze the time complexity of the following code snippet.
numbers = [1, 2, 3, 4, 5]
result = []
numbers.each do |num|
doubled = num * 2
result << doubled
end
This code takes a list of numbers, doubles each number using multiplication, and stores the results in a new list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying each number by 2 inside a loop.
- How many times: Once for each number in the list.
As the list gets bigger, the number of multiplications grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: The number of operations grows directly with the size of the input list.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input list gets bigger.
[X] Wrong: "Multiplying numbers takes longer when the numbers themselves are bigger."
[OK] Correct: The time to multiply is generally considered constant for typical integer sizes in programming, so what matters is how many numbers you multiply.
Understanding how simple arithmetic inside loops affects time helps you explain how your code will behave with bigger data, a skill that shows clear thinking in interviews.
"What if we changed the code to multiply each number by every other number in the list? How would the time complexity change?"
Practice
Solution
Step 1: Recall basic arithmetic symbols in Ruby
Ruby uses standard arithmetic symbols: + for addition, - for subtraction, * for multiplication, / for division.Step 2: Identify the multiplication symbol
The symbol for multiplication is the asterisk (*).Final Answer:
* -> Option BQuick Check:
Multiplication = * [OK]
- Confusing + with multiplication
- Using / instead of *
- Using - instead of *
Solution
Step 1: Understand the modulus operator
The modulus operator (%) returns the remainder after division.Step 2: Identify the correct expression for remainder
10 % 3 calculates the remainder when 10 is divided by 3, which is 1.Final Answer:
10 % 3 -> Option DQuick Check:
Remainder = % [OK]
- Using / instead of %
- Using * or - which do not give remainder
- Confusing % with division
result = 15 / 4 puts result
Solution
Step 1: Understand integer division in Ruby
When dividing two integers, Ruby performs integer division, discarding the decimal part.Step 2: Calculate 15 divided by 4 as integer division
15 / 4 equals 3.75, but integer division returns 3.Final Answer:
3 -> Option AQuick Check:
Integer division truncates decimals = 3 [OK]
- Expecting float result without floats
- Confusing / with %
- Thinking output is 4 or 3.75
num = 10 result = num / 0 puts result
Solution
Step 1: Analyze the division operation
The code divides 10 by 0, which is mathematically undefined.Step 2: Understand Ruby's behavior on division by zero
Ruby raises a ZeroDivisionError when dividing by zero.Final Answer:
Division by zero error -> Option AQuick Check:
Dividing by zero causes error [OK]
- Thinking output is 0
- Assuming syntax error
- Ignoring runtime exceptions
Solution
Step 1: Calculate sum of numbers
Sum is 7 + 8 + 9 = 24.Step 2: Divide by 3 as float division
Dividing by 3.0 forces float division, giving 8.0 instead of integer 8.Final Answer:
(7 + 8 + 9) / 3.0 -> Option CQuick Check:
Use float divisor for float average [OK]
- Using integer division losing decimals
- Misusing % instead of /
- Wrong operator precedence without parentheses
Ready to prove it?
Pass the test and this pattern moves unknown
