Setting geofence boundaries in Drone Programming - Time & Space Complexity
When setting geofence boundaries for a drone, we want to know how the time to check or set these boundaries changes as we add more points.
We ask: How does the work grow when the number of boundary points increases?
Analyze the time complexity of the following code snippet.
function setGeofence(points) {
for (let i = 0; i < points.length; i++) {
drone.addBoundaryPoint(points[i]);
}
}
This code adds each point from a list to the drone's geofence boundary one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each point in the points list.
- How many times: Once for every point in the list.
As the number of points increases, the number of times the drone adds a boundary point grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly in proportion to the number of points.
Time Complexity: O(n)
This means the time to set geofence boundaries grows linearly as you add more points.
[X] Wrong: "Adding more points won't affect the time much because each point is added quickly."
[OK] Correct: Even if each addition is fast, doing it many times adds up, so total time grows with the number of points.
Understanding how time grows with input size helps you explain your code's efficiency clearly and confidently in real projects or interviews.
"What if we stored all points first and added them to the drone in one batch? How would the time complexity change?"