Computed properties in Swift - Time & Space Complexity
Let's see how the time it takes to get a computed property changes as the input changes.
We want to know how the work grows when we access a computed property that does some calculation.
Analyze the time complexity of the following code snippet.
struct Rectangle {
var width: Int
var height: Int
var area: Int {
return width * height
}
}
let rect = Rectangle(width: 10, height: 5)
print(rect.area)
This code defines a rectangle and calculates its area using a computed property.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying two numbers (width and height).
- How many times: Once each time the computed property is accessed.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications (if accessed 10 times) |
| 100 | 100 multiplications (if accessed 100 times) |
| 1000 | 1000 multiplications (if accessed 1000 times) |
Pattern observation: Each access does a fixed small calculation, so the total work grows directly with how many times you ask for the property.
Time Complexity: O(1)
This means each time you get the computed property, it takes the same small amount of time, no matter the size of the input.
[X] Wrong: "Computed properties always take longer if the numbers are bigger."
[OK] Correct: The size of the numbers does not affect how many steps the computer takes to multiply them; it's a single operation each time.
Understanding how computed properties work helps you explain how your code runs efficiently and clearly, a skill valued in real projects and interviews.
"What if the computed property performed a loop over an array inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand property types in Swift
Stored properties hold fixed values, while computed properties calculate values dynamically.Step 2: Identify computed property behavior
Computed properties use code to return a value each time they are accessed instead of storing it.Final Answer:
A property that calculates its value dynamically when accessed -> Option CQuick Check:
Computed property = dynamic value calculation [OK]
- Confusing computed properties with stored properties
- Thinking computed properties store values permanently
- Assuming computed properties cannot have setters
Solution
Step 1: Recall computed property syntax
Computed properties use curly braces with a getter that returns a value.Step 2: Check each option
var area: Double { return width * height } correctly uses braces and return keyword. var area: Double = width * height is stored property syntax. var area: Double { width * height } misses return keyword. var area: Double get { width * height } has incorrect getter syntax.Final Answer:
var area: Double { return width * height } -> Option DQuick Check:
Computed property getter uses braces and return [OK]
- Omitting the return keyword inside getter
- Using equals sign instead of braces
- Incorrect getter syntax without braces
struct Rectangle {
var width: Double
var height: Double
var area: Double {
return width * height
}
}
let rect = Rectangle(width: 3, height: 4)
print(rect.area)
What is the output?Solution
Step 1: Understand the struct and computed property
Rectangle has width=3 and height=4. The computed property area returns width * height.Step 2: Calculate the area value
3 * 4 = 12.0, so rect.area returns 12.0.Final Answer:
12.0 -> Option BQuick Check:
3 * 4 = 12.0 [OK]
- Confusing stored and computed properties
- Expecting area to be zero by default
- Thinking code causes compilation error
struct Circle {
var radius: Double
var diameter: Double {
get {
return radius * 2
}
set {
radius = newValue / 2
}
}
}
let c = Circle(radius: 5)
c.diameter = 20
print(c.radius)Solution
Step 1: Analyze variable mutability
Variable 'c' is declared with 'let', making it immutable (constant).Step 2: Understand setter behavior
Setter tries to modify 'radius' property, but 'c' is constant, so assignment is not allowed.Final Answer:
Cannot assign to property 'radius' because 'c' is a constant -> Option AQuick Check:
Constant instance cannot change properties via setter [OK]
- Ignoring that 'let' makes instance immutable
- Thinking setter syntax is wrong
- Missing return keyword confusion
Temperature that stores a temperature in Celsius and has a computed property fahrenheit to get and set the temperature in Fahrenheit. Which implementation correctly updates Celsius when Fahrenheit is set?Solution
Step 1: Recall temperature conversion formulas
Fahrenheit = Celsius * 9/5 + 32; Celsius = (Fahrenheit - 32) * 5/9.Step 2: Check each option's formulas
struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } uses correct formulas in getter and setter. The other options use incorrect formulas.Final Answer:
The implementation with correct conversion formulas -> Option AQuick Check:
Correct formulas = struct Temperature { var celsius: Double var fahrenheit: Double { get { return celsius * 9 / 5 + 32 } set { celsius = (newValue - 32) * 5 / 9 } } } [OK]
- Mixing up Celsius and Fahrenheit formulas
- Using addition instead of multiplication
- Forgetting to update Celsius in setter
Ready to prove it?
Pass the test and this pattern moves unknown
