Pre-flight checklist automation in Drone Programming - Time & Space Complexity
When automating a pre-flight checklist, it is important to know how the time to complete checks grows as the number of items increases.
We want to understand how the program's running time changes when more checklist items are added.
Analyze the time complexity of the following code snippet.
function runPreFlightChecklist(items) {
for (let i = 0; i < items.length; i++) {
if (!items[i].check()) {
return false;
}
}
return true;
}
This code runs through each checklist item and performs a check. If any check fails, it stops early.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each checklist item and calling its check method.
- How many times: Up to once per item, stopping early if a check fails.
As the number of checklist items grows, the program may check more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of operations grows roughly in direct proportion to the number of checklist items.
Time Complexity: O(n)
This means the time to run the checklist grows linearly with the number of items.
[X] Wrong: "The program always checks every item no matter what."
[OK] Correct: The code stops checking as soon as one item fails, so it may do fewer checks than the total number of items.
Understanding how loops and early exits affect time helps you explain your code clearly and shows you can think about efficiency in real tasks like drone pre-flight checks.
"What if the checklist items were grouped and each group had to be checked fully before moving on? How would the time complexity change?"