Amazon Interview Question for Software Engineer in Tests






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

We can solve this using a HashTable.
Traverse First Array, keeping each of the array elements as a key, increment the value at the key. Once done with traversing the first array.
Now for the second array again in the same HashTable lookup with the key as element from second array. If the value is != 0, copy the element in to output array. decrement the value count.
if value is 0 then move to next element in the array list. Once done with traversing the second array as well return the output array.

I screwed up with this in the intv, I took two different hash tables for both the arrays with definite pre-defined set of Keys (All Possible character's). The after comparision of two Hashtables returned the output array.

This was asked in tele phonic intv.

- Anonymous May 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

solution 1:
Anonymous is correct. scan Array_1, record in hash table. scan Array_2, look up in Hash table.
time: O(n)

solution 2:
use BST, build Array_1 to a BST, scan Array_2, look up in BST
Time: O(nlgn)

Solution 3:
if memory is not large enough, use bitset

- bbs59 June 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bitset might not be a good solution, since it can only store true or false. What we also want to store is the repeated times of that value.

- Anonymous July 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution don't work properly since it won't print the output as "2 x" solution to that problem can be solved if u use 2 hash tables. do exactly as the first solution but while reading the second array when u find the element reduce the occurrence by 1 and till it becomes zero and on the second hash table increase the value by one. after the scan is done, print the second hash table.

- roy August 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is no need to maintain a counter variable for it, like if you put all the elements of the first array in the hashset and then, keep checking the elements from the hashset which matches and print them while inserting the elements of the first array into the hashset don't check whether the element is present in the array because it will not print the duplicate element.

- swapnilkant11 July 24, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

That's a good solution. Thanks for sharing.

- Anonymous May 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the number of elements in the array is very large or the memory is not big enough, the HashTable solution can't work. Then, how to solve this problem??

- Andy May 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort first array by any good sorting technique say quick sort then pick first element and do binary search for in array1 which is sorted this search will be ologn for each element.

- cunomard May 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how do u get d count then

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

template <class T>
vector<T> ArrayIntersection(const vector<T> vecA, const vector<T> vecB)
{
vector<T>::const_iterator ite= vecA.begin ();
map<T, int> counter;

while (ite!= vecA.end())
{
++counter[*ite];
++ite;
}

vector<T> result;

ite= vecB.begin();
while (ite!=vecB.end ())
{
if (counter[*ite])
{
result.push_back(*ite);
if (counter[*ite]==1){
counter.erase (*ite);
}
else counter[*ite]--;
}
++ite;
}
return result;
}

- vFeng May 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Sort both arrays (O(nlogn))
2. Take two counters to traverse both arrays one counter pointing to each array in the beginning O(1)
3. move the counter to next elt which is pointing to smaller number eg. first array has current number 5 while second array has 8 then move the first counte to next elt.
4. If both are same print the elts and move both counters.

As soon as you are done with any array. steps 3 and 4 th O(n)

Total complexity = o(nlogn) + o(n) = o(nlogn)

- Anant Kumar August 20, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ANANT
first you need to find out sorting algos on bigger chunk of data...for bigger ur solution seems inappropriate.....suggest another thought of ur mind

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

Can Anyone suggests why Ananat algo is wrong?I think it should work fine.

- Anonymous October 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Anant's solution fails miserably for a larger list. have you thought of it?

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

we can do this also by XORing the elements of one array to find the repeated ones in both arrays indivisually in O(n).then if any element is in common move that to output array.now XOR elements of one array with other taking one element from both turn by turn.if result comes 0,chaek if the element is in common,if not move to output array else leave it.
complexity is O(n)+O(n)=O(n)

- parkhi September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry the soln above is not correct as XORing 1,2,3,1 is 2,3 not 0

- parkhi September 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

How do you guys plan to sort an array of characters?

- Yash July 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Using ASCII values.

- Kubix July 27, 2009 | 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