## Amazon Interview Question for Data Engineers

• 0

Team: none
Country: india
Interview Type: Written Test

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

Union : (Assuming order of element doesn't matter )

Put all the elements of both arrays in HashSet , now Hashset will have union of both the arrays.

Intersection: (Assuming order of element doesn't matter )

Put all the elements of one array in HashSet.
Iterate over other array and check whether element is present HashSet created in previous step if yes include it in intersection result else ignore.

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

@Ankit Tomar's idea will not generally work - because arrays are not guaranteed to be unique. Of Course with unique array - where all elements are different it will work, but that is now as trivial as intersection and union of sets.

Solution to this problem is to imagine the generalised set operations.
Suppose an element valued "e" has count "count_a" in the "a" array or list, and "count_b" in the "b" array or list.

count of element "e" in the intersection of a,b will be defined as:

``min( count_a, count_b)``

count of element "e" in the union of a,b will be defined as:

``max( count_a, count_b)``

This generalised algorithm now can be used to Union/Intersection.You can generalised this to even find out set minus.

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

@NoOne : It was mentioned in Question - "elements of the two given array may be repeated but cannot be repeated in union and intersection array."
so i proposed the above solution. :)

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

Ah I see. Then your solution works, and the question is not even a question, sadly. :(

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

Good point from both of you guys!

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

Code for Union: putting all in one hashset
hash_dict={}
for value in list1:
hash_dict[value] = hash_dict.setdefault(value,0)+0
for value in list1:
hash_dict[value] = hash_dict.setdefault(value,0)+0
return(hash_dict.keys())

for intersection:
hash_dict={}
output = []
for value in list1:
hash_dict[value] = hash_dict.setdefault(value,0)+0
for value in list1:
if value in hash_dict:
output.append(value)
return(output)

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.

### 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.