Arming and disarming sequence in Drone Programming - Time & Space Complexity
We want to understand how the time it takes to arm or disarm a drone changes as the number of checks or steps increases.
How does the process time grow when we add more safety checks or commands?
Analyze the time complexity of the following code snippet.
function armDrone(checks) {
for (let i = 0; i < checks.length; i++) {
if (!checks[i].passed()) {
return false;
}
}
setMotors(true);
return true;
}
function disarmDrone() {
setMotors(false);
return true;
}
This code arms the drone by running through a list of safety checks one by one. If all pass, it turns on the motors. Disarming just turns off the motors.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list of safety checks in
armDrone. - How many times: Once for each check in the list, stopping early if a check fails.
As the number of safety checks grows, the time to arm the drone grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The time grows directly with the number of checks, so doubling checks roughly doubles the time.
Time Complexity: O(n)
This means the time to arm the drone grows in a straight line with the number of safety checks.
[X] Wrong: "Disarming takes the same time as arming because both turn motors on or off."
[OK] Correct: Disarming just switches motors off once, so it takes constant time, unlike arming which checks many conditions.
Understanding how loops affect time helps you explain how real drone systems handle safety efficiently and reliably.
"What if the safety checks were nested inside another loop that runs multiple times? How would the time complexity change?"
Practice
arming a drone do in the flight sequence?Solution
Step 1: Understand the meaning of arming
Arming means getting the drone ready to fly by enabling its motors to spin.Step 2: Differentiate arming from disarming
Disarming stops the motors, so arming must be the opposite: preparing motors to start.Final Answer:
Prepares the drone's motors to start spinning for flight -> Option AQuick Check:
Arming = prepare motors [OK]
- Confusing arming with disarming
- Thinking arming turns off power
- Assuming arming calibrates sensors
Solution
Step 1: Identify correct function call syntax
Function calls use the format functionName() with parentheses and no spaces.Step 2: Check each option
armDrone() uses correct syntax: armDrone() with parentheses and no spaces.Final Answer:
armDrone() -> Option BQuick Check:
Function call = name() [OK]
- Using brackets [] or braces {} instead of parentheses
- Adding spaces inside function name
- Missing parentheses in function call
drone_state = {'armed': False}
def arm():
if not drone_state['armed']:
drone_state['armed'] = True
return 'Drone armed'
else:
return 'Drone already armed'
print(arm())
print(arm())What is the output?
Solution
Step 1: Check initial drone state
Initially, drone_state['armed'] is False, so first call to arm() sets it to True and returns 'Drone armed'.Step 2: Analyze second call to arm()
Now drone_state['armed'] is True, so second call returns 'Drone already armed'.Final Answer:
Drone armed\nDrone already armed -> Option CQuick Check:
First arm sets True, second arm detects already armed [OK]
- Assuming both calls arm the drone again
- Confusing the boolean check logic
- Expecting a KeyError due to dictionary access
drone_state = {'armed': True}
def disarm():
if drone_state['armed'] = True:
drone_state['armed'] = False
return 'Drone disarmed'
else:
return 'Drone already disarmed'
print(disarm())What is the error and how to fix it?
Solution
Step 1: Identify the error in the if condition
The code uses '=' which is assignment, not comparison. This causes a SyntaxError.Step 2: Correct the comparison operator
Replace '=' with '==' to compare drone_state['armed'] to True.Final Answer:
SyntaxError: Use '==' for comparison instead of '=' -> Option DQuick Check:
Comparison needs '==' not '=' [OK]
- Using '=' instead of '==' in conditions
- Assuming dictionary key is missing
- Confusing indentation errors with syntax errors
toggle_arm() that arms the drone if disarmed, and disarms if armed. Given drone_state = {'armed': False}, which code correctly implements this behavior?Solution
Step 1: Understand toggle logic
Toggle means switch state: if armed, disarm; if disarmed, arm.Step 2: Check each option's logic
def toggle_arm(): drone_state['armed'] = not drone_state['armed'] return 'Drone armed' if drone_state['armed'] else 'Drone disarmed' uses 'not' to flip boolean and returns correct message based on new state.Final Answer:
def toggle_arm(): drone_state['armed'] = not drone_state['armed'] return 'Drone armed' if drone_state['armed'] else 'Drone disarmed' -> Option AQuick Check:
Toggle by flipping boolean with 'not' [OK]
- Not flipping the state correctly
- Returning wrong message after toggle
- Code after return statement ignored
Ready to prove it?
Pass the test and this pattern moves unknown
