Command acknowledgment handling in Drone Programming - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a drone receives commands, it often waits for acknowledgments to confirm each step.
We want to understand how the time to handle these acknowledgments grows as the number of commands increases.
Analyze the time complexity of the following code snippet.
function handleCommands(commands) {
for (let i = 0; i < commands.length; i++) {
sendCommand(commands[i]);
waitForAck(commands[i]);
}
}
function sendCommand(cmd) {
// sends command to drone
}
function waitForAck(cmd) {
// waits until drone acknowledges cmd
}
This code sends each command to the drone and waits for its acknowledgment before moving to the next.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sends each command and waits for its acknowledgment.
- How many times: Once for each command in the list.
Each command requires sending and waiting for acknowledgment, so the total time grows as more commands are added.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 send-and-wait cycles |
| 100 | 100 send-and-wait cycles |
| 1000 | 1000 send-and-wait cycles |
Pattern observation: The total time increases directly with the number of commands.
Time Complexity: O(n)
This means the time to handle acknowledgments grows in a straight line as the number of commands increases.
[X] Wrong: "Waiting for all acknowledgments happens at once, so time stays the same no matter how many commands."
[OK] Correct: The code waits for each acknowledgment before sending the next command, so time adds up with each command.
Understanding how waiting for acknowledgments affects time helps you design efficient drone communication and shows you can analyze real-world code timing.
"What if we sent all commands first, then waited for all acknowledgments together? How would the time complexity change?"
Practice
What is the main purpose of command acknowledgment in drone programming?
Solution
Step 1: Understand command acknowledgment
Command acknowledgment means the drone sends back a message confirming it got the command.Step 2: Identify the purpose
This confirmation ensures the command was received and understood, which is critical for safe control.Final Answer:
To confirm the drone received and understood the command -> Option AQuick Check:
Command acknowledgment = Confirm command received [OK]
- Confusing acknowledgment with drone speed control
- Thinking acknowledgment changes hardware settings
- Assuming acknowledgment recharges battery
Which of the following code snippets correctly checks if a drone command acknowledgment is received?
if response == 'ACK':
print('Command confirmed')
else:
print('No confirmation')Solution
Step 1: Identify correct equality operator
In Python, '==' checks if two values are equal, which is needed here.Step 2: Review options
'=' is assignment, '===' is not valid in Python, '!=' means not equal, so only '==' is correct.Final Answer:
if response == 'ACK': -> Option BQuick Check:
Equality check uses '==' in Python [OK]
- Using single '=' instead of '==' for comparison
- Using '===' which is JavaScript syntax, not Python
- Confusing '!=' with equality check
What will be the output of this code snippet handling drone command acknowledgment?
response = 'NACK'
if response == 'ACK':
print('Command successful')
else:
print('Command failed')Solution
Step 1: Check the response value
The variable response is set to 'NACK', which means negative acknowledgment.Step 2: Evaluate the if condition
The condition checks if response equals 'ACK'. Since it is 'NACK', the else block runs.Final Answer:
Command failed -> Option CQuick Check:
response != 'ACK' triggers else [OK]
- Assuming 'NACK' means success
- Expecting no output when else runs
- Confusing syntax error with logic error
Identify the error in this drone command acknowledgment code snippet:
response = get_drone_response()
if response = 'ACK':
print('Command confirmed')
else:
print('No confirmation')Solution
Step 1: Check the if condition syntax
The condition uses '=' which assigns value, but comparison needs '=='.Step 2: Verify other parts
Print statements have parentheses, function name is assumed correct, else block exists.Final Answer:
Using '=' instead of '==' in the if condition -> Option DQuick Check:
Comparison uses '==' not '=' [OK]
- Confusing assignment '=' with equality '=='
- Thinking print needs no parentheses in Python 3
- Assuming function name is wrong without context
You want to send a command to a drone and wait for acknowledgment with a timeout of 5 seconds. Which approach correctly handles this?
import time
def send_command_with_ack(command):
send_command(command)
start = time.time()
while time.time() - start < 5:
response = get_drone_response()
if response == 'ACK':
return 'Success'
return 'Timeout'Solution
Step 1: Analyze the timeout loop
The while loop runs while elapsed time is less than 5 seconds, checking for 'ACK'.Step 2: Check return behavior
If 'ACK' is received, returns 'Success'; if time exceeds 5 seconds without 'ACK', returns 'Timeout'.Final Answer:
The code waits up to 5 seconds for 'ACK', then returns 'Timeout' if none -> Option AQuick Check:
Timeout loop waits max 5 seconds for acknowledgment [OK]
- Not implementing timeout, causing infinite wait
- Returning timeout immediately without waiting
- Ignoring response check before returning success
