QoS levels (0, 1, 2) in IOT Protocols - Time & Space Complexity
We want to understand how the work done by different QoS levels changes as the number of messages grows.
How does the effort to deliver messages grow when more messages are sent?
Analyze the time complexity of the following QoS message delivery logic.
// Pseudocode for QoS message delivery
function deliverMessages(messages, qos) {
for (let msg of messages) {
if (qos === 0) {
sendOnce(msg);
} else if (qos === 1) {
sendAndWaitAck(msg);
} else if (qos === 2) {
sendWithTwoStepHandshake(msg);
}
}
}
This code sends each message differently based on QoS level: once, with one acknowledgment, or with a two-step handshake.
Look at what repeats as messages increase.
- Primary operation: Loop over all messages to send them.
- How many times: Once per message, so number of messages times.
As the number of messages grows, the sending steps grow too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 sends (each with steps depending on QoS) |
| 100 | 100 sends (each with steps depending on QoS) |
| 1000 | 1000 sends (each with steps depending on QoS) |
Pattern observation: The total work grows directly with the number of messages.
Time Complexity: O(n)
This means the work to deliver messages grows in a straight line as you add more messages.
[X] Wrong: "QoS 2 takes much longer time because of the handshake, so its time complexity is higher than QoS 0 or 1."
[OK] Correct: While QoS 2 has more steps per message, the number of messages still controls the total work linearly. The extra steps are a constant factor, not a change in growth rate.
Understanding how message delivery scales helps you explain system behavior clearly and shows you can think about efficiency in real-world IoT systems.
"What if we added retries for failed messages in QoS 1 and 2? How would the time complexity change?"