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. memoryLimit is 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 same source, destination, and timestamp already exists in the router. Return true if the packet is successfully added (i.e., it is not a duplicate); otherwise return false.
  • 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 specified destination and 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], 0]
💡 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 0 (no packets match since only (3,4,200) remains with destination 4).
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
Router Implementation INPUT Router(memoryLimit=3) Initialize with capacity 3 Operations Sequence: 1. addPacket(1, 2, 100) 2. addPacket(3, 4, 200) 3. addPacket(1, 2, 100) DUP 4. forwardPacket() 5. getCount(2, 50, 150) Packet Structure: src dest timestamp ALGORITHM STEPS 1 Hash Set for Duplicates Store (src,dest,time) tuples Set: {(1,2,100), (3,4,200)} O(1) lookup for duplicates 2 Queue for FIFO Order Maintain packet arrival order [1,2,100] [3,4,200] Front Back 3 Destination Index Map Map dest --> list of timestamps dest 2: [100] dest 4: [200] 4 Binary Search for Count Range query [startTime, endTime] getCount(2, 50, 150) --> 1 FINAL RESULT Operation Results: Router(3) null addPacket(1,2,100) true addPacket(3,4,200) true addPacket(1,2,100) false forwardPacket() [1,2,100] getCount(2,50,150) 1 Output Array: [null, true, true, false, [1,2,100], 1] OK - All operations complete Key Insight: Combine HashSet for O(1) duplicate detection with Queue for FIFO ordering. Use a HashMap mapping destinations to sorted timestamp lists, enabling O(log n) range queries via binary search. Memory limit enforced by evicting oldest packets when queue exceeds capacity. Time Complexity: add: O(1), forward: O(1) getCount: O(log n) TutorialsPoint - Implement Router | Hash Set + Queue Approach
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
12.4K Views
Medium Frequency
~35 min Avg. Time
256 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen