Microsoft Interview Question for Software Engineer in Tests






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

Similar to the other question they asked you.

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

Push all elements of array #2 onto an hash.
Use the index as value.

Search array #1 and get the indexes.

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

I gave this exact ans but it the interviewer was looking for more optimal solution. :(

- manish August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

More optimal? In what way?

- Anonymous August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm having trouble understanding this solution. What is he using as the hash function?

- Anonymous August 09, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

is the first array always consisting of sorted natural numbers in a range...

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

O(n) space & O(nlogn) solution :

Sort the 2nd array. Also maintain the original indexes of numbers before sorting. Now, traverse the first array, search each element in the 2nd array in logn(as it is sorted).

- oshin August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

A similar approach

Create a BST out of those elements in the second array where each node has got the value and index number. O(n log n)
For every element in the first array, do a search on the BST created in step 1 and return the index. O(n log n)
Time Complexity:O(n log n) and space complexity O(n).

PS: If we are given doubly linked lists instead of arrays, then we can reduce the space complexity to O(1)

- Triton August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how is this more optimal than O(n) time and O(n) space solution using hashmap?

No way you get better than O(n) time. I guess he was looking for loop optimizations, fine tuning as such.

- David August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I wonder the same thing. The hash table idea seems like a solid solution to me. That would have been my solution.

- Anonymous August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hash table uses a lot of additional space compared to a BST.

- Triton August 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Assuming the arrays to be like what u have put as example . To avoid extra space(without hashtable) and arrays are faster than Hash table.replace the 2nd array with
for(i<arr2.lenght)
arr2[arr2[i]]= i //O(m) say

and then scan through the arr1 and then get it by:
arr2[arr1[i]]

Please comment

- satish August 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice!

- DRY August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Did either of you actually try this?

(Though the idea of using an array on keys 1..n instead of a hash is brilliant.)

- Anonymous August 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you don't mind trashing the array.

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

Lets say that Array2 is {3,2,4,5,1000}.
Will this logic work still?? What is the disadvantage with this solution??

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

arr2[arr2[i]]= i
let:
arr2[i] = X
doesn't this destroy the value of arr2[X] which is needed later?
If I step through the above example a collision occurs.

That said if arr1 is always guaranteed to be from 1..n, then we can use that as the "hash" map.

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

Thanks guys.

I am assuming we don't need arr2 later.The problem is to to return the index positions. The program sud run fine.
Instead we may use some type casting like this but we dont require it:
for(i<arr2.lenght)
arr2[(int)arr2[i]]= i //O(m) say

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

You can't write into array #2 as you are looping through it, because you will inevitably destroy information that you need later in the loop. What you can do is write into array #1 because you don't need it (as long as the input arrays are the same length and the first array is the first n natural numbers).

for (i = 1; i++; i <= length(arr2) {
    arr1[arr2[i]] = i;
}
output(arr1);

- wookie August 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ LSK , i said assuming the array as mentioned in the example. I guess other than that there is nothing special about the question and there is always the hastable soln. May be the question's example is knowingly like this. The approach is specific to the problem..Thanks

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

int arr1[]= {1,2,3,4,5};
int arr2[]= {3,2,4,5,1};
int arr3[]= new int[6];

for(int i=0;i<arr2.length;i++)
arr3[arr2[i]]=i;


for(int i=0;i<arr1.length;i++)
{

System.out.println(arr3[arr1[i]]);

}

- satish August 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

seems hash table is a better solution for array like
arr1 = {7,8,9,10,11}
arr2 = {11,8,7,9,10}
this cannot be done by using the array method ..

- AA September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about this. This is O(N) but uses O(1).

int j = 0;
for(int i = 0 ; i < A.Length() , j < A.Lenght(); i++)
{
   if(A[j] == B[i])
   {
      printf("%d ", i+1); ///position in B. A & B are arrays.
      j++; i = 0; //reset i to continue loop.
   }

}

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

you fucker !! don't you understand that your solution is O(n^2) .. people with such an IQ don't deserve to comment on such portals

- gaurav singh November 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

why so serious? It's just his opinion. If you don't like it, ignore it.. no need to be derogatory towards the sol.

- Anonymous May 19, 2011 | 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