Adobe Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

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

while not EOF
  read and reverse in memory the N MB of characters;
  save it in a temporary file and add it to some sort of file stack;

open final file;

while file_stack not empty
  pop the last file;
  open it and write its content in the final file;
  close and delete temp file

close final file;

- S July 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I 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.

- Sanjay July 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- prolificcoder July 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- prolificcoder July 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Triton July 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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... :)

- S July 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

why not?

- chenming831 July 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You have to get to the end... and this consumes time.
I am not saying this is not a solution. I am saying that the interviewer might not like the fact that you read the front of the file each time.

- S July 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

mmap the file?

- Anonymous July 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous July 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops .. mistake above :) ...
We have to take the trival 2 pointers technique to reverse the file .
However , we have direct access to the end of the file now ( if we make use of the filesystem ( the data blocks )) ..

- Anonymous July 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Saurabh@ IIT Kanpur December 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, the file read pointer for the original file must start at the end.

- Saurabh@ IIT Kanpur December 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Shabbas Saurabh...

- chooran Prasad January 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cannot we do it like reversing of link list?? Just start storing the data in list and store the address of that chunk and finally use reverse link list..

- Ashish February 14, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More