Boolean values in PowerShell - Time & Space Complexity
We want to see how the time it takes to run a script with Boolean values changes as we work with more data.
Specifically, we ask: How does the script's work grow when checking many Boolean values?
Analyze the time complexity of the following code snippet.
# Create an array of Boolean values
$boolArray = @( $true, $false, $true, $false ) * 1000
# Count how many are True
$countTrue = 0
foreach ($value in $boolArray) {
if ($value) {
$countTrue++
}
}
Write-Output $countTrue
This code counts how many True values are in a list of Booleans.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each Boolean in the array.
- How many times: Once for every item in the array.
As the list of Booleans gets bigger, the script checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of Booleans.
Time Complexity: O(n)
This means the time to count True values grows in a straight line as the list gets longer.
[X] Wrong: "Checking Boolean values is instant and does not depend on list size."
[OK] Correct: Even though each check is fast, the script must check every item, so more items mean more total work.
Understanding how simple loops over Boolean values scale helps you explain how scripts handle data efficiently in real tasks.
"What if we used nested loops to compare each Boolean with every other Boolean? How would the time complexity change?"
Practice
Solution
Step 1: Recall PowerShell Boolean syntax
PowerShell uses special keywords starting with $ for Boolean values.Step 2: Identify correct Boolean values
The correct Boolean values are $true and $false, not plain True/False or numbers.Final Answer:
$true and $false -> Option AQuick Check:
Boolean values = $true, $false [OK]
- Using True/False without $
- Using 1 and 0 as Booleans
- Confusing Yes/No as Boolean
Solution
Step 1: Check assignment syntax in PowerShell
PowerShell uses = to assign values to variables, and variables start with $.Step 2: Identify correct Boolean value and syntax
The Boolean value must be $true with $ sign, and assignment uses =.Final Answer:
$flag = $true -> Option AQuick Check:
Assign Boolean with $true and = [OK]
- Missing $ on Boolean value
- Using := instead of =
- Missing $ on variable name
if ($false) { "Yes" } else { "No" }Solution
Step 1: Understand the if condition
The condition is $false, which means the if block will NOT run.Step 2: Determine which block runs
Since the if condition is false, the else block runs and outputs "No".Final Answer:
No -> Option CQuick Check:
if $false runs else block = No [OK]
- Thinking $false runs if block
- Expecting output $false instead of string
- Confusing syntax causing error
if ($True) { Write-Output "Yes" } else { Write-Output "No" }Solution
Step 1: Check Boolean case sensitivity
PowerShell Boolean values are case-insensitive, so $True works like $true.Step 2: Verify syntax correctness
The if statement syntax is correct with parentheses and Write-Output is valid.Final Answer:
No error, code runs fine -> Option DQuick Check:
PowerShell Booleans are case-insensitive [OK]
- Thinking $True is invalid case
- Expecting syntax error without parentheses
- Believing Write-Output can't be used here
Solution
Step 1: Understand Boolean checks in PowerShell
PowerShell treats variables as Boolean in if conditions directly, so if ($isReady) works.Step 2: Analyze other comparisons
Checking explicitly with -eq $true or -ne $false also works to test if $isReady is true.Step 3: Confirm all options are valid
All three ways correctly check if $isReady is true and run the block.Final Answer:
All of the above -> Option BQuick Check:
Multiple ways to test Boolean true in PowerShell [OK]
- Thinking only direct if ($isReady) works
- Confusing -eq $true with assignment
- Ignoring that -ne $false also works
Ready to prove it?
Pass the test and this pattern moves unknown
