Given there exists a network flow that makes up a majority of bandwidth (flow using more than 50% of bandwidth), could you identify it given a stream of packets? You only need to tell the flow ID at the end of the stream.
import java.util.*;
class Packet {
String flowId; // Nonempty
int size; // Positive
Packet(String source_id, int bytes) {
flowId = source_id;
size = bytes;
}
}A A A B B C B A (flowId)
2 3 6 1 1 2 1 5 (size)
max: A
total bandwidth = 2 + 3 + 6 ... + 1 + 5 = 21
size of flow A = 2 + 3 + 6 + 5 = 16 > total * 50%
==> A is the large flow
List<Packet> packets -> Immutable, Well-Formed, and Never EmptyFor Algorithmic Complexity:
F for number of flowIDs, P for number of Packets
The packets can be non-contiguous Eg
A B A B C A A..
1 1 1 1 1 1 1..
I was able to come up with a solution with O(n) space complexity using a hashmap. Interviewer asked for a solution with constant space and suggested maintaining a running sum as the approach. Any ideas?
hints to use - majority bandwith will use more than 50% of bandwidth.
sorting cannot be used since its an immutable list