This is what I wrote at Facebook onsite interview, 2 minutes job.


  • 1
    X
        private int cache_r, cache_w;
        private char [] cache = new char[4];
        public int read(char[] buf, int n) {
            int buf_ptr = 0;
            while (buf_ptr < n) {
                if (cache_r < cache_w) {
                    buf[buf_ptr++] = cache[cache_r++];
                    continue;
                }
                cache_r = 0;
                cache_w = read4(cache);
                if (cache_w == 0) break;
            }
            return buf_ptr;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.