Microsoft Interview Question for Dev Leads


Country: United States




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

I'm thinking insertion sort for the first case.

For the second case, if it is mostly unsorted, then a standard mergesort or quicksort should do the job. If it is still mostly sorted (just not the extreme case given), then mergesort would probably be safest. In both of these second cases, radix sort could be best depending on the domain.

- Lucas Pantalones January 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

right, insertion sort is the winner in case of given example.

- zr.roman January 26, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

search for these glitches (unordered elements in nearly sorted array) and insert them at appropriate place(insertion sort on few elements)

- vishalsahunitt January 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Patience sorting? It gives a linear performance when data is pre-sorted. In case of only a few out-of-order elements, its performance will be close to linear.

- Lokesh January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For nearly sorted data insertion sort will be best. Let maximum displaced elements are k distance away. For ex. (3 2 1 4 6 5) k = 3. Then insertion sort will be O(kN).
If elements are nearly sorted like (3 4 5 2 1) few elements are n distance away and rest are sorted. Then also insertion sort will be O(kN)

- sanjiv kumar January 26, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, the way you wrote the example, I would wonder if the interview is actually asking about a rotated, linear data structure (e.g. rotated array or DLL).

If the answer is yes, then I would apply a binary search to identify the point of rotation (a situation in which the left element is greater than the adjacent, right element). Once the split point is a found, a series of shifts would occur for an array, or a single pointer adjustment for a DLL, in order to change to a sorted list.

O(lgn) to find + O(1) for a DLL
or
O(lgn) + O(n) for an array

This would be better than any comparison-based sorting.

- Jason January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Insertion sort takes maximum time to sort if elements are sorted in reverse order. And it takes minimum time (Order of n) when elements are already sorted.

- Aakriti January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Partially sorted lists --> Insertion sort is winner.

- Anonymous January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

bubble sort is the best for nearly sorted array

- fasil January 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

in case of given example the best is insertion sort (14 iterations) against ShellSort (21 iterations), bubble sort (28 iterations) and selection sort (28 iterations).

- zr.roman January 26, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bubble sort allows an "out-of-place" element to travel long distance in a single iteration. Typically it works in one direction, but why not change the direction every second table scan? This approach looks best to me in case of a "nearly sorted" array.

- hb March 11, 2016 | Flag


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