Amazon Interview Question for Software Engineer / Developers






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

It should be a HashMap, with key=numbers and value=count;
One HashMap for each stream. There is a another HashSet for the final calculation.

Consider these two stream are two FIFO queue. The actions can be enqueue and dequeue, each action will affect its corresponding hashmap and the final hashset.

- Anonymous October 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort both the arrays using quick sort.
It takes O(nlogn) operations.
Take two indexes i and j for the arrays
while(i < size1 && j < size2)
{
if(a[i]==b[j])
{ printf("%d\n",a[i]);i++;j++}
if(a[i]<b[j]) i++;
if(a[i]>b[j]) j++;
continue;
}

- Aditya December 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

List != Array, so quicksort does not work

- Rag November 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

we are talking about a stream of integers, so i would think either list could grow over time, so taking a LL or array approach is wrong in that case.
Here is a O(n) algorithm, assuming we can use extra space:
build a hash as we traverse elements from both lists A and B.
if an element is not in the list, just put it in the hash with value A if we are traversing A or B otherwise.
if the element is in the list already, if it maps to A and element is in A, do nothing. if the element is in the other list, right it down as part of the intersection, and change the value it maps to to "Both".
Thats only O(n).

- Dominator December 09, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There doesn't appear to be a justification for sorting. After all, the problem asks to find the intersection; it doesn't mention anything about priorities or sequences.

HashSet appears to be a viable solution; minimal space. If the lists are large, a HashTable may allocate unnecessary memory upfront, causing a memory leak.

- Jack December 18, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

construct the BST with larger array and search the elements in the smaller array in BST, if found print or store those numbers which are nothing but insersection.

arrays a[m],b[n] with m>n
constructing BST for 'a' would take O(log m) and searching the elements in 'b' would take O(n log m)... sounds reasonable.

Any comments??

- Rossi January 24, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Everything is wrong!

> construct the BST with larger array 
> and search the elements in the smaller 
> array in BST, if found print or store 
> those numbers which are nothing but insersection.

Construct BST with the smaller array would be more efficiently from the point of memory view

> arrays a[m],b[n] with m>n
> constructing BST for 'a' would take O(log m)

Construct BST from array will take O(m).
Imagine you have array 1,2,3,4,5,....,N-1 => BST with root at 1 will look like one branch tree. So, to insert Nth element you should iterate from 1 to N-1.

To find an element in BST take O(n) in worth case. Binary Search Tree and Binary search is not the same.

I would suggest use hash, or sort smaller and then binary search.

- zdmytriv January 28, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Create 2 Bit Maps one each for the List. For example, for the list 24 45 56 12 34, will set the bits 24, 45 56 12 and 34. Do the same for the second list and set the corresponding bits in the second bit map. ANDing of the bit maps should give you the intersection. I think this should work without any sorting.

- Indian May 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hello Indian,

How big do you think the bitmaps can be? Are we not wasting memory here?

- noviceprog June 17, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi, Indian
I didn't get your solution. So you change 24 45 ... to 11000 101010 ... and get 2 maps, then do AND?

- goose May 08, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No goose, it should be like set the 24th bit to 1 for the first list and similarly for the second list.

Once you AND both the bitmaps, only the points of intersection would be 1.

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

hey...
i think it all depends on how the lists are coming....
make a data structure in which we can remember from which list the element came
struct node{
int key;
int came_frm;//set as 1 if from A set as 2 if from B and set as 3
}
now make a hash map using this datastruct
node map[MAX];
for each new element if does not exist in the map just check
eg:

insert_fromA(int val)
{
if map[val]->came_frm==0 map[val]->came_frm=1;
else if(map[val]->came_frm==2) map[val]->came_frm=3;
else if(map[val]->came_frm==1) //do nothing;
}


insert_fromB(int val)
{
if map[val]->came_frm==0 map[val]->came_frm=2;
else if(map[val]->came_frm==1) map[val]->came_frm=3;
else if(map[val]->came_frm==2) //do nothing;
}

Print_Intersection(map[])
{
for(map.start to map.end)
if(map[val]->came_frm==3)// put in new set or print or watever
}

i donno how we could implement this map... :( even a RBtree can be made i suppose...with the same datastructure...

and if we have information associated with each record,....we may put some string * or sumthin thr

- utscool March 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use TRIEs instead, and treat integers as strings.... this should do better with nlogk where k=<max_len_of_int_str>

- XYZ September 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use TRIEs instead, and treat integers as strings.... this should do better with nlogk where k=<max_len_of_int_str>

- XYZ September 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use TRIEs instead, and treat integers as strings.... this should do better with nlogk where k=max_len_of_int_str

- XYZ September 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use TRIEs instead, and treat integers as strings.... this should do better with nlogk where k=max_len_of_int_str

- Anonymous September 06, 2011 | 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