Microsoft Interview Question for Software Engineer in Tests






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

i don think there's any sorting technique in O(n) time

- Anonymous May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given extra space, make use of SortedDictionary. Traverse the list once and store in SortedDictionary's value the number of times the key appears. Loop over the SortedDictionary.

- Adi June 12, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorted dictionary uses binary tree internally which would cost O(nlogn).

- foobar June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

counting sort

- Anonymous May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

see the range of values and assumption made in counting sort........... counting sort fails on large range

- Anonymous May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Radix sort does not suffer from large range of numbers and its O(n)

- Ashish Kaila June 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

i don think there's any sorting technique in O(n) time

- Anonymous May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i don think there's any sorting technique in O(n) time

- Anonymous May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use any "Non-comparison sorts" (see wiki)

- celicom May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void sortUnsortedArray()
{
int[] arr = new int[] { 1,3,2,1,6,45,6,7,3,67,43,499,9 };
int i=0, j=0, count = 0;
int[] tempArr=new int[500];
for (i = 0; i < arr.Length - 1; i++)
{
j = arr[i];
tempArr[j] = tempArr[j] + 1;
}

arr = new int[12];
int intAdd=0;
for (i = 0; i < tempArr.Length; i++)
{
if (tempArr[i] > 0)
{
count = tempArr[i];
while (count != 0)
{
arr[intAdd] = i;
count -= 1;
intAdd += 1;
}
}
}
}

Please correct this answer if any deviation.

- Here is the solution with o(2n) ~ O(n) May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's not O(2N), it's O(N+M), where M is the max value in the array. And how did you magically arrive at M (500) without any work?

Counting sorts are good when the range of the values is given, but that's not the case here.

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Bucket sort should work

- azgirlcoder May 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To sort integers, I think most efficient way is using radix/bucket sort. It needs O(kn) time where k is < 20 for 64 bit int. So, we can claim that it's O(n) indeed. It needs O(n) space like merge sort & counting sort. Counting sort is a lot simpler than radix sort, but it needs space of O(max-min) where max (min) is largest (smallest) item in the array.

- anonymous May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) is a deception. Cos k is not a constant its arbitrary

- Anonymous May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(n) is a deception. Cos k is not a constant its arbitrary

- Anonymous May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nonsense, Anonymous.

- ianam May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ianam which anonymous u refer to?

- Anonymous May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@ianam which anonymous u refer to?

- Anonymous May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To do bucket sort you need the assumption that the numbers are uniformly distributed within a certain range. Even then, the analysis only gives *expected* linear time..

- magritte128 July 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

counting sort ,

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about using hashing technique ?

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hash the elements into a table.. Since there are duplicate values, associate a counter with each hash entry.. if there is a collision, increment the counter.. Once done (O(n)), read the table entry sequentially and multiply by counter and display the number that many number of times..

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

"read the table entry sequentially" -- requires sorting the keys; you lose.

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

But, the precondition to produce expected result by reading table entries sequentially is that the entries are sorted. Your solution shows no magic how it will happen without a non-linear sorting process involved.

- Anonymous May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. set the appropiate bits in a bit array
2. print the array index whoes bit is set.

- guru May 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Though it's O(n) in term of time complexity, but it's O((max-min) space complexity, where max-min can be the full range of integer type.

- Anonymous May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

isnt it O(max-min) in time too?

- camSun May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Fails for duplicate values (which the original problem contains). Also, it's not O(N), it's O(M), where M is the max value.

- ianam May 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not create build a multimap from the elements and print it out. I think keys in the map data structure are sorted??

- Infinity May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You must believe in magic. An O(1) map is not sorted, and a sorted map is not O(1).

- ianam May 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The insertion into a map if it is implemented based on Red Black tree would be log(n) to search and another log(n) to insert. And while printing it would again be logarithmic.

- Infinity May 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Following are the steps to solve it in O(n):

1) Create a BST for the array
2) do inorder traversal

Please correct me if I am wrong

- jay May 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you cant build a BST in O(n). any kinda comparison sort algorithm have the lower bound of O(nlogn).

- foobar June 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@jay - The best insert complexity for any "balanced" tree is min O(log n) amortized...and unbalanced BST has worst case O(n)...so your solution wont be O(n)...it might be worst case O(N^2) itself...
only counting sort/bucket sort would be O(n) worst case..

BTW its not written in question that we need worst case complexity?

- Anonymous May 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In my opinion, Radix sort is good enough for this question...

- Anonymous May 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Radix sort will do it in O(kn) where k = max possible no. of digits in an element.
for the example case-
1 1 1 2 2 4 444 44 5 5 45 45 6
1 1 1 2 2 4 4 5 5 6 444 44 45 45
1 1 1 2 2 4 4 5 5 6 44 45 45 444
Each of the above three steps takes O(n)
So the total time complexity becomes O(3n) which is basically O(n)

- VIP May 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This approach will fail if all are dupilicate. Then result will be O(nXn).

- sidhartha.mahapatro May 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Radix sort doesn't perform any comparisons, so the fact that every element in the array is the same should have no effect on the algorithm. And, in fact, the algorithm doesn't decay to O(n^2) if all elements are duplicates.

- Orr May 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I second "Orr"... Radix sort is the best, and is not depended upon the values at all !!

- Saurabh July 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess it is radix sort, which in this case is O(3n).

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

Counting sort works. Keep a bit vector representing the values and hash the count in hash table. Then check bit vector and pull count from hash table and print the number as many times as count.

- Ashish Kaila June 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

OK, there are lot of misinformation with the comments and answer. First thing you need to know is that any kinda comparison sorting algorithm you pick will have a lower bound of O(nlogn), you cant do any better than that.

In that case, you will need to use bucket sort, count sort or radix sort. Which will amortize O(n) solution. I see answers like O(3n), there is no such notion in algorithm.

O(n) == O(3n)== O(100n). there are all same. linear time.

So the answer is non comparison sorting algorithms.

Easy answer would be, I find the max element and then create a dictionary from 0 to max.

Keys will be the sequence from 0 to max, and values would be the occurences.

At the end, you traverse the dictionary once and voila.

- foobar June 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is just Counting Sort...
and it requires really lot of space, specially when numbers are spread over a wide range !!

- Saurabh July 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer is "Spaghetti Sort" which is possible using quantum computing. Check the wikipedia page (I can't paste the links here)

- Sathish June 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I couldn't find any implemetation of Spaghatti sort.

- rchopra@computerengineerings.com July 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the range is known then we can imlement this o(n) time
but if the ange is not given and can be go beyond the range of machine then no easy to to do better than the normal agorithm like 0(nlgnn) do in heap sort and merge sort or average case of quick sort

- geeks July 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As a lot of people already said.. there's no comparison sorting algorithm having a worst case running time better than O(nlgn). Out of the Linear time Sorting algorithms, in this particular case, Radix Sort is best suited !!

- Saurabh July 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

They say that in O(n) and it is only for integer. So we can the following:
int[] unsorted; //here is the input
int[] output;
int[] tmp;

int min=FindMax(unsorted);//O(n)
int max=FindMin(unsorted);//O(n)
int length=Math.Abs(max-min)
output=int[length];
tmp=int[unsorted.length];
for(int i=0;i<length;i++)//O(n)
{//Initializing
tmp[i]=0;
}
int inp;
for(int i=0;i<length;i++)//O(n)
{//Using the value as an index
inp=input[i];
tmp[inp-min]+=1;
}
int index=0;
int j=0;
for(int i=0;i<length;i++)//O(n)
{
if(tmp[i]!=0)//No occurrences
{index=0;
while(index<tmp[i])
{
output[j]=i-min;
j++;
index++;
}
}
//The final anwser is in output
//O(5n)==O(n)
}

- gerardo.ruiz@aiesec.net December 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think radix sort would be the best. As its complexity is O(N*d) where d is negligible as compared to N.
Here is the implementation.

#include <stdio.h>
#include <limits.h>
#include <math.h>
 
int findMaxDigit(int *arr,int size)
{
        int i,max=INT_MIN,n=0;
        for(i=0;i<size;i++)
                if(arr[i]>max)
                        max=arr[i];
        
        while(max)
        {
                n++;
                max/=10;
        }
        return n;
}
 
int isEmpty(int *r,int j)
{
        return r[j]==-1;
}
 
void Enqueue(int (*Q)[10],int *f,int *r,int digit,int n)
{
        if(f[digit]==-1)
                f[digit]++;
        Q[++r[digit]][digit]=n;
}
 
int Dequeue(int (*Q)[10],int *f,int *r,int digit)
{
        int n=Q[f[digit]][digit];
        if(f[digit]==r[digit])
                f[digit]=r[digit]=-1;
        else
                f[digit]++;
        return n;
}
 
void radixSort(int *arr,int size)
{
        int i,j,k,Q[size][10],f[10],r[10],d,digit,top=-1;
        
        d=findMaxDigit(arr,size);
 
        for(i=0;i<size;i++)
                f[i]=r[i]=-1;
 
        for(i=0;i<d;i++)
        {
                for(j=0;j<size;j++)
                {
                        digit=(arr[j]/pow(10,i));
                        digit%=10;
                        Enqueue(Q,f,r,digit,arr[j]);
                }
                top=-1;
                for(j=0;j<10;j++)
                {
                        while(!isEmpty(r,j))
                                arr[++top]=Dequeue(Q,f,r,j);
                }
        }
        for(i=0;i<size;i++)
                printf("%d ",arr[i]);
}
 
int main()
{
        int arr[]={4,2,6,1,5,5,1,2,45,444,44,45,4,1};
 
        radixSort(arr,sizeof(arr)/sizeof(arr[0]));
 
        return 0;
}

- Aashish June 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

simple

int[] Unsorted_integer = { 4, 2, 6, 1, 5, 5, 1, 2, 45, 444, 44, 45, 4, 1 };
Array.Sort(Unsorted_integer);

- Anonymous September 06, 2013 | 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