String freezing for immutability in Ruby - Time & Space Complexity
We want to understand how freezing a string affects the time it takes to run code that uses that string.
Specifically, does making a string unchangeable change how long operations take as the string gets bigger?
Analyze the time complexity of the following code snippet.
str = "hello world"
str.freeze
10.times do
puts str
end
This code freezes a string to make it unchangeable, then prints it 10 times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Printing the string 10 times inside a loop.
- How many times: 10 times, fixed number, not depending on string size.
Printing the string depends on the string length, but the loop count is fixed at 10.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 prints of 10 characters each |
| 100 | About 10 prints of 100 characters each |
| 1000 | About 10 prints of 1000 characters each |
Pattern observation: The total work grows linearly with the string length, but the number of repeats is constant.
Time Complexity: O(n)
This means the time grows in direct proportion to the string length, regardless of freezing.
[X] Wrong: "Freezing a string makes all operations on it instant or faster."
[OK] Correct: Freezing only stops changes; it does not speed up reading or printing the string, which still depends on string size.
Understanding how immutability affects performance helps you write safer code without surprises about speed.
"What if we changed the loop to print the string n times instead of 10? How would the time complexity change?"
Practice
str = "hello".freezeSolution
Step 1: Understand the freeze method
Thefreezemethod in Ruby makes an object immutable, meaning it cannot be changed after freezing.Step 2: Apply to strings
When applied to a string, it prevents any further modifications to that string's content.Final Answer:
Makes the string unchangeable (immutable). -> Option CQuick Check:
freeze means immutable = A [OK]
- Thinking freeze deletes the string
- Assuming freeze changes string content
- Believing freeze speeds up modification
Solution
Step 1: Review Ruby freeze syntax
The correct syntax to freeze a string is to call.freezeon the string object, like"hello".freeze.Step 2: Check each option
str = "hello".freeze correctly calls.freezeon the string literal. Options B, C, and D use invalid syntax or incorrect method calls.Final Answer:
str = "hello".freeze -> Option BQuick Check:
Call .freeze on string = A [OK]
- Trying to assign freeze as a variable
- Using freeze as a standalone keyword
- Calling freeze as a function with parentheses
str = "hello".freeze str << " world"
Solution
Step 1: Understand string freezing effect
Freezing the string"hello"makes it immutable, so any modification attempt will raise an error.Step 2: Analyze the << operation
The<<operator tries to append " world" to the frozen string, which is not allowed and causes a runtime error.Final Answer:
A runtime error occurs: can't modify frozen String. -> Option DQuick Check:
Modifying frozen string = error C [OK]
- Assuming string changes silently
- Thinking string resets to new value
- Believing no error occurs
name = "Alice" name.freeze name.upcase!
Solution
Step 1: Check freeze usage
Thefreezemethod is correctly called on the stringname, making it immutable.Step 2: Understand upcase! effect
Theupcase!method tries to change the string in place, which is not allowed on a frozen string, causing a runtime error.Final Answer:
Error because upcase! tries to modify a frozen string. -> Option AQuick Check:
Modifying frozen string with upcase! = error B [OK]
- Thinking freeze is called incorrectly
- Assuming no error on upcase!
- Confusing variable type
GREETING = "Welcome!".freeze
Why is freezing important here?
Solution
Step 1: Understand constant usage
Constants likeGREETINGshould not change during program execution to avoid bugs.Step 2: Role of freeze on constants
Freezing the string ensures it cannot be modified accidentally, protecting the constant's value.Final Answer:
It prevents accidental changes to the greeting, ensuring data integrity. -> Option AQuick Check:
Freeze constant for safety = D [OK]
- Thinking freeze speeds up printing
- Believing freeze allows changes later
- Assuming freeze compresses data
Ready to prove it?
Pass the test and this pattern moves unknown
