Interview Question
My take on this is: Lets say we need to reach line 77 [ i.e 77 is input line #].
Given, constant buffer can store only 10 lines. Other condition is that reading is only once => If sequential reading is ruled out. Reading is only one time and data comes in batch of ten lines.
My understanding is that we need to choose a batch where line 77 comes in as input.
So, in order to read line 77, We need to fetch batch # = 77 / (input capacity of buffer) = 77/10 = 7
i.e from the text file, leave first six batches [or first 7 blocks or 70 lines] and read in lines 71 - 80.
Now once buffer is filled, go to line 77%10.
In this way given line is reached in 2 steps.
Any other views are welcome.
This is Amazon 1st round
- Anonymous March 13, 2010