Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

You can do this with hashing.Example below:

Start with an empty hash.
5 -0 = 5 does this exist in hash?No so put in array[5]
5 -1 = 4 does this exist in hash?No so put in array[5, 1]
5 -2 = 3 does this exist in hash?No so put in array[5, 1, 2]
5 -6 = -1 does this exist in hash?No so put in array[5 ,1, 2, 6]
5 -3 = 2 does this exist in hash?Yes so put in array[5 , 1, 2, 6, 3] note here the 3+2
5 -4 = 1 does this exist in hash?Yes so put in array[5, 1, 2, 6, 3,4] note here 4+1

- aka October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Instead of using HashMap:
if 1) numbers are non-negetive
2) a known upper limit

We can use an int[] and solve the problem.

Initially int[] is zeros. When we meet a number(num) use that as index and flag it to '1' and check if there is another arr[(key-num)] and check if that is also flagged as 1. If so, then we have a pair.
It is of order O(n).

- bluesky October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

care to give an example?

- aka October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ex: input = [1,2,3,4,7,8]. Key: 7.

Array: a[] = {0,0,0....}
for i in length(input) {
a[input[i]] = 1
if(a[key-input[i]] == 1){
print "Pair found"
}
}

- bluesky October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution is no doubt good but it will take up lot of un-necessary space.

- aka October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Somewhere we have to pay the price: either memory or speed (computation). If using HashMap, insert and find operations do cost computation time.
Using array will cost memory but less computation.

- bluesky October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I have a doubt here.... what if the number is a negative number?
eg:) Given array is [0, -1, ....]
how can we take the number in the array as an index? We can't have a negative index right?
The algorithm is good but how can we implement it? :(

- prernac October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how can your solution handle duplicates ?

[1,2,3,6,5,7,5] Sum: 10

- VR October 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

input a[] = {1,11,10,9,8,3,4,5,7,6,2,0}  Key =10;
int first = 0,last = 11;

Sort the array

while(first<last)
	{
		if(a[first]+a[last]==x)
		{
			printf("pair found %d %d\n",a[first],a[last]);
			first++;
			last--;
		}
		else if(a[first]+a[last] >=x)
		{
			last--;
		}
		else
		{
			first++;
		}
	}

- Deepak October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

While this solution works for your input data, it would ONLY work if there are no missing numbers as shown in the initial problem.

- Anonymous October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this solution will also work for missing numbers. This solution is sorting the numbers and in case it does not find array elements with sum = Key value, it wont print anything and continue the loop until first > last

- Deepak October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

-- else if(a[first]+a[last] >=x)
I don't think we need ">=", we just need ">", the equality is already matched in IF statement. Hence it should be
else if(a[first]+a[last] >x)

- Gautam October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

My bad.... it should be '>' instead of '>='.

- DeepakRulez October 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

int[] a = {0,1,2,6,3,4};
		int key = 5;
		for(int i=0; i <a.length; i++)
		{
			for(int j=i+1; j<a.length; j++)
			{
				if((i+j) == key)
					System.out.println("("+i+","+j+")");
			}
		}

- Pankaj Gadge October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

only consider the sum of two numbers whose sum equal to X?

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

What do you mean return true?
Do you want us to return true if there exists a subset of numbers that adds up to that number? Or do you want us to return an array of tuples with the possible combinations?

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

public static void GetAddition(int[] array, int x)
{
int elem = 0;
int[][] arrRes = new int[array.Length][];
int resIndex = 0;
for (int i = 0; i < array.Length; i++)
{
elem = array[i];
int tempres = 0;
for (int j = 0; j < array.Length; j++)
{
if (i != j)
{
tempres = elem + array[j];
if (tempres == x)
{
arrRes[resIndex] = new int[] { elem, array[j] };
resIndex++;
}
}
}
}
}

- SHoeb Bhaldar October 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually,
if we use hashmap, O(n) space complexity and O(n) time complexity
if we use loop, O(1) space complexity and O(n^2) time complexity.

- Kevin March 04, 2013 | 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