RESTful API design for devices in IOT Protocols - Time & Space Complexity
When devices communicate using RESTful APIs, the time it takes to handle requests matters a lot.
We want to know how the processing time grows as more devices or requests come in.
Analyze the time complexity of the following code snippet.
GET /devices
fetch all devices from database
for each device:
fetch device status
return list of devices with status
This code fetches all devices and then gets the status for each device before sending the response.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each device to fetch its status.
- How many times: Once for every device in the list.
As the number of devices grows, the time to fetch all statuses grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 status fetches |
| 100 | 100 status fetches |
| 1000 | 1000 status fetches |
Pattern observation: The work grows directly with the number of devices.
Time Complexity: O(n)
This means if you double the devices, the time to process doubles too.
[X] Wrong: "Fetching all devices and their statuses is always fast regardless of device count."
[OK] Correct: Each device adds extra work, so more devices mean more time needed.
Understanding how request time grows with device count shows you can design APIs that scale well.
"What if the API fetched all device statuses in one batch call instead of one by one? How would the time complexity change?"