Let for constants (immutable) in Swift - Time & Space Complexity
Let's explore how using constants with let affects the time complexity of Swift code.
We want to see if making values unchangeable changes how long the program takes to run.
Analyze the time complexity of the following code snippet.
let numbers = [1, 2, 3, 4, 5]
let doubled = numbers.map { $0 * 2 }
print(doubled)
This code creates a constant array and then makes a new array by doubling each number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
mapfunction loops over each element in the array. - How many times: It runs once for every item in the array.
As the array gets bigger, the number of times the doubling happens grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Using let makes the code run faster because values don't change."
[OK] Correct: The time depends on how many items you process, not on whether values are constant or not.
Understanding how constants affect performance helps you write clear and predictable code, a skill valued in many coding challenges.
"What if we changed let to var and modified the array inside the loop? How would the time complexity change?"
Practice
let keyword do in Swift?Solution
Step 1: Understand the purpose of
Theletletkeyword is used to declare constants in Swift, meaning the value assigned cannot be changed later.Step 2: Compare with other keywords
Unlikevar, which declares variables that can change,letensures immutability.Final Answer:
Creates a constant value that cannot be changed -> Option CQuick Check:
letmeans constant = A [OK]
- Confusing let with var
- Thinking let declares variables
- Assuming let can be reassigned
age with value 30 in Swift?Solution
Step 1: Identify correct Swift syntax for constants
In Swift,letis used to declare constants, solet age = 30is correct.Step 2: Check other options
vardeclares a variable, not a constant.constantandconstare not valid Swift keywords.Final Answer:
let age = 30 -> Option AQuick Check:
Useletfor constants = B [OK]
- Using var instead of let for constants
- Using non-Swift keywords like const
- Syntax errors with declaration
let number = 10 // number = 20 print(number)
Solution
Step 1: Understand the code behavior
The constantnumberis set to 10. The line trying to change it to 20 is commented out, so it does not run.Step 2: Determine the printed value
Sincenumberremains 10,print(number)outputs 10.Final Answer:
10 -> Option AQuick Check:
Constant unchanged prints original value = 10 [OK]
- Thinking commented code runs
- Expecting reassignment to work
- Confusing output with attempted change
let pi = 3.14 pi = 3.14159 print(pi)
Solution
Step 1: Check reassignment of constant
The code tries to assign a new value topi, which was declared withletand is immutable.Step 2: Understand Swift's error
Swift does not allow changing a constant's value, so this causes a compile-time error.Final Answer:
Compile-time error: cannot assign to value: 'pi' is a 'let' constant -> Option BQuick Check:
Reassigning let constant causes compile error = D [OK]
- Trying to reassign let constants
- Expecting runtime error instead of compile error
- Ignoring error messages
Solution
Step 1: Choose correct keyword for constant
Usingletdeclares a constant that cannot be changed after assignment.Step 2: Check code correctness
let daysInWeek = 7 // daysInWeek cannot be changed laterdeclares and assignsdaysInWeekwith 7 and prevents changes later.var daysInWeek = 7 // Later changed to 8 accidentallyusesvarwhich allows changes.constant daysInWeek = 7 // Prevents changesuses invalid keyword.let daysInWeek // Assign value laterdeclares without assignment, which is invalid for constants.Final Answer:
let daysInWeek = 7 // daysInWeek cannot be changed later -> Option DQuick Check:
Use let with assignment to make constant = A [OK]
- Using var instead of let for constants
- Trying to declare let without initial value
- Using non-Swift keywords like constant
Ready to prove it?
Pass the test and this pattern moves unknown
