Microsoft Interview Question for Software Engineer / Developers


Team: windows
Country: United States
Interview Type: In-Person




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

There's an elegant solution(publicized, not my own) to this problem.

Median of a continuous stream of numbers can be found out by:
>> Maintaining two heaps. 
>> Max heap and a Min heap
>> Max heap keeps track of first half of the numbers.
>> Min heap keeps track of the second half.
>> The root elements along with the TOTAL number of elements will give you enough info to find the median
>> Take care in adding new elements to the heaps. You might need to delete and push into the other heap.

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

Yep, this is more or less the optimal solution. You can also do something similar with a binary tree.

- eugene.yarovoi February 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the source with more details would be appreciated.

- Venky February 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

more explanations here:
h t t p ://w w w .careercup.com/question?id=421668

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

hi all,

my answer was

1. create a BST and a counter.

2. each time you get a number insert it into the BST and increment the counter.

3. each node in the BST has a field "rank" that is the number of nodes in the subtree rooted at that node.

4. when median is asked return the node whose rank = counter/2 if counter is odd else return the node whose rank = (counter/2)-1

first the stream is 0,1,2,3,4. the tree will be
0(4)
\
1(3)
\
2(2)
\
3(1)
\
4(0)
counter = 5
now median is 2 as rank(2) = (counter/2).

now comes -2. the tree will be

0(5)
/ \
(0)-2 1(3)
\
2(2)
\
3(1)
\
4(0)
counter = 6
(counter/2)-1 = 2
median is node 2 as rank(2) = 2.

now comes -4. the tree will be

0(6)
/ \
(1)-2 1(3)
/ \
(0)-4 2(2)
\
3(1)
\
4(0)
counter = 7
counter/2 = 3
median = 1 as rank(1) = 3

please tell me if there is any problem in this algorithm. please try it on some other examples and if you find anything not working properly then please notify.

thanking you all

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

Your solution is correct, but there is cases where it works with quadratic time, because your BST is not balanced and you can't control input values.

- KVF February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

since the range is always sorted median will be at n/2 always.
lets say the median value at the beginning ie Array[n/2] is k.

from here whenever you are inserting any value to the array check if that value is > k or <k.. ie are you inserting in left half or right half.. and keep count the number of insertion in left half and in right half.
At the end calculate l
j+(lefthalfcount -righthalfcount)/2..
j+k would be the next median.. please tell me if you see any mistake

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

to all of you who have implemented array to find the median : the input is a continuous string of integers. that means integers are coming continuously. so the number of integers tends to infinity theoretically and practically you are not obviously supposed to store them all in an ARRAY to find the median. if you do that then your program will crass after the memory is filled. this type of questions are asked to check if you are able to think of space complexity or not. so i would suggest never start with an array. it'll suddenly cost you negative points in the interviews.

and the answer of kill is the best described and the optimal one. my answer would work but i can get median in O(log n) time where kill can do it in O(1) time.

thanks friends for replying to this question.

- CAFEBABE February 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

since the range is always sorted median will be at n/2 always.
lets say the median value at the beginning ie Array[n/2] is k.

from here whenever you are inserting any value to the array check if that value is > k or <k.. ie are you inserting in left half or right half.. and keep count the number of insertion in left half and in right half.
At the end calculate l
j+(lefthalfcount -righthalfcount)/2..
j+k would be the next median.. please tell me if you see any mistake

- m February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{its j= (lefthalfcount -righthalfcount)/2}

- m February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it can be simply maintaining an AVL tree. the median value will be the root always (in case number of nodes are odd)

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

Find sum of all numbers.
Multiply each integer with the total number of integers in the stream.
Median will be the integer which is more close to the sum,in case of even number of integers, find two such numbers else only one

- Anonymous February 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 4 vote

store all elements in array den
int i,n;
for ( i = 0; i < 1000; i++ ) {
if ( array[i] == '\0' )
{ n=ceil((i-1)/2);
cout<<array[n];
break;
}
}

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

modification
store all elements in array den
int i,n,j;
for ( i = 0; i < 1000; i++ ) {
if ( array[i] == '\0' )
{
if((i-1%2)==0)
{
n=ceil((i-1)/2);
cout<<array[n];
break;
}
else
{
n=ceil((i-1)/2);
j=floor((i-1)/2);
r=(n+j)/2;
cout<<r;
break;

}
}

- Kaushik February 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

store all elements in array den
int i,n;
for ( i = 0; i < 1000; i++ ) {
if ( array[i] == '\0' )
{ n=ceil((i-1)/2);
cout<<array[n];
break;
}
}

- Kaushik February 01, 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