I am preparing for a phone interview. I came upon these questions on the internet. Can anyone tell me some good answers for these?
Suppose I give you a text file and ask you a to write a program that will return a random line from the file (all lines must have equal probability to be returned)
Same as part 1, except this time the entire text file cannot fit into main memory
Same as part 2, except now you have a stream instead of a file.
Please help.
Ok...@Everyone, I really had a few ideas in my mintod before asking this...Seeing the relentless attack by my fellow SOers, I am posting my answers. Please feel free to attack them too...
1: Count the number of '\n' in the file. Generate a random number between 1 and the number and return the line after the number-1 '\n'.
2: Bring the file into main memory part by part and follow the above procedure.
3: I dont have much idea about this and would appreciate any inputs.
Its wonderful that you guys really give an inspiration to push forward.....