Inspection of infrastructure in Drone Programming - Time & Space Complexity
When a drone inspects infrastructure, it often checks many parts one by one. Understanding how the time it takes grows as the number of parts grows helps us plan better.
We want to know how the inspection time changes when there are more parts to check.
Analyze the time complexity of the following code snippet.
function inspectInfrastructure(parts) {
for (let i = 0; i < parts.length; i++) {
parts[i].scan();
}
}
This code makes the drone scan each part of the infrastructure one after another.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each part to scan it.
- How many times: Once for every part in the list.
As the number of parts increases, the drone spends more time scanning each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 scans |
| 100 | 100 scans |
| 1000 | 1000 scans |
Pattern observation: The time grows directly with the number of parts. Double the parts, double the scans.
Time Complexity: O(n)
This means the inspection time grows in a straight line with the number of parts to check.
[X] Wrong: "The drone scans all parts instantly, so time does not depend on the number of parts."
[OK] Correct: Each part needs a separate scan, so more parts always mean more time.
Understanding how tasks grow with input size is a key skill. It helps you explain how your code will behave when handling more data or more work.
"What if the drone scans pairs of parts together instead of one by one? How would the time complexity change?"