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

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.
Tags:
file, file read
Answer

Interview Answer

2 Answers

1

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
0

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();
    do{
        if( $char === null ){
            break;
        }
        //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();
    }while(!$interrupt);
}

Duy Nguyen on Jul 10, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.