Reassemble pieces arriving out of order
Quick review (2 minutes)
Printed pages that say Page 2 of 11 can be put back in order however the pile was dropped.
The receiver places each usable card in the slot of its piece number when that slot is empty, whatever the order of arrival, and reads the data of slots 1 to N in order once all N slots are filled; an empty slot means no usable card for that piece has arrived yet.
Check each arriving card against the protocol. Read the total on the first usable data card and lay out that many empty slots.
Put each usable card into the slot of its piece number if that slot is empty; put a card that fails the check aside.
After each card, list the slots still empty.
When no slot is empty, read the data of the slots from 1 to N.
If a slot stays empty, the message is not complete; do not guess its data.
Joining pieces in the order they arrive gives NOONMEET-AT- for the arrivals 3, 1, 2. Place by number.
Reading the message while a slot is empty gives a message with a hole in it. Wait for every slot; the next lesson shows how the sender sends a missing piece again.
1 In plain words
Printed pages that say Page 2 of 11 can be put back in order however the pile was dropped.
Packets on the internet may arrive in order, out of order, or not at all. They can take different paths: routing is the process of finding a path from sender to receiver, and on the internet it is usually decided as the packets travel, not fixed in advance.
In the card protocol, the receiver lays out N empty slots, puts each usable card into the slot of its number when that slot is empty (a card for a filled slot is a duplicate, set aside as a later lesson shows), and reads the data of slots 1 to N in order once every slot is filled. A slot still empty means no usable card for that piece has arrived yet.
Remember one sentence: place each piece by its number, not by when it came. To look it up, see essential knowledge CSN-1.C.2 and CSN-1.C.3 in the AP Computer Science Principles Course and Exam Description.
2 Picture it
A table of three cards that arrive out of order. The first to arrive is piece 3 of 3, which fills slot 3. The second is piece 1 of 3, so slots 1 and 3 are filled. The third is piece 2 of 3, and all three slots are filled. The last row is marked.
3 Why
The order of arrival depends on the paths the pieces took, and one path can be faster or shorter than another. In this unit's runs every usable card keeps the number the sender wrote, so the slots put the data back in the order the sender cut it.
The total on each card tells the receiver how many slots to lay out, so a gap shows. With pieces 1 and 3 of 3 in their slots, slot 2 is empty, and the message is not ready.
Joining the pieces in arrival order can mix up the message when they arrive out of order. The Field Guide describes TCP, the Transmission Control Protocol used on the internet, putting a sequence number on each packet for this reason; without one, a delayed piece can turn HELLO into HELOL.
4 The rule
The receiver places each usable card in the slot of its piece number when that slot is empty, whatever the order of arrival, and reads the data of slots 1 to N in order once all N slots are filled; an empty slot means no usable card for that piece has arrived yet.
5 How
- Check each arriving card against the protocol. Read the total on the first usable data card and lay out that many empty slots.
- Put each usable card into the slot of its piece number if that slot is empty; put a card that fails the check aside.
- After each card, list the slots still empty.
- When no slot is empty, read the data of the slots from 1 to N.
- If a slot stays empty, the message is not complete; do not guess its data.
6 Trace
| Card arrives | Slots 1, 2, 3 | Empty slots | Explanation |
|---|---|---|---|
| 3 of 3: NOON | _, _, NOON | 1, 2 | The total says three slots; NOON goes in slot 3. |
| 1 of 3: MEET | MEET, _, NOON | 2 | MEET goes in slot 1. |
| 2 of 3: -AT- | MEET, -AT-, NOON | none | Every slot is filled: the message is MEET-AT-NOON. |
7 The trap
Joining pieces in the order they arrive gives NOONMEET-AT- for the arrivals 3, 1, 2. Place by number.
Reading the message while a slot is empty gives a message with a hole in it. Wait for every slot; the next lesson shows how the sender sends a missing piece again.
Quick checks
Quick check 1 of 2
Cards of a 3-piece message arrive in the order 2 of 3 (-AT-), 3 of 3 (NOON), 1 of 3 (MEET). What message does the receiver read, and after which arrival is it complete?
Show the steps and the answer
It reads MEET-AT-NOON, complete after the third arrival.
Why: Each card goes into the slot of its number. Slot 1 stays empty until the last card, piece 1, arrives.
Quick check 2 of 2
A receiver holds pieces 1, 2 and 4 of a 4-piece message. What does it know, and what can it not do yet?
Show the steps and the answer
It knows piece 3 is missing, and it cannot read the whole message yet.
Why: The total says four slots. Slot 3 is empty, so a piece has not arrived, and packets can be lost on the way.
Source: College Board, AP Computer Science Principles Course and Exam Description
Sources
- Computer Science Field Guide: Network Communication Protocols, 16.3 Transport layer protocols - TCP, UDP. University of Canterbury Computer Science Education Research Group. CC BY-SA 4.0. Read 2 Dec 2026.
- AP Computer Science Principles Course and Exam Description. College Board. All rights reserved. Read 2 Dec 2026.