Protocol translation at edge in IOT Protocols - Time & Space Complexity
When devices speak different languages, edge devices translate their messages. We want to see how the time to translate grows as more messages arrive.
How does the work increase when the number of messages grows?
Analyze the time complexity of the following code snippet.
function translateMessages(messages) {
let translated = [];
for (let i = 0; i < messages.length; i++) {
let msg = messages[i];
let result = translateProtocol(msg);
translated.push(result);
}
return translated;
}
function translateProtocol(message) {
// Simulate translation work
return "translated:" + message;
}
This code takes a list of messages and translates each one using a translation function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each message to translate it.
- How many times: Once for every message in the input list.
Each new message adds one more translation step, so the work grows steadily with the number of messages.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 translations |
| 100 | 100 translations |
| 1000 | 1000 translations |
Pattern observation: The work grows directly in proportion to the number of messages.
Time Complexity: O(n)
This means if you double the messages, the translation time roughly doubles too.
[X] Wrong: "The translation time stays the same no matter how many messages come in."
[OK] Correct: Each message needs its own translation step, so more messages always mean more work.
Understanding how work grows with input size helps you explain system behavior clearly. This skill shows you can think about efficiency in real-world device communication.
"What if the translation function itself called another loop over message parts? How would the time complexity change?"