Testing failsafe scenarios in Drone Programming - Time & Space Complexity
When testing failsafe scenarios in drone programming, it is important to know how the time to run these tests changes as the number of scenarios grows.
We want to understand how the testing effort scales with more failsafe checks.
Analyze the time complexity of the following code snippet.
function testFailsafeScenarios(scenarios) {
for (let i = 0; i < scenarios.length; i++) {
runTest(scenarios[i]);
}
}
function runTest(scenario) {
// simulate checking one failsafe condition
checkFailsafe(scenario);
}
function checkFailsafe(scenario) {
// simple check logic here
}
This code runs a test for each failsafe scenario in a list, checking each one once.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that runs through all failsafe scenarios.
- How many times: Once for each scenario in the input list.
As the number of failsafe scenarios increases, the total tests run increase at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 tests |
| 100 | 100 tests |
| 1000 | 1000 tests |
Pattern observation: Doubling the number of scenarios doubles the work needed.
Time Complexity: O(n)
This means the time to test grows directly in proportion to the number of failsafe scenarios.
[X] Wrong: "Testing more scenarios only adds a little extra time, so it's almost constant."
[OK] Correct: Each scenario requires a separate test, so time grows steadily as scenarios increase, not staying almost the same.
Understanding how testing time grows helps you plan and explain your approach clearly, showing you think about real-world limits and efficiency.
"What if each failsafe scenario required running multiple checks inside it? How would the time complexity change?"
Practice
Solution
Step 1: Understand failsafe scenarios
Failsafe scenarios are tests to check how the drone reacts to problems like lost connection or low battery.Step 2: Identify the main goal
The main goal is to keep the drone safe and avoid crashes during these problems.Final Answer:
To ensure the drone handles problems safely and avoids crashes -> Option CQuick Check:
Failsafe = safety first [OK]
- Confusing failsafe with performance improvements
- Thinking failsafe adds new features
- Assuming failsafe makes drone fly higher
Solution
Step 1: Understand the battery check condition
Low battery means battery level is less than a safe threshold, here 20%.Step 2: Match condition to code
Only 'if battery_level < 20:' correctly checks for low battery to start landing.Final Answer:
if battery_level < 20: initiate_landing() -> Option AQuick Check:
Low battery = less than 20% [OK]
- Using greater than instead of less than
- Checking for exact battery value
- Using not equal instead of less than
connection_status = "lost"
if connection_status == "lost":
print("Return to home activated")
else:
print("Continue flying")Solution
Step 1: Check the value of connection_status
The variable connection_status is set to "lost".Step 2: Evaluate the if condition
The condition 'connection_status == "lost"' is true, so the first print runs.Final Answer:
Return to home activated -> Option AQuick Check:
Lost connection triggers return home [OK]
- Confusing else output with if output
- Assuming no output if condition true
- Thinking code has syntax error
gps_signal = False
if gps_signal = False:
emergency_landing()Solution
Step 1: Identify the if condition syntax
The code uses 'if gps_signal = False:', which assigns False instead of comparing.Step 2: Correct the comparison operator
It should be 'if gps_signal == False:' to check the value properly.Final Answer:
Using assignment '=' instead of comparison '==' in the if condition -> Option DQuick Check:
Use '==' for comparison, not '=' [OK]
- Confusing '=' and '==' in conditions
- Forgetting parentheses on function calls
- Assuming variable is undefined
battery_level = 10
gps_signal = False
# Choose the correct condition to trigger landing
if _____:
emergency_landing()Solution
Step 1: Understand the condition requirements
The drone should land if battery is below 15% OR GPS signal is lost (False).Step 2: Match logical operator to condition
Use 'or' because either condition triggers landing, so 'battery_level < 15 or gps_signal == False'.Final Answer:
battery_level < 15 or gps_signal == False -> Option BQuick Check:
Use OR for either condition triggers landing [OK]
- Using AND instead of OR
- Checking for battery > 15 instead of < 15
- Confusing GPS signal True/False meaning
Ready to prove it?
Pass the test and this pattern moves unknown
