Microsoft Interview Question for Software Engineer / Developers


Country: United States




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

bit maps is one probable solution if the data range is small, other wise it takes lots of space

time complexity O(m+n)

- algos August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can do it even if the data range is large also right ?..

- rohith August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes, this solution works for larger data sets also

- algos August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Algos, could you explain how bit map can be used here? What is the entry in map?

- Kallu August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea.+1

- Murali Mohan August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

your solution is correct, here the pit falls is how to map a -ve no to the corresponding bit in the bit map.

For each X in the array, you should add 2^(n-1) to get the correct bit position in the bit map. Here n is the no of bits used to represent X (ie, 8*sizeof(X))

- Vin August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

Using two bitmaps will solve the problem of negative numbers. Use one bitmap just for the positive numbers and another bitmap for the negative numbers. Also to know what should be the size of bitmaps, just find the largest positive element and smallest negative element in both arrays.

- as09 August 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Bit map may not be a good one if the data range is big. The time complexity here may become max_number_in_array/8 for the AND operation and this could be ridiculously big.

- houzaizai May 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
3
of 5 vote

For Intersection of two sorted arrays, print the element only if the element is present in both arrays.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then increment i.
3) If arr1[i] is greater than arr2[j] then increment j.
4) If both are same then print any of them and increment both i and j.

int printIntersection(int arr1[], int arr2[], int m, int n)
{
  int i = 0, j = 0;
  while(i < m && j < n)
  {
    if(arr1[i] < arr2[j])
      i++;
    else if(arr2[j] < arr1[i])
      j++;
    else /* if arr1[i] == arr2[j] */
    {
      printf(" %d ", arr2[j++]);
      i++;
    }
  }
}

Time Complexity: O(m+n) for sorted arrarys.

For unsorted arrays Time complexity will be O(mlogm + nlogn)

Note: Sorting phase takes O(nlogn)

- pirate August 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Your code is not a generic one. Please do consider the case where the arrays are unsorted.

- ganeshjwhr August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will this algorithm work even for unsorted arrays? I suppose not.

- Inappropriate name August 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

First sort the arrays and then apply this algorithm then it will work . That is why he said that the time complexity will be O(mlogm+nlogn).

- vgeek August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Who said arrays are sorted ?

- smashit December 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why should we even go for extra memory?

One approach we could follow is:

1) Get the length of the first array and second array. say N and M respectively.
2) Get |N-M| and move till that length in the larger array.
3) Now both are at equal start point, keep checking the values. When you find the match, from then it should be a intersection array.

- Sunil May 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Comparison only algorithms have proven lower bounds which are O(K log K), where K = min (m,n).

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

that is Omega(K log K).

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

If the question instead was to solve the problem for two sorted arrays, there is a simple comparison-based O(m+n) time algorithm whose logic is very similar to the "merge" step of mergesort. Step through the two arrays in the same way that the merge algorithm does, and when the next element from array1 is equal to the next element from array2, add the common number to a list of results.

For two unsorted arrays, you can solve using a hash set, but this is O(m+n) only in the expectation and not in the worst case. You can solve the problem in worst-case O(m+n+R) time where R is the range of the numbers, if you also have O(R) space, by using a bitset. Finally, if you are programming in a language that does not initialize memory, you can solve the problem with O(R) space and O(m+n) (no R) time by combining the bitset approach with uninitialized memory tricks.

- eugene.yarovoi August 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good explanation for all the options I can think of. +1. Why is the worst case of hash set solution is O(m+n+R) though? Isn't it O(max(m,n))? Can you explain?

- oOZz August 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 votes

The O(m + n + R) runtime is for the approach of using a bitset of size R, not a hash table. The R comes into the equation because you have to initialize the bitset.

The worst-case runtime of the hash table approach is actually O(m*n) in the case that all the entries end up in the same bucket.

- eugene.yarovoi August 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If space is not an issue we can do:
1.Hash the elements of the smaller array based on their values -> O(m) time and O(m) space
2. Search the elements of larger array in the hash and if exists keep it -> O(n)

Hence, we achieve a O(n) time with the addition of O(m) space where n>m. Apparently, it would be a O(n) time and space if m=n

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

can someone please explain in detail the solution with bitmaps

- aayushikuls August 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int findIntersection(int[] a, int[] b)
{
quickSort(a);
quickSort(b);
int N = a.length;
int M = b.length;
int i = 0;
int j = 0;
while(i<N && j<M)
{
if (a[i]<b[j]) i++;
else if (a[i]>b[j]) j++;
else if(a[i] == b[j]) break;
else return -1;
}
return a[i];
}


public static void main(String[] args)
{

int[] a = {5,1,99,2,4};
int[] b = {3,5,7,23,66,100};
System.out.println(findIntersection(a,b));
}

- neuron August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Suppose we have 2 arrays of size n & m respectively, now sort the array of size n with complexity O(n log n) , then look for every element of the second array (size m) in the first array (size n) then the complexity will be O(m log(n))
Will this be considered acceptable ?

- muhTamer September 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use binary search when looking for an element

- muhTamer September 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

If each array has only distinct elements, then:
- XOR all elements of one array.. Let's say it is x.
- For each element of the second array, find (x XOR element). If that is 0, this element is in the intersection list.

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

That's not right.

- ophis.W October 27, 2013 | Flag


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