Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Interview Question


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(); 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.