Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

what i think is 1)computer all points' distance to the given point, and put them in an array
2) find the kth element in the array in O(n) time on average, by choosing a pivot from the array and then put all smaller elements in front of the pivot and all larger ones after it, just as quicksort. if # of smaller/equal elements <k, we find the element with rank=( k - # of smaller /equal elements) in the array after the pivot, and discard the other half. Do this recursively.
#include<iostream>
#include<math.h>
using namespace std;

struct point{
int x;
int y;
};

int * computedistance(point * set, int n)
{
int *a =new int[n];
for(int i=0;i<n;i++)
{

a[i]=(int)pow((double)set[i].x,2)+(int)pow((double)set[i].y,2);
}
return a;
}
void swap(int *a, int i, int j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}

int findkth(int * a, int size, int k)
{
if(size==1)
{
return a[0];
}
int pivot=a[size/2];
swap(a,0,size/2);
int left=1;
int right=size-1;
while(left<right)
{
while(a[left]<=pivot&&left<right)
left++;
while(a[right]>=pivot&&left<right)
right--;
swap(a,left,right);
}
int bound;
if(a[left]<=pivot)
{
bound=left;
}
else
{
bound=left-1;
}
swap(a,0,bound);
if(bound==k)
return a[bound];
else if(bound>k)
findkth(a,bound,k);
else
findkth(a+bound+1,size-bound-1,k-(bound+1));
}

void main()
{
point p;
p.x=1;
p.y=2;

int x[]={7,2,4,5,6,4,7};
int y[]={2,3,4,5,6,7,9};
int n=sizeof(x)/sizeof(int);
point * set=new point[n];
for(int i=0;i<n;i++)
{
set[i].x=x[i]-p.x;
set[i].y=y[i]-p.y;
}
int k=1;
int *a=computedistance(set, n);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";

int r=findkth(a, n, k-1);
double result=sqrt((double) r);
cout<<result<<endl;
}

- luckycat March 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

can we use heapsort? Since we just need k-th. readjustment will cost log(n). But we just need readjust k times. So the total cost is klog(n) for sorting part.

- Sean.B.Wan March 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah, we can do this. O(n) time to build the heap + klog(n) tme to extract, it may be better.

- luckycat March 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm afraid using a heap would be more like n*log k, since every value in your set of values still has a possibility of being included in the heap.

- Anonymous March 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Rather than sorting u can create the min heap with those distance in O(n)
And then u can exatract min k time .... well this will give (O(n) + klogn)


The other way is to find first k node(distance) is using variation of quick sort ..... this can be done in O(n) .... check out careercup.com/question?id=4356905

- NaiveCoder March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A little suggestion. we need to create mean heap of size k only and find out the largest element in heap O(n) + O(nlogk )

- Sanjay Kumar March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

use a max heap.. of size k.. in O(n) add each of the distances to the max heap..
maintain the size as k, by adding the new element and removing the maximum element..(the top of heap) ...
all the heap properties ll be done in O(k log k) time..
if k is O(n).. (// assumed to be a big number approx to n)
then the total complexity is O(n log n)..
else if k is a small number , it could be argued for the complexity to be O(n + k log k) = O(n).

- dhandeep March 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Create an array of size k, call it mins
- While computing distances of each of n points from B, keep the minimum ones sorted in the array mins.
- At the end you will have the kth distant point in the kth element of the array mins.
O(n*k)

- amshali March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a k-d tree
it is a data structure used specifically to find n nearest neighbors and u store the squared distance at each node

- RKD March 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

maintain two heap, one max heap takes care of first k, one min heap takes care of rest.

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

Finding Kth order statistic is O(n) when you do something like "partition" part of quick sort. So find all distances and find the Kth order statistic - O(n) time.

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

previously calculate the distance and then put into the array. and apply the quickselect algorithm to find the kth smallest elements.

- RockFord March 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We may use randomized divide and conquer, the time complexity of which should be O(n).

- Jeff September 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

quick select alg is ok for that.

- wangxuyang September 25, 2012 | 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