Arming and takeoff command in Drone Programming - Time & Space Complexity
When we send commands to a drone, like arming and taking off, it's important to know how long these commands take as we repeat or scale them.
We want to understand how the time to complete these commands grows when we change how many times we run them.
Analyze the time complexity of the following code snippet.
function armAndTakeoff(targetAltitude) {
armDrone();
while (!isArmed()) {
wait(100); // wait 100ms
}
takeoff(targetAltitude);
while (getAltitude() < targetAltitude) {
wait(100); // wait 100ms
}
}
This code arms the drone, waits until it is armed, then commands it to take off and waits until it reaches the target altitude.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two while loops that repeatedly check conditions.
- How many times: Each loop runs until the drone is armed or reaches altitude, which depends on time, not input size.
Explain the growth pattern intuitively.
| Input Size (targetAltitude) | Approx. Operations |
|---|---|
| 10 meters | Depends on time to arm + time to reach 10m altitude |
| 100 meters | Longer wait to reach 100m altitude, more loop checks |
| 1000 meters | Much longer wait, many more loop checks |
Pattern observation: The number of checks grows roughly in proportion to the time it takes to reach the target altitude, so larger altitude means more loop iterations.
Time Complexity: O(n)
This means the time to complete the command grows linearly with the target altitude, as the drone takes longer to reach higher altitudes.
[X] Wrong: "The loops run a fixed number of times regardless of altitude."
[OK] Correct: The loops depend on the drone's state, which changes over time and altitude, so more altitude means more loop checks.
Understanding how commands scale with input helps you explain how your code behaves in real situations, showing you think about efficiency and timing.
"What if we replaced the waiting loops with event listeners that trigger when the drone is armed or reaches altitude? How would the time complexity change?"
Practice
Solution
Step 1: Understand drone startup sequence
Before flying, the drone motors must be started to prepare for flight.Step 2: Identify the correct first command
The 'arm' command starts the motors, which is necessary before takeoff.Final Answer:
Arm the drone motors -> Option AQuick Check:
First step = Arm motors [OK]
- Trying to takeoff before arming
- Skipping arming step
- Confusing landing with arming
Solution
Step 1: Identify the object and method
To arm the drone, the method 'arm()' is called on the drone object.Step 2: Check syntax correctness
'drone.arm()' correctly calls the arm method on the drone instance.Final Answer:
drone.arm() -> Option BQuick Check:
Correct method call = drone.arm() [OK]
- Reversing method and object
- Using takeoff() instead of arm()
- Calling arm() without object
drone.arm() drone.takeoff(10) print(drone.altitude)Assuming the drone starts at altitude 0 and takeoff sets altitude to the given height.
Solution
Step 1: Arm the drone
The drone.arm() command starts the motors, allowing takeoff to proceed.Step 2: Takeoff to 10 meters
drone.takeoff(10) sets the altitude to 10 meters.Final Answer:
10 -> Option CQuick Check:
Altitude after takeoff = 10 [OK]
- Assuming altitude stays 0
- Thinking takeoff fails without arm
- Confusing altitude with speed
drone.takeoff(5) drone.arm()
Solution
Step 1: Check command order
Takeoff should only happen after the drone is armed.Step 2: Identify the error
Calling takeoff before arm is incorrect and may cause failure or error.Final Answer:
Takeoff called before arming -> Option AQuick Check:
Arm first, then takeoff [OK]
- Swapping command order
- Assuming arm needs parameters
- Ignoring command sequence importance
Solution
Step 1: Arm the drone first
Start motors with drone.arm() before any flight commands.Step 2: Takeoff to 15 meters and hover
Use drone.takeoff(15) to rise, then pause with time.sleep(5) to hover.Step 3: Land safely after hover
Call drone.land() to bring the drone down safely.Final Answer:
drone.arm() drone.takeoff(15) time.sleep(5) drone.land() -> Option DQuick Check:
Arm -> Takeoff -> Hover -> Land [OK]
- Taking off before arming
- Hovering before takeoff
- Landing before takeoff
Ready to prove it?
Pass the test and this pattern moves unknown
