Reliable Packet Delivery System
Problem Statement
You are designing a reliable packet delivery system for Amazon’s internal messaging service.
In this system, packets may arrive out of order or some packets may be lost during transmission. Your task is to implement a packet tracking mechanism that:
- Keeps track of received packets by their sequence numbers
- Determines the highest consecutive packet received (for acknowledgment)
- Identifies missing packets that need to be resent
Each packet contains:
sequence_number: A unique integer identifying the packet’s position in the sequence (starting from 0)data: The actual payload of the packet
You need to implement two main functions:
ReceivePacket(packet): Process a newly received packetGetAcknowledgment(): Return the highest consecutive packet sequence received and a list of missing packets that need to be resent
The acknowledgment system follows these rules:
- The
highest consecutive packetis the highest sequence numberNsuch that all packets with sequence numbers0toNhave been received - Missing packets are those with sequence numbers between
0and the highest received packet that have not yet been received
Constraints
- Sequence numbers are non-negative integers:
0 <= sequence_number <= 10^5 - The data payload is a string with length between
1and1000 - The number of operations will be between
1and10^4
This problem asks you to build a packet-tracking mechanism for out-of-order delivery. A practical solution is to store received sequence numbers in a hash set or boolean structure, then maintain the largest contiguous prefix starting from 0. When a packet arrives, mark it as received and advance the contiguous pointer as far as possible; when generating an acknowledgment, return both the current highest consecutive sequence number and the missing sequence numbers up to the highest received packet. The key challenges are handling gaps, duplicates, and incremental updates efficiently.