Meta interview question

Implement a function char* readLine(); which returns single lines from a buffer. To read the buffer, you can makes use of a function int read(char* buf, int len) which fills buf with upto len chars and returns the actual number of chars filled in. Function readLine can be called as many times as desired. If there is no valid data or newline terminated string available, it must block. In order to block, it can use read function which in turn will block when it doesn't have anything to fill the buf.

Interview Answers

Anonymous

29 Dec 2011

are you stupid?

3

Anonymous

8 Oct 2011

// this code makes an assumption that the max line length is 1000 chars // assuming each line has a maximum length of 1000 char* readLine() { // allocate one more char for terminating character char* buf = new char[1001]; int numRead = read(buf, 1000); buf[numRead] = '\n'; }