Bool type and logical operators in Swift - Time & Space Complexity
When using Bool values and logical operators, it is important to see how the number of operations changes as we combine more conditions.
We want to know how the time to evaluate these expressions grows when we add more logical checks.
Analyze the time complexity of the following code snippet.
func checkAllTrue(values: [Bool]) -> Bool {
for value in values {
if !value {
return false
}
}
return true
}
This function checks if all Boolean values in an array are true by scanning each one until it finds a false.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each Boolean in the array.
- How many times: Up to once per element, stopping early if a false is found.
As the number of Booleans increases, the function may check more values, but it can stop early if a false appears.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows roughly in direct proportion to the number of values, but can be less if a false is found early.
Time Complexity: O(n)
This means the time to check all values grows linearly with the number of Booleans in the array.
[X] Wrong: "Logical operators always take constant time regardless of input size."
[OK] Correct: When used in a loop over many values, the total time depends on how many values are checked, so it grows with input size.
Understanding how logical checks scale helps you write efficient conditions and shows you can think about how code runs as data grows.
What if we changed the function to check if any value is true instead of all? How would the time complexity change?
Practice
Solution
Step 1: Understand Boolean values in Swift
Swift usestrueandfalseas Boolean values, not numbers or words.Step 2: Identify the valid Boolean value
Among the options, onlytrueis a valid Boolean value.Final Answer:
true -> Option DQuick Check:
Boolean values aretrueorfalse[OK]
- Confusing 1 or 0 as Boolean values
- Using words like 'yes' or 'no' instead of true/false
Solution
Step 1: Recall Swift logical AND syntax
Swift uses&&for logical AND, not a single ampersand or words.Step 2: Identify the correct syntax
a && buses&&, which is the correct logical AND operator.Final Answer:
a && b -> Option AQuick Check:
Logical AND in Swift is&&[OK]
- Using single & instead of &&
- Writing 'and' as a word
- Confusing AND with OR operator ||
let a = true let b = false print(a || b && !a)
Solution
Step 1: Evaluate the NOT operator
!a means NOT true, which is false.Step 2: Evaluate AND operation
b && !a is false && false, which is false.Step 3: Evaluate OR operation
a || (result of AND) is true || false, which is true.Final Answer:
true -> Option CQuick Check:
Logical operators follow precedence: NOT, AND, OR [OK]
- Ignoring operator precedence
- Evaluating OR before AND
- Confusing !a as true
let x = true let y = false let result = x & y
Solution
Step 1: Check logical AND operator usage
In Swift, logical AND uses&&, not a single&.Step 2: Identify the syntax error
Using&causes a compile error because it's a bitwise operator, not logical.Final Answer:
Using single & instead of && for logical AND -> Option BQuick Check:
Logical AND requires &&, not & [OK]
- Confusing bitwise & with logical &&
- Thinking semicolons are required in Swift
- Assuming let cannot be used for constants
let number = 15 // Which condition is correct?
Solution
Step 1: Understand the range condition
To check if number is between 10 and 20 inclusive, it must be greater or equal to 10 AND less or equal to 20.Step 2: Analyze each option
number >= 10 && number <= 20uses&&with inclusive comparisons, which is correct. Others use OR or exclusive comparisons, which are incorrect.Final Answer:
number >= 10 && number <= 20 -> Option AQuick Check:
Use AND to combine both range checks [OK]
- Using || instead of && for range
- Using > or < instead of >= or <=
- Mixing OR and AND incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
