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 ?

- Srikandh October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Technically, increments and decrements are also assignments.

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

- Varun November 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void FinMin(int[] array)
{
int minVal = array[0];
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);

}

- TechBoss October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@rperla1234

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

- prakhar.asthana04 October 04, 2014 | Flag
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?

- Ben October 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Walter October 29, 2014 | Flag
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)

- sid1505 August 30, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
	int a[20],small,n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	scanf("%d",&a[i]);
	small=a[0];
	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

- apoorva September 27, 2015 | 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