Microsoft Interview Question
Software DevelopersCountry: India
Interview Type: In-Person
1. Read file line by line and into a new file. Whenever new file size is greater than 2.5 GB create a new file and start adding into new file.
2. Now sort every file individually.
3. Using sorted merge algorithm, read line by line from all file and only add smaller data into new file and move to next line of that particular file.
I don't know what he expects, but I would do following:
- Alexey March 25, 20201) split 10GB file into 4 chunks (read row by row until I fill about 2.5 Gb of memory)
2) sort each chunk in memory using quicksort (because it does not consume additional memory)
3) write every chunk back to the disk
4) using mergesort algorithm to merge data back to single sorted array reading from chunks by pieces and writting it simultaneously to the disk into the destination file.