0
0
IOT Protocolsdevops~5 mins

RESTful API design for devices in IOT Protocols - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: RESTful API design for devices
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

As the number of devices grows, the time to fetch all statuses grows too.

Input Size (n)Approx. Operations
1010 status fetches
100100 status fetches
10001000 status fetches

Pattern observation: The work grows directly with the number of devices.

Final Time Complexity

Time Complexity: O(n)

This means if you double the devices, the time to process doubles too.

Common Mistake

[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.

Interview Connect

Understanding how request time grows with device count shows you can design APIs that scale well.

Self-Check

"What if the API fetched all device statuses in one batch call instead of one by one? How would the time complexity change?"