Facebook Interview Question: You have a file consists of b... | Glassdoor

Find your next job here

Interview Question

Software Engineer Interview

You have a file consists of billions of records. It cannot

  fit into memory, so you need to reverse every word in that file and save to another file.
file, file read

Interview Answer

2 Answers


Question seems easy, but you cannot read and store it in memory.
You cannot read that by line by line cause that might be just 1 line of records.

Interview Candidate on Jun 18, 2012

Comments are welcomed.

function reverse(){
    //probably can be used to interrupt this function
    global $interrupt;
    $interrupt = false;

    $buffer = '';
    //nextChar is a function to read the next character
    $char = nextChar();
        if( $char === null ){
        //if char is a space, then print the buffer
        if( $char === ' '){
            echo $buffer;
            $buffer = '';
        }else{ //otherwise prepend to the buffer
            $buffer = $char . $buffer;
        $char = nextChar();

Duy Nguyen on Jul 10, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.