Delivery drone concept in Drone Programming - Time & Space Complexity
When programming a delivery drone, it's important to know how the time it takes to complete tasks grows as the number of deliveries increases.
We want to understand how the drone's delivery process scales with more packages to deliver.
Analyze the time complexity of the following code snippet.
function deliverPackages(packages) {
for (let i = 0; i < packages.length; i++) {
flyTo(packages[i].location);
dropPackage(packages[i]);
returnToBase();
}
}
This code makes the drone deliver each package one by one by flying to the location, dropping the package, and returning to base.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each package in the list.
- How many times: Once for every package in the input list.
As the number of packages increases, the drone must perform more deliveries, so the total time grows directly with the number of packages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 deliveries |
| 100 | 100 deliveries |
| 1000 | 1000 deliveries |
Pattern observation: The time grows in a straight line as the number of packages increases.
Time Complexity: O(n)
This means the time to complete deliveries grows directly in proportion to the number of packages.
[X] Wrong: "The drone can deliver all packages instantly, so time stays the same no matter how many packages there are."
[OK] Correct: Each package requires flying to a location and returning, so more packages mean more trips and more time.
Understanding how tasks grow with input size helps you explain your code clearly and shows you can think about efficiency in real-world problems like drone deliveries.
"What if the drone could carry all packages at once and deliver them in a single trip? How would the time complexity change?"