Facebook Interview Question for SDE1s


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BufferRead {

	private static int BufSize = 4000;
	private char[] extraRead = new char[BufSize];
	private int extraBytes = 0;
	private int extraBytesStart = 0;
	private int extraBytesEnd = 0;

	private boolean EOFencountered = false;

	private int read4K(char[] buf) {
		return 0;
	}

	// IMPLEMENT:
	public int read(char[] buf, int toRead) {

		if (EOFencountered) {
			return 0;
		}

		int localtoRead = 0;

		while (localtoRead < extraBytes && localtoRead < toRead && extraBytesStart < extraBytesEnd) {
			buf[localtoRead++] = extraRead[extraBytesStart++ % BufSize];
		}

		while (localtoRead < toRead) {
			char[] buf4k = new char[4000];
			int buytesRead = read4K(buf4k);
			int i = localtoRead;
			for (; i < localtoRead + buytesRead && i < toRead; i++) {
				buf[i] = buf4k[i - localtoRead];
			}

			// Store the extra Bytes
			extraBytesStart = 0;
			for (int j = i - localtoRead; j < buytesRead; j++) {
				extraRead[extraBytesStart++] = buf4k[j];
			}
			extraBytesEnd = extraBytesStart;
			extraBytesStart = 0;

			localtoRead = i;

			if (buf[localtoRead - 1] == 0) {
				extraBytes = 0;
				EOFencountered = true;
				break;
			}

		}
		return localtoRead;
	}

}

- Vibhav May 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package fun;

public class BufferRead {

private static int BufSize = 4000;
private char[] extraRead = new char[BufSize];
private int extraBytes = 0;
private int extraBytesStart = 0;
private int extraBytesEnd = 0;

private boolean EOFencountered = false;

private int read4K(char[] buf) {
return 0;
}

// IMPLEMENT:
public int read(char[] buf, int toRead) {

if (EOFencountered) {
return 0;
}

int localtoRead = 0;

while (localtoRead < extraBytes && localtoRead < toRead && extraBytesStart < extraBytesEnd) {
buf[localtoRead++] = extraRead[extraBytesStart++ % BufSize];
}

while (localtoRead < toRead) {
char[] buf4k = new char[4000];
int buytesRead = read4K(buf4k);
int i = localtoRead;
for (; i < localtoRead + buytesRead && i < toRead; i++) {
buf[i] = buf4k[i - localtoRead];
}

// Store the extra Bytes
extraBytesStart = 0;
for (int j = i - localtoRead; j < buytesRead; j++) {
extraRead[extraBytesStart++] = buf4k[j];
}
extraBytesEnd = extraBytesStart;
extraBytesStart = 0;

localtoRead = i;

if (buf[localtoRead - 1] == 0) {
extraBytes = 0;
EOFencountered = true;
break;
}

}
return localtoRead;
}

}

- Anonymous May 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        char[] buf = new char[6];
        read(buf, 6);
        System.out.println("Read the string: " + new String(buf));
    }

    private static final int C_CHUNK_SIZE = 4;
    private static Random random = new Random();
    private static void readChunk(char[] buf) throws IOException {
        Arrays.fill(buf, 'a');
        if(random.nextBoolean())
            throw new IOException("Something went wrong");
        if(random.nextBoolean())
            buf[buf.length-1] = '\0';
    }

    private static void read(char[] buf, int length) {
        int lastIndex = 0;
        char[] temp = new char[C_CHUNK_SIZE];
        while(lastIndex < length) {
            try {
                readChunk(temp);
                for(int i=0; i < temp.length && lastIndex < length; i++) {
                    if (temp[i] == '\0')
                        return;
                    buf[lastIndex++] = temp[i];
                }
            } catch (IOException e) {
                System.out.println("There was an error reading, will try again...");
            }
        }
    }

- kredible May 20, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More