Block-wise transfer for large payloads in IOT Protocols - Time & Space Complexity
When sending large data in IoT, block-wise transfer splits it into smaller parts.
We want to know how the time to send data grows as the data size increases.
Analyze the time complexity of the following code snippet.
function sendLargePayload(payload) {
const blockSize = 1024;
const totalBlocks = Math.ceil(payload.length / blockSize);
for (let block = 0; block < totalBlocks; block++) {
const start = block * blockSize;
const end = Math.min(start + blockSize, payload.length);
sendBlock(payload.slice(start, end));
}
}
This code splits a large payload into blocks and sends each block one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each block of the payload.
- How many times: Once for every block, which depends on payload size divided by block size.
As the payload size grows, the number of blocks grows proportionally.
| Input Size (n bytes) | Approx. Operations (blocks sent) |
|---|---|
| 10,240 | 10 blocks |
| 102,400 | 100 blocks |
| 1,024,000 | 1000 blocks |
Pattern observation: Doubling the payload roughly doubles the number of blocks and operations.
Time Complexity: O(n)
This means the time to send data grows directly with the size of the payload.
[X] Wrong: "Sending data in blocks makes the time constant no matter the size."
[OK] Correct: Each block still takes time to send, so more data means more blocks and more time.
Understanding how splitting data affects sending time helps you design efficient IoT communication.
What if we doubled the block size? How would the time complexity change?
Practice
Solution
Step 1: Understand the problem of large data in IoT
Large data can be hard to send in one go because devices have limited memory and networks can be unstable.Step 2: Identify the role of block-wise transfer
Block-wise transfer breaks big data into smaller blocks, making it easier and safer to send piece by piece.Final Answer:
To split large data into smaller blocks for easier transmission -> Option BQuick Check:
Block-wise transfer = splitting data [OK]
- Confusing block-wise transfer with encryption
- Thinking it compresses data
- Assuming it prioritizes messages
Solution
Step 1: Identify the correct block option name
In CoAP, Block1 is used for block-wise transfer of request payloads, and Block2 for responses. Since this is a request, Block1 is correct.Step 2: Check block number and more flag
Block number is 3, 'more' flag is 1 meaning more blocks follow, and size is 64 bytes.Block1: num=3; more=1; size=64matches all correctly.Final Answer:
Block1: num=3; more=1; size=64 -> Option AQuick Check:
Block1 with num=3 and more=1 means block 3 with more blocks [OK]
- Using Block2 instead of Block1 for request
- Setting 'more' flag to 0 when more blocks exist
- Mixing block number and more flag values
Block2: num=0; more=1; size=64 Block2: num=1; more=1; size=64 Block2: num=2; more=0; size=64
Solution
Step 1: Analyze the 'more' flag in each block
Blocks 0 and 1 have 'more=1', meaning more blocks follow. Block 2 has 'more=0', meaning it is the last block.Step 2: Identify the last block number
Since block number 2 has 'more=0', it is the last block received.Final Answer:
2 -> Option DQuick Check:
Last block has more=0 and num=2 [OK]
- Choosing block with more=1 as last block
- Confusing block numbers with sequence count
- Ignoring the 'more' flag
Solution
Step 1: Understand the role of acknowledgments in block-wise transfer
Each block must be acknowledged by the receiver to confirm it was received correctly.Step 2: Analyze why block 2 is not acknowledged
If block 2 is never acknowledged, it likely means the sender never sent it, causing the receiver to wait indefinitely.Final Answer:
The sender skipped sending block 2 -> Option CQuick Check:
No ack for block means block not sent [OK]
- Blaming 'more' flag for missing ack
- Assuming receiver dropped block without evidence
- Thinking block size alone causes missing ack
Solution
Step 1: Calculate number of blocks needed
Payload size is 200 bytes, block size is 64 bytes. 200 / 64 = 3.125, so 4 blocks are needed to send all data.Step 2: Determine 'more' flag for last block
For all blocks except the last, 'more' flag is 1. For the last block, it is 0 to indicate no more blocks follow.Final Answer:
4 blocks; 'more' flag = 0 on last block -> Option AQuick Check:
Payload/size = 4 blocks; last block more=0 [OK]
- Rounding down number of blocks
- Setting 'more' flag to 1 on last block
- Ignoring partial block for last piece
Ready to prove it?
Pass the test and this pattern moves unknown
