Amazon Interview Question for SDE1s


Country: United States
Interview Type: Phone Interview




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

# assumes sorted and unique
def get_union(a,b):
	if not b:
		return a
	if not a:
		return b
	p = q = 0
	union = []
	while p < len(a) and q < len(b):
		if a[p] < b[q]:
			union.append(a[p])
			p += 1
		elif a[p] > b[q]:
			union.append(b[q])
			q += 1
		else:
			union.append(a[p])
			p += 1
			q += 1
	if p < len(a):
		union.extend(a[p:])
	if q < len(b):
		union.extend(b[q:])
	return union

a = [2, 10, 14, 19, 51, 71]
b = [2, 9, 19, 40, 51]
u = get_union(a,b)
print u
u_from_stdlib = set(a) | set(b)
assert len(u) == len(set(u))
assert len(set(u) ^ u_from_stdlib) == 0, "symmetric difference exists"

- marcopolo May 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would rather put them both in a set. Set would filter out distinct elements from each of them. Question doesn't say output should be sorted. So I would get the union that is distinct element from both the lists into the set.

- Archana Kumari May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51} ;
int union[]= getUnion(a,b);
for(int i=0;i<union.length-1;i++){ System.out.println(union[i]);
}
}
private static int[] getUnion(int[] a, int[] b) {
int u[] = new int[a.length+b.length];
int i=0,j=0,k=0;
boolean flag =true;
while(flag){
if(a[i] == b[j]){
u[k] =b[j];
k+=1;
j+=1;
i+=1;
}
else if(a[i]<b[j]){
u[k]=a[i];
k+=1;
i+=1;
}
else{
u[k]=b[j];
k+=1;
j+=1;
}
if(j==b.length){
flag=false;
}
}
return u;
}

}

- Harshad Deo May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51} ;
int union[]= getUnion(a,b);
for(int i=0;i<union.length-1;i++){ System.out.println(union[i]);
}
}
private static int[] getUnion(int[] a, int[] b) {
int u[] = new int[a.length+b.length];
int i=0,j=0,k=0;
boolean flag =true;
while(flag){
if(a[i] == b[j]){
u[k] =b[j];
k+=1;
j+=1;
i+=1;
}
else if(a[i]<b[j]){
u[k]=a[i];
k+=1;
i+=1;
}
else{
u[k]=b[j];
k+=1;
j+=1;
}
if(j==b.length){
flag=false;
}
}
return u;
}
}

- Harshad Deo May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{

public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51} ;
int union[]= getUnion(a,b);
for(int i=0;i<union.length-1;i++){
System.out.println(union[i]);
}
}
private static int[] getUnion(int[] a, int[] b) {
int u[] = new int[a.length+b.length];
int i=0,j=0,k=0;
boolean flag =true;
while(flag){
if(a[i] == b[j]){
u[k] =b[j];
k+=1;
j+=1;
i+=1;
}
else if(a[i]<b[j]){
u[k]=a[i];
k+=1;
i+=1;
}
else{
u[k]=b[j];
k+=1;
j+=1;
}
if(j==b.length){
flag=false;
}
}
return u;
}

}

- Harshad Deo May 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def get_union(a,b):
  a_len = len(a)
  b_len = len(b)
  print(a_len, b_len)

  result = []
  hashmap = {}
  for i in range(len(a)-1):
    if (i > a_len or i>b_len):
        break

    # print(mn, mx)
    if hashmap.get(a[i]) == None:
      result.append(a[i])
      hashmap[a[i]] = True

    if hashmap.get(b[i]) == None:
      result.append(b[i])
      hashmap[b[i]] = True

  return result

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

def get_union(a,b):
  a_len = len(a)
  b_len = len(b)
  print(a_len, b_len)

  result = []
  hashmap = {}
  for i in range(len(a)-1):
    if (i > a_len or i>b_len):
        break

    # print(mn, mx)
    if hashmap.get(a[i]) == None:
      result.append(a[i])
      hashmap[a[i]] = True

    if hashmap.get(b[i]) == None:
      result.append(b[i])
      hashmap[b[i]] = True

  return result

- sk May 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void union(int arr1[], int[] arr2) {
		int i = 0, j = 0, k = 0;
		List<Integer> list = new ArrayList<>();
		boolean flag = true;
		while (flag) {
			if (arr1[i] == arr2[j]) {
				list.add(arr1[i]);
				i++;
				j++;
			} else if (arr1[i] < arr2[j]) {
				list.add(arr1[i]);
				i++;
			} else {
				list.add(arr2[j]);
				j++;
			}

			if (j == arr2.length) {
				flag = false;
			}
		}
		if (arr1.length > arr2.length) {
			int min = arr2.length;
			for (int x = min; x < arr1.length; x++) {
				list.add(arr1[x]);
			}
		} else {
			int min = arr1.length;
			for (int x = min; x < arr2.length; x++) {
				list.add(arr2[x]);
			}
		}
		System.out.println();
		for (Integer x : list) {
			System.out.print(x + " ");
		}
	}

- Somendra Raj May 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Swift

let setA:Set = [2, 10, 14, 19, 51, 71]
let setB:Set = [2, 9, 19, 40, 51]

let unionOfAandB = setA.union(setB)

for item in unionOfAandB
{
print("items is \(item)")
}

- Rahul Jain May 20, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use 'sorted' built-in function if you want the union to be sorted

a = {2, 10, 14, 19, 51, 71}
b = {2, 9, 19, 40, 51}
c = sorted(a.union(b))
print (c)

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

Use python's built-in function 'sorted' if you want the union to be sorted

a = {2, 10, 14, 19, 51, 71}
b = {2, 9, 19, 40, 51}
c = sorted(a.union(b)

- Goutham May 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public List<Integer> union(int[] nums1, int[] nums2) {
	List<Integer> result = new LinkedList<Integer>();
	helper(result, nums1, nums2, 0, 0);
	return result;
}

void helper(List<Integer> result, int[] nums1, int[] nums2, int nums1Index, int nums2Index) {
	if(nums1Index >= nums1.length && nums2Index >= nums2.length) return;
	if(nums1Index >= nums1.length) {
		result.add(nums2[nums2Index]);
		helper(result, nums1, nums2, nums1Index, nums2Index + 1);
		return;
	}

	if(nums2Index >= nums2.length) {
		result.add(nums1[nums1Index]);
		helper(result, nums1, nums2, nums1Index + 1, nums2Index);
		return;
	}

	if(nums1[nums1Index] == nums2[nums2Index]) {
		result.add(nums1[nums1Index]);
		helper(result, nums1, nums2, nums1Index + 1, nums2Index + 1);
		return;
	}

	if(nums1[nums1Index] < nums2[nums2Index]) {
		result.add(nums1[nums1Index]);
		helper(result, nums1, nums2, nums1Index + 1, nums2Index);
		return;
	}

	result.add(nums2[nums2Index]);
	helper(result, nums1, nums2, nums1Index, nums2Index + 1);
}

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

public int[] GetUnion(int[] intArr1, int[] intArr2){
		int arrLen1 = intArr1.Length;
		int arrLen2 = intArr2.Length;
		
		Dictionary<int, int> dict = new Dictionary<int,int>();
		
		while(arrLen1 > 0
				|| arrLen2 >0){
				
				if(arrLen1 > 0){
					 if(arrLen2 == 0){
						 if(!dict.ContainsKey(intArr1[intArr1.Length - arrLen1])){
							 dict.Add(intArr1[intArr1.Length - arrLen1],intArr1[intArr1.Length - arrLen1]);
						 }
						 arrLen1--;
					 }
					 else if(intArr1[intArr1.Length - arrLen1]<=intArr2[intArr2.Length - arrLen2]){
						 if(!dict.ContainsKey(intArr1[intArr1.Length - arrLen1])){
							 dict.Add(intArr1[intArr1.Length - arrLen1],intArr1[intArr1.Length - arrLen1]);
						 }
						 arrLen1--;
					 }else if(intArr1[intArr1.Length - arrLen1]>intArr2[intArr2.Length - arrLen2]){
						 if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
							 dict.Add(intArr2[intArr2.Length - arrLen2],intArr2[intArr2.Length - arrLen2]);
						 }
						 arrLen2--;
					 }
				}
				else if(arrLen2 > 0){
					 if(arrLen1 == 0){
						 if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
							 dict.Add(intArr1[intArr2.Length - arrLen2],intArr1[intArr2.Length - arrLen2]);
						 }
						 arrLen1--;
					 }
					 else if(intArr2[intArr2.Length - arrLen2]<=intArr1[intArr1.Length - arrLen1]){
						 if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
							 dict.Add(intArr2[intArr2.Length - arrLen2],intArr2[intArr2.Length - arrLen2]);
						 }
						 arrLen1--;
					 }else if(intArr2[intArr2.Length - arrLen2]>intArr1[intArr1.Length - arrLen1]){
						 if(!dict.ContainsKey(intArr1[intArr1.Length - arrLen1])){
							 dict.Add(intArr1[intArr1.Length - arrLen1],intArr1[intArr1.Length - arrLen1]);
						 }
						 arrLen2--;
					 }
				}
				
		}
		return dict.Select(v=>v.Value).ToArray();

}

- alir2t2 June 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this need to be resolved using a min heap.

build the first array into a min heap and then add the second array one by one into the min heap.

- sj June 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static Integer[] generateUnion(int[] a, int[] b){
        int i = 0, j = 0;
        List<Integer> list = new ArrayList<Integer>();
        while(i<a.length && j<b.length){
            if(a[i]<b[j]){
                list.add(a[i]);
                i++;
            }else if(a[i]>b[j]){
                list.add(b[j]);
                j++;
            }else{
                list.add(b[j]);
                i++;
                j++;
            }
        }
        if(i<a.length){
            for (int k = i; k < a.length; k++) {
                list.add(a[k]);
            }
        }else if(j<b.length){
            for (int k = j; k < b.length; k++) {
                list.add(b[k]);
            }
        }
        return (Integer[])list.toArray(new Integer[list.size()]);
    }

- huehuehuehuehue June 25, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you can't use Javascript union function and need to preserve the sorting:

let union = function(arr1, arr2) {
    let out = {};
    let len = Math.max(arr1.length, arr2.length);
    for(let i=0; i<len; i++) {
        if(arr1[i]) {
            out[arr1[i]] = true;
        }
        
        if(arr2[i]) {
            out[arr2[i]] = true;
        }
    }
    
    return Object.keys(out);
};

var a = [2, 10, 14, 19, 51, 71];
var b = [2, 9, 19, 40, 51];

console.log(union(a,b));
console.log(union(b,a));

- Tim July 31, 2018 | 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