“Last 100 bytes” Interview Scenario

Something like this (circular buffer) :

byte[] buffer = new byte[100];
int index = 0;

public void recordByte(Byte b) {
   index = (index + 1) % 100;
   buffer[index] = b; 
}

public void print() {
   for(int i = index; i < index + 100; i++) {
       System.out.print(buffer[i % 100]);
   }
}

The benefits of using a circular buffer:

  1. You can reserve the space statically. In a real-time network application (VoIP, streaming,..)this is often done because you don’t need to store all data of a transmission, but only a window containing the new bytes to be processed.
  2. It’s fast: can be implemented with an array with read and write cost of O(1).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)