Microsoft Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
7
of 9 vote

Well actually hashtable is not pratical...because the key space can be very large. The most general 2 ideas are: 1. sort and check 2. build a binary search tree for one of the array, and go through the other.

- Owensy December 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

why using hash is not practical?

- xr February 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

searching in BST for every element is logn in worst case. If you use hashMap its O(1)

- smashit December 14, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

solution 1
sort the two arrays A[],B[],compare every elements Ai,Bj.
i=j=0;
if Ai==Bj ,then add to the result
else if Ai<Bi ,then i++
else j++

vector<int> Intersection(vector<int>& A,vector<int>& B)
{
	sort(A.begin(),A.end());
	sort(B.begin(),B.end());
	int lenA=A.size();
	int lenB=B.size();
	vector<int> ans;
	int i=0;
	int j=0;
	while(i<lenA && j<lenB)
	{
		if(A[i]==B[j])ans.push_back(A[i]);
		else if(A[i]>B[j])j++;
		else i++;
	}
	return ans;
}

- duckling December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for the answer, minor fix tho.

if(A[i]==B[j]){ans.push_back(A[i]); i++; j++;}
		else if(A[i]>B[j])j++;
		else i++;

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

thanks for pointing it out,i missed it in a hurry..

- duckling December 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I exactly did the same. But I used array to store the common elements instead of vectors, which was my blunder. I did not realize it would take away extra memory when there is no common elements or few ones.

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

You don't need to sort both the arrays. Smarter way of doing this would be to compare the sizes of the array, and sort the array with smaller size, thus yielding less time. Once, and array is sorted, you can traverse through all the elements of the other array and do a binary search on the sorted one. If the element exists, you add it to a vector and return the vector.

- Rushabh October 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

We can do better then O(max(n log n,m log m))

Put all the elements from A in a HashMap (key = element, value = 1)
for each element in B if exists in the HashMap (this check is done in O(1)) then added to the returned structure.

Complexity : time :O(max(n,m)) ; space: O(min(n,m)). // we assume ^_^ that A.length < B.length.

- Kamy December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

basically, we can use either HashMap or ArrayList

- Kamy December 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Shouldn't we be writing the complexity as O(m+n) :)

- The Artist December 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you can say O (m+n) or O(max(m,n)) because : lets say m > n so m = n + x => m + n = n + n + x = 2 * n + x = > O( m + n ) = O(2*n) + O (x) = O(n) + O (x) = O(n+x) = O(m) and we know m is the max between m and n.

Let me know if I'm wrong

- Kamy December 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, you are right. I was just pulling legs :P sorry about that. Even if m = n then O(m+n) = O(2n) = O(n) and therefore there ain't any difference between O(m+n) or O(max(m,n)) as m, n tends to infinity

- The Artist December 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Algo:

Step1 : Sort the first array.(m log m)
Step2 : Then for each elements of second array, do a binary search on the first array, if found print the value.(n log m).

No extra space is required.

- Anonymous December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given {1,2,4} and {2,4,2} this algorithm would return {2, 4, 2} as opposed to just {2, 4}. And if we are supposed to return duplicates, such that given {1,2,2,4} and {2,4,2} we should return {2,2,4}, then it's even worse.

- Sunny January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

import java.util.HashMap;

public class Q1 {

	public static void main(String arg[]) {
		HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
		int a[] = { 2, 3, 6, 42, 6, 43, 2, 3 };
		int b[] = { 42, 5, 7, 3, 8, 34, 2, 2};

		h.put(2, 2);
		h.put(3, 2);
		h.put(6, 2);
		h.put(42, 1);
		h.put(43, 1);

		for (int i = 0; i < a.length; i++) {
			int temp = b[i];
			if (h.containsKey(temp)) {
				if (h.get(temp) > 0) {
					System.out.print(temp + ", ");
					int count = h.get(temp) - 1;
					h.put(temp, count);
				}
			}
		}

	}

}

- Anonymous January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

1.merge both the arrays
2.sort
3.return duplicate items
}

- chetan kumar b v January 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

negative

- Tom April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ HashSet<int> h1 = new HashSet<int>(); HashSet<int> h2 = new HashSet<int>(); int[] a = { 2, 6, 2, 4, 7, 5, 8 }; int[] b = { 5, 4, 3, 1, 2, 5 }; for (int i = 0; i < a.Length; i++) { h1.Add(a[i]); } for (int i = 0; i < b.Length; i++) { h2.Add(b[i]); } Console.Write("Common elemnts in Two arrays : "); foreach(int i in h1) { if (h2.Contains(i)) { Console.Write(i + " "); } } Console.ReadLine(); - Anonymous January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ HashSet<int> h1 = new HashSet<int>(); HashSet<int> h2 = new HashSet<int>(); int[] a = { 2, 6, 2, 4, 7, 5, 8 }; int[] b = { 5, 4, 3, 1, 2, 5 }; for (int i = 0; i < a.Length; i++) { h1.Add(a[i]); } for (int i = 0; i < b.Length; i++) { h2.Add(b[i]); } Console.Write("Common elemnts in Two arrays : "); foreach(int i in h1) { if (h2.Contains(i)) { Console.Write(i + " "); } } Console.ReadLine(); - Anonymous January 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int[] first = new int[] { 1, 4, 3, 2, 6, 5, 16, 7 };
int[] second = new int[] { 8, 3, 5, 4, 2, 1 };

Hashtable result = new Hashtable();
foreach (int i in first)
{
result[i] = 1;
}
foreach (int i in second)
{
if (!result.ContainsKey(i))
{
result.Remove(i);
}
}

- Anonymous February 04, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can anyone help me understanding the complexity of this code?

- Sandy February 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we need to ask interviewer how to handle duplicate common element?
Then we could use different methods to solve it!

- Kevin March 01, 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