## Microsoft Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Written Test

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

Correct me if I am wrong . What if we try to develop an algorithm similar to quick sort ?
1. Set two pointers , one at index(0) as i and other at index(n) as j.
2. If a(i) > a(j) , Increment i else decrement j
3. Stop when i=j
4. No assignments needed right ?

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

Technically, increments and decrements are also assignments.

i=i+1; j=j-1;

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

public void FinMin(int[] array)
{
int minVal = array;
int cnt = 0;
for(int i=1; i<array.length; i++)
{
if(array[i] < min)
{
min = array[i];
cnt++;
}
}
Console.WriteLine("Min Value: {0}, number of assignments: {1}", minVal, cnt);

}

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

@rperla1234

In worst case no. of assignments in above solution will be "n".

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

I would assume that this is the solution they get most often, is there a better way? if we were first to sort the array via mergeSort, would we get a better efficiency?

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

No. Mergesort or other sorting algorithms such as QuickSort take O(nlogn) best case time which is slower then the O(n) time the above algorithm takes.

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

The simplest(yet efficient) solution for the above problem would be the one that most guys are posting here :-

int min(int nArray[], int nLength)
//
int nMin <- infinite
for int i <- 0 to nLength - 1
do
- if nArray[i] < nMin
then
- - nMin <- nArray[i]

return nMin

Time Complexity :- O(n)
Assignment operations :-
Worst case :- n, where n is the length of array (Array sorted in decreasing order)
Best case :- 1, Only for the first time (Array sorted in increasing order)

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

``````#include<stdio.h>
int main()
{
int a,small,n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=a;
for(i=1;i<n;i++)
{
if(a[i]<small)
{
small=a[i];
}
}
printf("smallest=%d",small);
}
as the array is unsorted,so we have to go for linear search
worst case=n assignments within the loop
best case=1 assignment within the loop``````

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.