Microsoft Interview Question for Software Engineer in Tests






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

Let 'k' be sum. Read the array and start updating in hashtable. for each update check if k-a[i] is available in hash table, if not then update else print the sol.

This will be in O(n).

- Anonymous October 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your solution is O(n) in space and in time also.
If the interviewer wants a O(1) in space, I think the best algo is O(n logn) = sort array, and then have two pointers - one at the 0 and one at n - and compare the current sum with k and then increase or decrease one pointer.

- S October 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i was asked the same question in my interview with Amazon. I gave a hash based algorithm and the interviewer was fine with it. The solution is not O(n) space complexity. It is infact O(SUM) because you insert or retrieve elements that are less than SUM. if SUM >max(n) then space complexity is O(n).

- chennavarri October 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@chennavarri... suppose you have this array: -1, -2, -3, and the next n numbers (negatives).
What is the complexity? O(-n^2)?
If you are using an hash, you will use in the worst case n different "cells". Therefore, the solution with hash is O(n) in space.

- S October 10, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We need o(sum) as space.
let 2,4,10,6,8,5 and sum =15
then first we insert f[2] and f[15-2].
then we insert f[4] and f[11] and so on..
so we need to maintain hash of sum

- Ashish October 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What group was this for?

- Metta October 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If using no extra space, then O(n*logm) is the best solution I think.

- Messi October 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you highlight the sorting method in detail ??
How do u decide to increase/decrease the two pointers ??

- Ankur October 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

for each element of the array do: sum - array[i] and store that in a different array.
from the second array, search the elements in the first array
O(n + n lg n) = O(n lg n)

- Vinay October 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the sorting method, complexity is O(n^2):
First pointer points to the 0th element and you advance the second pointer from the 1st to the nth element until sum of 0th and ith element exceeds the desired sum or the end of list is reached.
When exceeded, you need to advance the first pointer to the 1st element but while doing this, you also need to set the second pointer to the 2nd element, you cannot just keep the second pointer where it is. So it becomes O(n^2)..

- erm October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If it is going to be O(n^2), why do we need to sort it? Can't we find the pairs without sorting, in O(n^2)?

- Sathish October 17, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i totally agree, thats the point already, its a useless method

- erm October 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Sort the array, say in ascending order.
2. Have two references: one to the beginning(left) of the sorted array and one at the end(right).
3. while right > left
3.0 Check if the two add up to the sum.
3.1 If yes, return them.
3.2 Else if their sum is < required sum, increment left.
3.3 else decrement right.
4. If we get out of the above loop, there are no two numbers that add up to sum.

This runs in O(nlogn + n/2) ~ O(nlogn), depending on the sorting method that is used.

- saurako October 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I like this solution. That looks elegant.

- MikeZ November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I apologize,
the method of S works fine.
one of the two pointers shall be on 0th element and the other on the nth element.
They will be increased/decreased by comparing with the desired sum.
That will be O(n), so the total complexity will be O(n logn)

- erm October 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can solve it in o(2*n) also....

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

Store the array into a hashmap.
Traverse the array again, and for each element Arr[i], check whether Sum - A[i] exists in the hashmap.

Since, hashmap has constant put/get times, this gives a linear solution in the size of the array.

--Chander

- Chander Shivdasani October 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A hash table does not have constant put/get times. A hash table has expected constant times, worst case O(n) time. This is a very fundamental and important thing to understand.

- Anonymous January 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use a HashSet with O(2n) space complexity and 0(n) time complexity

- Mandar November 14, 2010 | 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