## Microsoft Interview Question for Software Engineer / Developers

• 0

Country: United States
Interview Type: In-Person

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

selection algorithm, find max or min k numbers.

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

Construction of min heap of size n for the given input series and then clearing the min heap will give us the required result. Complexity is O(n log n).

Adding element to min heap is O(log n) as it requires correction of the heap through log levels. For n elements it would be O(n log n).

Comment hidden because of low score. Click to expand.
2

I think it is O(n) to build a heap with an array

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

``````int *findLowestUniqueNumber(int a[], int n){
int m = sizeof(a)/sizeof(int);
int *res = malloc(sizeof(int)*n);
int *temp = malloc(sizeof(int)*m);
memset(res, 0, sizeof(int)*n);
memset(temp, 0, sizeof(int)*m);

int min = 0;
for (int i = 0; i < n; i++){
int minFlag = 0 ;
min = a;
for (int j = 0; j < m; j++){
if (temp[j] == 0){
if (min >= a[j]){
min = a[j];
temp[j] = min;
minFlag = 1;
}
}
}
if (minFlag == 0){
temp = min;
}
}
int k = 0;
// remove 0s
for (int i = 0; i < m; i++){
if (temp[i] != 0){
res[k++] = temp[i];
}
}
free(temp);
return res;
}``````

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

``````def min_heapify(a,i):
l = 2*i+1
r = 2*i+2
min = i
if (l < len(a) and a[l] < a[i]):
min = l
if (r < len(a) and a[r] < a[min]):
min=r
if (min != i):
a[i],a[min] = a[min],a[i]
min_heapify(a,min)

if __name__ == '__main__':
temp = [5,3,17,10,84,19,6,22,9]
print ('enter the number:')
n = int(input())
for i in range(n):
for j in range (int(math.ceil(len(temp)/2)),-1,-1):
min_heapify(temp,j)
print (temp)
temp = temp[1:]``````

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

Could someone elaborate the question. The way i understand it we have a stream of numbers and we have to return all numbers less than a given number (n). And that seems to be a trivial problem.

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

No, you need to find the n minimum numbers.

For [5,3,17,10,84,19,6,22,9], with n=3, return [5, 3, 6]

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

There are two efficient solutions.

1) Create a bst (allow duplicates)out of these numbers. Each time a query comes, it would take O(k) time to get k least numbers. Here the catch is insertion time O(logn)

2) Store all these numbers in an array. Apply selection algorithm each time a query comes. Here it will take O(n) time to figure out the least k numbers. Also there is a huge constant factor involved in this method. Insertion takes O(1). If array size needs to be increased, that will take O(n) time. Also deletion is a problem here.

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.

### 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.