Implement Router - Problem
Design a data structure that can efficiently manage data packets in a network router. Each data packet consists of the following attributes:
- source: A unique identifier for the machine that generated the packet
- destination: A unique identifier for the target machine
- timestamp: The time at which the packet arrived at the router
Implement the Router class:
Router(int memoryLimit): Initializes the Router object with a fixed memory limit.memoryLimitis the maximum number of packets the router can store at any given time. If adding a new packet would exceed this limit, the oldest packet must be removed to free up space.bool addPacket(int source, int destination, int timestamp): Adds a packet with the given attributes to the router. A packet is considered a duplicate if another packet with the samesource,destination, andtimestampalready exists in the router. Returntrueif the packet is successfully added (i.e., it is not a duplicate); otherwise returnfalse.int[] forwardPacket(): Forwards the next packet in FIFO (First In First Out) order. Remove the packet from storage. Return the packet as an array[source, destination, timestamp]. If there are no packets to forward, return an empty array.int getCount(int destination, int startTime, int endTime): Returns the number of packets currently stored in the router (i.e., not yet forwarded) that have the specifieddestinationand have timestamps in the inclusive range[startTime, endTime].
Note that queries for addPacket will be made in non-decreasing order of timestamp.
Input & Output
Example 1 — Basic Router Operations
$
Input:
operations = [["Router", 3], ["addPacket", 1, 2, 100], ["addPacket", 3, 4, 200], ["addPacket", 1, 2, 100], ["forwardPacket"], ["getCount", 2, 50, 150]]
›
Output:
[null, true, true, false, [1, 2, 100], 1]
💡 Note:
Initialize router with capacity 3. Add packet (1,2,100) - success. Add packet (3,4,200) - success. Try to add duplicate (1,2,100) - fails. Forward first packet [1,2,100]. Count packets to destination 2 in time range [50,150] - returns 1 (no packets match).
Example 2 — Memory Limit Enforcement
$
Input:
operations = [["Router", 2], ["addPacket", 1, 2, 100], ["addPacket", 3, 4, 200], ["addPacket", 5, 6, 300], ["forwardPacket"]]
›
Output:
[null, true, true, true, [3, 4, 200]]
💡 Note:
Initialize router with capacity 2. Add two packets fills capacity. Adding third packet removes oldest (1,2,100). Forward returns (3,4,200) which was the oldest remaining.
Example 3 — Count Range Query
$
Input:
operations = [["Router", 5], ["addPacket", 1, 10, 100], ["addPacket", 2, 10, 150], ["addPacket", 3, 20, 200], ["getCount", 10, 90, 160]]
›
Output:
[null, true, true, true, 2]
💡 Note:
Add three packets. Count packets to destination 10 in time range [90,160]. Two packets match: (1,10,100) and (2,10,150).
Constraints
- 1 ≤ memoryLimit ≤ 1000
- -106 ≤ source, destination ≤ 106
- 1 ≤ timestamp ≤ 106
- At most 1000 calls will be made to addPacket, forwardPacket, and getCount
- Queries for addPacket will be made in non-decreasing order of timestamp
Visualization
Tap to expand
Understanding the Visualization
1
Input Operations
Sequence of router operations including add, forward, and count
2
Router Processing
Handle packets with memory limit, detect duplicates, maintain FIFO order
3
Output Results
Return operation results: boolean for add, array for forward, count for queries
Key Takeaway
🎯 Key Insight: Router combines queue for FIFO ordering with hash set for constant-time duplicate detection
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code