You are given a list of packet data packets, where each packet is a list of two elements: a string with the protocol name and an integer with the packet size in bytes. You are also given an integer threshold.
Analyze the packet data to find the most frequent protocol. If there is a tie in frequency, return the protocol that appears first in the input.
Also identify the anomalous packets, whose size strictly exceeds threshold. Return a list where the first element is the most frequent protocol name and the second element is the list of 0-indexed positions of the anomalous packets, sorted in ascending order.
Function Signature
def analyze_protocols(packets: list[list], threshold: int) -> list:
Rules
-
Each
packets[i]
is
[protocol_name, packet_size]
.
-
Protocol names are compared exactly, so
"TCP"
and
"tcp"
are different protocols.
-
A tie in frequency goes to the protocol whose first occurrence has the smallest index.
-
A packet whose size equals
threshold
is not anomalous.
-
Return
[protocol_name, indices]
. When no packet is anomalous,
indices
is an empty list.
Constraints
-
1 <= len(packets) <= 10^5
-
1 <= len(protocol_name) <= 20
-
protocol_name
consists of uppercase and lowercase English letters and digits.
-
1 <= packet_size <= 10^6
-
1 <= threshold <= 10^6
Examples
Input:
packets = [
["TCP", 500],
["UDP", 1200],
["TCP", 300],
["HTTP", 800],
["UDP", 1500],
["TCP", 200]
]
threshold = 1000
Output: ["TCP", [1, 4]]
TCP appears 3 times, UDP 2 times and HTTP once, so TCP is the most frequent. The packets at index 1 (UDP, 1200 bytes) and index 4 (UDP, 1500 bytes) exceed the threshold of 1000.
Input:
packets = [
["HTTP", 400],
["FTP", 600],
["HTTP", 300],
["FTP", 500],
["DNS", 100]
]
threshold = 550
Output: ["HTTP", [1]]
HTTP and FTP both appear twice. HTTP appears first (index 0), so it wins the tie. Only the packet at index 1 (FTP, 600 bytes) exceeds 550.
Input:
packets = [["DNS", 50], ["SSH", 2000], ["SSH", 90], ["DNS", 1000]]
threshold = 1000
Output: ["DNS", [1]]
DNS and SSH both appear twice. SSH reaches a count of 2 first, but DNS appears first in the input, so DNS wins. The last packet is exactly 1000 bytes, which does not exceed the threshold.