## Amazon Interview Question

Software Engineer / Developers**Country:**United States

**Interview Type:**Phone Interview

I agree, this probably the best/only O(1) space solution. This involves a very large (but constant) array of bits, where the bit value represents whether the corresponding number is present in array A. Then iterate through array B checking the corresponding positions in the bit array.

So assuming that we're using 32 bit integers, then the bit array has 2^32 bits = roughly 512 mb. This approach would achieve O(n) execution time and O(1) space. But this solution is only really valid when there is a very large number of elements in array A and B.

Doesn't this require creating extra space? E.g. creating an array of 512 mb? Please correct me if I am thinking of constant space O(1) wrong.

Yes, it requires an bit array of size 2^32 = 512mb (assuming we're talking about 32 bit integers) . But this is a constant sized array regardless of the size of the input arrays A and B.

So this solution is O(1). a.k.a constant in space complexity.

As mentioned, this solution is only suitable for really large arrays A and B.

@charsyam :: Can you please explain how can we find the number by traversing the sorted array.

Yeah, that should work. Just do a quicksort then do something similar to merge step in mergesort.

Yes. QuickSort is O(nlogn), I'm not sure but, How can we ensure O(n) I just think LookupTable using BitVector size [INT_MAX/8], it is O(1) space :)

What do you mean "find values that appear in both arrays and output them"? what exactly "values" mean here. In your example,

int[] a = {1,2,3,4};

int[] b = {2,5,6,7,3,2};

What is the expected output? {2, 3} or {2, 2, 3}?

I think this answer satisfies the requirement for O(n) computation complexity and O(1) space complexity

I think you can use bit manipulation if the numbers in array is "close" to each other.

Build an bitmap for each array and set the bit to 1 if the number is exist.

Then use &. The bit remain 1 is the overlap bit.

say I have {0.1,2,3} and {2.5.6,7}

We Have bitmap 11110000 and 00100111

After & we get 00100000 which means 2 is overlapped.

This method is not suit for sparse array like{0.100000}

if sizeof(a) +sizeof(b) <maxvalue-minvalue of both arrays then its simple O(n)

else

use universal hash(linear probing) on to the array itself instead of

creating a temporary array to store the hash.( complexity may vary but avg would be O(n)).

It works even if the numbers are negative in the array.

forgot to mention.

in the second case restoring the original array

is possible if only if (maxv-minv)^2 <maxv value possible of data type

doesn't seem like a working algorithm with O(n) complexity and O(1) space.

In "sizeof(a) +sizeof(b) <maxvalue-minvalue", how do you make {-1000, 0, 1000}, {500, 1000} work?

It still doesn't seem working with O(n) complexity and O(1) space when you try to reuse and overwrite input arrays.

How do you make it work for {5,6,7}, {3,4,5} in O(n) complexity and O(1) space?

Remember, when you use hashing and linear probing, you can't lose information by overwriting elements without keeping that element's info somewhere and remember the space requirement is O(1).

I think this can be solved by using a bit vector of size 2 power (sizeof(int)-3)

O(2 power (sizeof(int)-3)) = O(constant) = O(1) space

This is what i can think of

sort both arrays

keep two pointers one at start of both the arrays

if(a[i]==b[j])

return

else if(a[i]<b[j])

i++

else

j++

The problem with this method is at sort step since i dont know if there is any sorting method which would sort in O(nlogn) time without using extra space. Quick sort takes O(n^2)

Really? I find that hard to believe unless you are a super typer. I mean just explaining everything in detail over the phone for one problem can take a bit of time. We were also trying to use a collaborative text editing site that was being finicky and slow.

I recently had an interview with Amazon, and was asked one simple question while we were trying to get the code site link working (about 2-3 mins to explain, very simple). Then one 'real' question close to the one asked in this discussion. I'm not sure how you would get more than two technical questions in an hour interview.

I was already 20-30 minutes into the interview after I was done talking about my myself and what I have worked on in the past. Then after the technical questions we talked for another 15+ mins about the company, city, etc, etc etc.

So 1 hour, -15, -2 , -30, +12 (went over the hour a little bit), so I had around 25mins to come up with a solution, code the solution, talk about other directions that could be taken to solve the problem, and coding a 2nd solution to the problem.

If I was perfect (or knew the answer before hand) I could see getting more than one question, but it sounded like the phone interview was meant to have one question asked.

I'll found out in a couple days if I made it to the next step, good luck to you too.

Okay, that is what I figured. My interview went almost exactly the same way. I talked with the interviewer about my favorite project I have worked on for a good 10 minutes, then we tried getting the coding site to work, and then worked on code for about 20-25 minutes, and then talked for another 15 or so.

Just making sure my experience was not wildly different from everyone else. Thanks and best of luck to you!

Two arrays A and B

1) First do a quick or merge sort on Array A

2) Now Iterate each element in Array B and do a binary search in Array A. If found, then add this element as found.Continue this to all the elements in Array B.

3) Finally u ll have the elements that are found in Array A and B.

If n is no of elements in A and m in B

Complexity - Sort of A( O(nlogn)) + Iterate B and do search in A(O(mlogn) = m+n(logn)

Please note what my question was asking. I want to do it with complexity O(n) and memory complexity of O(1). Yours has a higher computational complexity. While it may not use memory, it will take longer to run on large datasets.

Hi zimmerman....is this your first round.do u know how long it takes to notify for second round or a person is rejected by amazon...

When you get your first round scheduled they tell you in the email about the time frame of the entire process. You should here back with a yes or no within 2 business days after a phone interview. And hear back within 5 business days after an onsite interview.

It took me about a week to hear back from the first phone interview to schedule a second. It may vary from person to person and with how busy the recruiting people are.

The best solution that is O(1) is the array of max int bits. If int is 32 bit that's 512MiB.

- sonny.c.patterson November 11, 2012