Microsoft Interview Question for Software Engineer in Tests






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

As you scan the Array Generate a Max heap as you see an Even number and Create a Min Heap for Odd number. Once all the elements in the array are scanned. Perform heap sorts on both heaps.

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

just overwrite the < operator and do quicksort.

- TV May 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome solution. Hats-off

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

Awesome solution. Hats-off(2)

- Zaphod May 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice solution. But to simplify things, we can just partition input array to 2 parts - even and odd, and then do sipmle quicksort for both sub-arrays. I believe this approach will use less comparisions.

- gevorgk May 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can someone explain what he meant by overwrite > operator ?

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

gevorgk 's solution will take O(n+nlogn) i.e O(nlogn) time.
we can use two pointers approach to partition the array into odd and even subarrays (just like sorting 0's and 1's) in O(n) time and then apply quick sort to subarrays.

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

Will the soln given by bastard TV work? How?

- netappreject May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my implementation suggested by TV. Any comments are welcome!

void swap(int& x, int& y)
{
int t;

t = x;
x = y;
y = t;
}

int Partition(int a[], int l, int r)
{
int v = a[r];
int i = l-1;
int j = r;

while(j>i)
{
while(true)
{
++i;
if(a[i]%2==1 && v%2==1)
{
if(a[i] <= v)
break;
}
else if(a[i]%2==1 && v%2==0)
{
continue;
}
else if(a[i]%2==0 && v%2==1)
{
break;
}
else
{
if(a[i]>=v)
break;
}
}

while(true)
{
--j;
if(a[j]%2==1 && v%2==1)
{
if(a[j]>=v)
break;
}
else if(a[j]%2==1 && v%2==0)
{
break;
}
else if(a[j]%2==0 && v%2==1)
{
continue;
}
else
{
if(a[j]<=v)
break;
}
}

if(i>j)
break;

swap(a[i], a[j]);
}


swap(a[i], a[r]);
return i;
}

void mySort(int a[], int l, int r)
{
if(l>=r)
return;

int m = Partition(a, l, r);

mySort(a, l, m-1);
mySort(a, m+1, r);
}

- dullboy May 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

void swap(int& x, int& y)
{
int t;

t = x;
x = y;
y = t;
}

int Partition(int a[], int l, int r)
{
int v = a[r];
int i = l-1;
int j = r;

while(j>i)
{
while(true)
{
++i;
if(a[i]%2==1 && v%2==1)
{
if(a[i] <= v)
break;
}
else if(a[i]%2==1 && v%2==0)
{
continue;
}
else if(a[i]%2==0 && v%2==1)
{
break;
}
else
{
if(a[i]>=v)
break;
}
}

while(true)
{
--j;
if(a[j]%2==1 && v%2==1)
{
if(a[j]>=v)
break;
}
else if(a[j]%2==1 && v%2==0)
{
break;
}
else if(a[j]%2==0 && v%2==1)
{
continue;
}
else
{
if(a[j]<=v)
break;
}
}

if(i>j)
break;

swap(a[i], a[j]);
}

swap(a[i], a[r]);
return i;
}

void mySort(int a[], int l, int r)
{
if(l>=r)
return;

int m = Partition(a, l, r);

mySort(a, l, m-1);
mySort(a, m+1, r);
}

- badass May 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

shift all odd elements to left and even elements to right and form a min heap for even and max heap for odd elements..

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

best method is to multiply odd elements by a negative sign and then sort complete array afterwards remove negative sign

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

but ur code would be of complexity O(n^3)

- Anonymous October 25, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how would it be O(n3) its O(n + nlogn). I think this i much better than the solution proposed by TV.

- Anonymous November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I agree with multiplication answer since there is no guarantee that even/odd indices shall be preserved after using overloaded < operator. If original array contains -ve numbers as well then we need to find minimum negative number and then add that to all numbers, then do merge sort and finally subtract the negative number we added before. Complexity O(n log n)

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

1: Multiply the odd elements by -1
2. Sort the numbers
3. Again multiply the odd numbers by -1.

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

I have one basic solution ,Yes It may have some space complexity

1)Do the Sorting for an array(Ascending Order)
2)As We know Odd Number should be in Increasing Order So Start to read the sorted from the end Till 0 subscript and read the same array from the starting for even number till a[n]
3) Store Evan and Odd Numbers in the separate arrays

So We can get easy solution for this prob Not Much Complexity Analysis.
Am i right ?

- RUser April 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int n;
scanf("%d",&n);
int ar[n];
for (int i=0; i<n; i++) {
scanf("%d",&ar[i]);
if (ar[i] % 2) {
ar[i] *= -1;
}
}
sort(ar, ar+n);
for (int i=0; i<n; i++) {
if (ar[i] % 2) {
ar[i] *= -1;
}
printf("%d ",ar[i]);
}
printf("\n");
return 0;
}

- shubham October 08, 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