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?"
Practice
inspect() function in infrastructure inspection?Solution
Step 1: Understand the function purpose
Theinspect()function is designed to send the drone to a location for inspection.Step 2: Compare options with function role
Only To send the drone to a specific location for inspection matches the purpose of sending the drone to inspect infrastructure.Final Answer:
To send the drone to a specific location for inspection -> Option CQuick Check:
inspect() sends drone to location = A [OK]
- Confusing inspect() with repair functions
- Thinking inspect() controls speed
- Assuming inspect() charges battery
Solution
Step 1: Identify correct method naming style
In drone programming, method names use lowercase with underscores for readability.Step 2: Match options to correct style
drone.capture_image() usescapture_image(), which is the correct syntax.Final Answer:
drone.capture_image() -> Option DQuick Check:
Method names use underscores = D [OK]
- Using camelCase instead of snake_case
- Omitting underscores between words
- Using invalid characters like hyphens
drone.inspect('bridge')
image = drone.capture_image()
print(image)Solution
Step 1: Understand the inspect and capture_image functions
Theinspect('bridge')sends the drone to the bridge, andcapture_image()takes a photo and returns the filename.Step 2: Analyze the print output
Printingimageshows the filename string, which is'bridge_image.jpg'.Final Answer:
'bridge_image.jpg' -> Option BQuick Check:
capture_image() returns filename = C [OK]
- Expecting a boolean or None instead of filename
- Confusing print output with status messages
- Assuming syntax error without checking code
drone.inspect('tower')
image = drone.captureImage()
print(image)Solution
Step 1: Check method names for correct syntax
The methodcaptureImage()is incorrect; it should becapture_image()using underscores.Step 2: Verify other code parts
inspect('tower')is correct with parentheses and one argument;print()can print variables.Final Answer:
Method name should be capture_image(), not captureImage() -> Option AQuick Check:
Method names use underscores = A [OK]
- Using camelCase instead of snake_case
- Thinking print() can't print variables
- Assuming inspect needs two arguments
points = ['bridge', 'tower', 'dam']
images = {}
for point in points:
drone.inspect(point)
images[point] = drone.capture_image()
print(images)Solution
Step 1: Understand the loop and dictionary usage
The code loops over points, inspects each, and stores the image filename in a dictionary with the point as key.Step 2: Predict the dictionary content after loop
Each key is the point name, and value is the image filename matching that point, e.g. 'bridge_image.jpg'.Final Answer:
{'bridge': 'bridge_image.jpg', 'tower': 'tower_image.jpg', 'dam': 'dam_image.jpg'} -> Option AQuick Check:
Dictionary keys = points, values = filenames = B [OK]
- Using list instead of dictionary for mapping
- Forgetting colon after for loop
- Confusing set notation with dictionary
Ready to prove it?
Pass the test and this pattern moves unknown
