Adobe Interview Question
Software Engineer / DevelopersI think we can start reading from the last of the file. Suppose we read last m characters from the file and then reverse them and write it to another file. Then again read last but one m characters from the file reverse it and write them to second file and so on.
I guess you don't even need another file. I don't know why no one has suggested the striaght forward approach of two pointers. One at the start and one at the end.. keep swapping chars till start>=end . This takes maximum 2(n) assuming we have to traverse the entire string to reach the end otherwise this is n/2 :P.
Unless he wants to preserve words (i.e. this is a reversal of words not the string) this approach should work. The problem I think here is each read access could be costly and we want to minimize the number of reads on file.... So now I see the problem. I guess we should simulate something like bufferedstream in java (if we are doing in C/C++) if not the only thing we have to worry about is setting the appropriate buffer size. Bufferedstream is a look ahead stream which loads the next N bits into memory and for this problem N should be roughly equal to half of available memory.
Since the file is very large, and say the memory is of N Megabytes; get the first N/2 Megabytes from the front and N/2 Megabytes from the last (assuming user space in the memory is of N megabytes)..and swap first character with the last, 2nd character with N-1 and so on. When finished get the next N/2 MB from the front and next N/2 from the back , repeat the process. (Termination condition is i>j where i is the pointer denoting the end of the first chunk and j is the pointer pointing to the beginning of the last chunk as viewed from the start).
You cannot just read the last N MB (or N/2) of the end of the file. So, you are doing a lot of work for nothing... :)
Not sure if it is a pure algorithm question.
What about some low level stuff ?
A file is stored in blocks in the harddisk . Any filesystem will have the sequential data blocks holding the data of the file .
So , we have reverse the content of each of the data blocks and also reverse the sequence number of the data blocks ( i.e block 0 becomes block n , block 1 becomes block n-1 .... )
Friends, you people are forgetting that you can not EDIT a file in C, just like you edit an array.
You can open files in C, either in Write Mode (which we can't use here, since it will truncate the contents of the file), or append mode (which always write new Data, at the end of the file, regardless of any calls to fseek)
So the only possible solution is to open another file in the secondary memory, have your read pointer at the beginning in the 1st file, and do a character to character copy.
If its a WORD TO WORD copy, the job becomes slightly complicated, as you need to read the chars in a char buffer, in backward direction, till u get a space or any other delimiter, reverse the string in the buffer, an then write it to the 2nd file.
I think it can be done better than this... This is my solution:
lets say you can read/store N MB of data in memory(N < 1 Giga).
- S July 26, 2010