Google | Online Assesment | Read Stream of bytes
Anonymous User
1473

We want to be able to replicate how the os/browser recieves bytes of data and convert them into meaningful items. Let us consider the initial stream of data to be unknown. In the below representations, I will use . to repersent unknown data. All other data that are known will not be .. We will be doing 2 operations here:

  • byte[] read(int length)
  • void acceptData(int start, byte[] data)

Initial stream : .........................................................
Operation: read(4) -> empty bytes
Stream: .........................................................
Operation: acceptData(4, "ABCD")
Stream: ....ABCD.....................................................
Operation: read(6) -> empty bytes[Rule 1]
Stream: ....ABCD.....................................................
Operation: acceptData(0, "EFGH")
Stream: EFGHABCD.....................................................
Operation: read(6) -> EFGHAB
Stream: .........................................................
Operation: read(6) -> CD [Rule 2]
Stream: .........................................................
Operation: acceptData(0, "I")
Stream: I.....................................................
Operation: acceptData(2, "J") [Rule 2]
Stream: I.J...................................................
Operation: read(6) -> I
Stream: J........................................................ [Rule 3]

Rules for read():

  1. If any read starts with unknown char, return empty bytes as it is not readable yet
  2. If any read has unknown char in between it, cutoff read and output whatever is read
  3. If any read is successful as a result of cutoff or complete read, trim stream till that point.
  4. There will be no overlap (ie - if stream has 4th element filled, acceptData will never try to replace existing data at 4th element, it will always be till 3rd)

NOTE: This question might be tough to understand but if you read it multiple time you will understand.

I havn't had my results yet but yes I did implement this with an ArrayList in Java.

For leetcode folks,

  1. How do you rate this question - easy/medium/hard?
  2. What would be data structure you would choose?
Comments (3)