Battery failsafe in Drone Programming - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to check the battery status grows as the number of checks increases.
How does the program's running time change when it repeatedly monitors the battery?
Analyze the time complexity of the following code snippet.
function batteryFailsafe(batteryLevels) {
for (let level of batteryLevels) {
if (level < 20) {
triggerFailsafe()
break
}
}
}
function triggerFailsafe() {
// Actions to safely land the drone
}
This code checks each battery level in a list and triggers a failsafe if any level is below 20%.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the batteryLevels list.
- How many times: Up to once for each battery level until a low level is found or the list ends.
As the number of battery levels increases, the program may check more items.
| 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 battery levels.
Time Complexity: O(n)
This means the time to check battery levels grows linearly with the number of levels to check.
[X] Wrong: "The failsafe check always takes the same time no matter how many battery levels there are."
[OK] Correct: The program may need to check many battery levels before finding a low one or finishing the list, so time grows with input size.
Understanding how loops affect time helps you explain how your code handles real-world data sizes efficiently.
"What if the code checked battery levels in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the battery failsafe concept and identify the correct purpose
The battery failsafe is designed to protect the drone from running out of power mid-flight. Preventing crashes by triggering safe actions like landing or returning home when battery is low is the main goal.Final Answer:
To prevent drone crashes by acting when battery is low -> Option AQuick Check:
Failsafe = prevent crashes [OK]
- Confusing failsafe with speed control
- Thinking it improves camera quality
- Assuming it manages Wi-Fi connections
Solution
Step 1: Identify the comparison operator and confirm syntax for less than
The symbol '<' means less than, so 'battery_level < 20' checks if battery is below 20%. Using a single '=' is assignment, '==' is equality check, but we want less than, so '<' is correct.Final Answer:
if battery_level < 20: -> Option CQuick Check:
Less than 20% check = if battery_level < 20 [OK]
- Using '=' instead of '<' for comparison
- Using '>' which checks above threshold
- Confusing '==' with less than operator
battery_level = 15
if battery_level < 20:
action = 'Return to home'
else:
action = 'Continue flying'
print(action)Solution
Step 1: Check battery_level value, condition, and determine action
battery_level is 15, which is less than 20, so the condition is true. Since condition is true, action is set to 'Return to home'. The else block is skipped.Final Answer:
Return to home -> Option DQuick Check:
battery_level 15 < 20 triggers 'Return to home' [OK]
- Assuming else block runs when condition is true
- Confusing '<' with '>' operator
- Expecting syntax error from correct code
if battery_level = 15:
trigger_landing()Solution
Step 1: Check if statement syntax and confirm other elements
The '=' sign is used for assignment, not comparison. For comparison, '==' is needed. The colon ':' is present, function name looks valid, and battery_level should be a number, not string.Final Answer:
Using '=' instead of '==' in condition -> Option BQuick Check:
Use '==' to compare values in if [OK]
- Confusing '=' with '==' in conditions
- Ignoring missing colon errors
- Assuming variable type must be string
Solution
Step 1: Analyze battery level conditions and match to code options
Battery below 15% means battery_level < 15 triggers landing. Between 15% and 25% means battery_level >= 15 and battery_level <= 25 triggers return home.
if battery_level < 15:
land()
elif battery_level >= 15 and battery_level <= 25:
return_home() correctly uses conditions. Others mix conditions or reverse actions.Final Answer:
if battery_level < 15: land() elif battery_level >= 15 and battery_level <= 25: return_home() -> Option AQuick Check:
Correct ranges and actions match if battery_level < 15: land() elif battery_level >= 15 and battery_level <= 25: return_home() [OK]
- Swapping landing and return actions
- Using wrong comparison operators
- Not covering full battery range properly
