Microsoft Interview Question for Software Engineer in Tests






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

I answered Bucket sort or quick sort are the best for this prob. Any suggestions?? Is there a better algo for this problem?

- Sadineni.Venkat January 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If there are only two type elements(0/1) then go through all the elements, maintain two counters which would count # of 0's and 1's. Finally replace the whole array with x # of 0's and then y # of 1's.

This algo would take O(2n)=O(n) steps

- Prince January 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dutch National Flag Algo : O(n)

- Cougar_CS January 11, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hope this link help. It has the implementation for both with counter and quick sort.

http://mganesh.blogspot.com/2009/01/arrange-0-1.html

@Prince ==> your method will have the complexity of O(4n). See the above link for reference.

- Ganesh M January 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

oh yaar its simple count the number of times loop went and count number of ones in that loop. number of zeros = loop count - 1 count. either regenerate it during counting.

- ans January 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You just need to apply counting sort which would take O(2+n)steps.
Prince is correct!

- Ranjit January 30, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Prince sol is nice using bucket , but same can be done using less no of insertion and deletion just start two counter one from start and one from end,keep on incrementing start till 1 encountered by start counter and 0 encountered by end counter then swap them . stop doing this when start is grater then end.

- Hemant February 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Start from both ends of the array, swap 0s and 1s as required

- Dumbo February 22, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do you swap 1 and 1, 0 and 0?

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

If there are only two type elements(0/1) then we can also use counting sort. It has an advantage of being a stable sort and runs in O(n+k), where k is the range of numbers, in this case k = 2.

- Ankit Gupta August 02, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Dumbo is correct ......though it is not a stable sort (we do not need one here :-) )....

does the work in O(n) and no extra space .

- Anonymous September 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sortArraysOfOnesAndZeros(int[] a, int n)
{
int start = 0;
int end = n-1;
while(end > start)
{
if(a[start] > a[end])
{
swap(a[start], a[end];
}
else if(a[start] == a[end] == 0)
{
start++;
continue;
}
else if(a[start] == a[end] == 1)
{
end--;
continue;
}
start++; end--;
}

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

Complexity is O(n).

public static void SortZeroAndOne(int[] arr, int n)
        {
            int i=0,j=n-1;
            while (i < j)
            {
             
                if (arr[i] == 1)
                {
                    while (arr[j] != 0&& i < j)
                    {
                        j--;
                    }
               if(i==j) break;
                    if (arr[i] != arr[j] )
                    {
                        int temp = arr[j];
                        arr[j] = arr[i];
                        arr[i] = temp;
                    }
                }
                ++i;
            }
        }

- BVarghese October 07, 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