RC signal loss failsafe in Drone Programming - Time & Space Complexity
When a drone loses its remote control signal, it must quickly decide what to do next. We want to understand how the time it takes to check and respond to signal loss changes as the number of checks or inputs grows.
How does the program's work grow when it repeatedly checks the signal?
Analyze the time complexity of the following code snippet.
// Check signal status multiple times
for (int i = 0; i < checks; i++) {
if (signalLost()) {
activateFailsafe();
break;
}
wait(10); // wait 10 ms before next check
}
This code repeatedly checks if the RC signal is lost and activates a failsafe immediately when loss is detected.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that checks the signal status repeatedly.
- How many times: Up to
checkstimes, or fewer if signal loss is detected early.
Each additional check adds one more signal status test, so the work grows directly with the number of checks.
| Input Size (checks) | Approx. Operations |
|---|---|
| 10 | Up to 10 signal checks |
| 100 | Up to 100 signal checks |
| 1000 | Up to 1000 signal checks |
Pattern observation: The number of operations grows in a straight line as the number of checks increases.
Time Complexity: O(n)
This means the time to detect signal loss grows directly with how many times the program checks the signal.
[X] Wrong: "The failsafe check happens instantly no matter how many times we check."
[OK] Correct: Each check takes time, so more checks mean more total time spent before deciding.
Understanding how repeated checks affect timing helps you design reliable drone controls and shows you can think about program speed in real situations.
"What if the code checked multiple signals in each loop instead of just one? How would the time complexity change?"