Microsoft Interview Question






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

Iterate over all files in the directory and Create an array A of strings as follows:
<First word of file> + ":" + <Filename>

Sort Array A

Iterate through the array, parse out the filename and print it

Time Complexity: n + nlogn + n where 'n' is the number of files in the directory.

Space complexity: O(n)

- N/A October 27, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Same as above but insert it in a binary tree and read it inorder instead of sort it..

- Anon December 10, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it should read the sort order will be file B then file A. Sorry for the typo. It's 1 am here :-)

- Interviewee August 28, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Enumerate the contents of the directory. The filenames can be stored in an array.
2. Once you have the list of files, open each of the one by one and do an strcmpi on the first word in that file.
3. Selection sort can be used with this basic idea to end up with a sorted list of files.

- P August 28, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think one key point was missed over here. What if one of the files has the first word = 1 GB. Will you read the whole 1 GB into memory?
My guess is that this will take (O(n*c) + O(nlogn)) (where n = number of files in the directory and c = number of characters that you will have to read inorder to do the sort). What do you all think?
I can send a pseudo algo if anyone is interested.

- temp1 May 23, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

temp1. Please. thank you

- den May 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pseudo Code:
Let n be the number of files
Let c be the total characters read to sort all the files

Data structures:
1. Array of n strings for storing the strings
2. Array of n for length of each string

1. For each file read character from the file and append it to the existing string. O(n * c)
2. Convert the string to a number. For example if the string is abc then the corresponding number is (a * 100 + b * 10 + c). Keep another corresponding array for the length of each string. O(n * c)
3. Sort the numbers using quick sort. If 2 numbers are equal compare their corresponding string lengths. O(nlogn) (Note: this is independent of the length of the string)
4. Check for duplicates in the sorted array O(n). If there are, then repeat steps 1 - 4

There are some particular cases that need to handled like checking for EOF etc when reading from the file but this is the rough logic.

Does this make sense?

- temp1 May 27, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No temp1... If u are reading characters then simply compare their ascii values.. but u must convert each char to either to lowercase or uppercase so that the comparioson is even...

- KSD October 21, 2007 | 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