Mission upload and execution in Drone Programming - Time & Space Complexity
When a drone uploads and executes a mission, it processes a list of commands step by step.
We want to understand how the time it takes grows as the mission gets longer.
Analyze the time complexity of the following code snippet.
function uploadAndExecuteMission(mission) {
for (let i = 0; i < mission.length; i++) {
uploadCommand(mission[i]);
}
executeMission();
}
This code uploads each command in the mission one by one, then starts the mission execution.
- Primary operation: Looping through each command in the mission array.
- How many times: Once for every command in the mission.
As the mission gets longer, the number of commands to upload grows, so the time grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 uploads + 1 execute |
| 100 | About 100 uploads + 1 execute |
| 1000 | About 1000 uploads + 1 execute |
Pattern observation: The time grows roughly in direct proportion to the number of commands.
Time Complexity: O(n)
This means the time to upload and start the mission grows linearly with the number of commands.
[X] Wrong: "Uploading commands happens all at once, so time stays the same no matter how many commands there are."
[OK] Correct: Each command must be sent one by one, so more commands mean more time.
Understanding how mission upload time grows helps you explain how drones handle tasks efficiently as missions get bigger.
"What if the uploadCommand function itself had a loop inside? How would that affect the overall time complexity?"