Rubrik | Phone Screen | Majority bandwidth from a network

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-11-11Added By Anonymous
Source: Link
1Given 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. 2 3 4import java.util.*; 5class Packet { 6 String flowId; // Nonempty 7 int size; // Positive 8 9 Packet(String source_id, int bytes) { 10 flowId = source_id; 11 size = bytes; 12 } 13} 14A A A B B C B A (flowId) 152 3 6 1 1 2 1 5 (size) 16max: A 17total bandwidth = 2 + 3 + 6 ... + 1 + 5 = 21 18size of flow A = 2 + 3 + 6 + 5 = 16 > total * 50% 19==> A is the large flow 20 21 List<Packet> packets -> Immutable, Well-Formed, and Never Empty 22For Algorithmic Complexity: 23F for number of flowIDs, P for number of Packets 24 25The packets can be non-contiguous Eg 26A B A B C A A.. 271 1 1 1 1 1 1.. 28 29I 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? 30 31hints to use - majority bandwith will use more than 50% of bandwidth. 32sorting cannot be used since its an immutable list
Comments