## Amazon Interview Question for SDE1s

• 1
of 1 vote

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"``````

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.

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;
}

}

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;
}
}

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;
}

}

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``````

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``````

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]) {
i++;
j++;
} else if (arr1[i] < arr2[j]) {
i++;
} else {
j++;
}

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

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)")
}

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)``````

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)``````

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

``````public List<Integer> union(int[] nums1, int[] nums2) {
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) {
helper(result, nums1, nums2, nums1Index, nums2Index + 1);
return;
}

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

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

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

helper(result, nums1, nums2, nums1Index, nums2Index + 1);
}``````

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])){
}
arrLen1--;
}
else if(intArr1[intArr1.Length - arrLen1]<=intArr2[intArr2.Length - arrLen2]){
if(!dict.ContainsKey(intArr1[intArr1.Length - arrLen1])){
}
arrLen1--;
}else if(intArr1[intArr1.Length - arrLen1]>intArr2[intArr2.Length - arrLen2]){
if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
}
arrLen2--;
}
}
else if(arrLen2 > 0){
if(arrLen1 == 0){
if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
}
arrLen1--;
}
else if(intArr2[intArr2.Length - arrLen2]<=intArr1[intArr1.Length - arrLen1]){
if(!dict.ContainsKey(intArr2[intArr2.Length - arrLen2])){
}
arrLen1--;
}else if(intArr2[intArr2.Length - arrLen2]>intArr1[intArr1.Length - arrLen1]){
if(!dict.ContainsKey(intArr1[intArr1.Length - arrLen1])){
}
arrLen2--;
}
}

}
return dict.Select(v=>v.Value).ToArray();``````

}

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.

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]){
i++;
}else if(a[i]>b[j]){
j++;
}else{
i++;
j++;
}
}
if(i<a.length){
for (int k = i; k < a.length; k++) {
}
}else if(j<b.length){
for (int k = j; k < b.length; k++) {
}
}
return (Integer[])list.toArray(new Integer[list.size()]);
}``````

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));``````

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

``````public class AmazonQ1 {

private static Set<Integer> uniqueSet = new HashSet<>();

/**
* Complexity O(n)
* @param a
* @param b
* @return
*/
public static void union(int[] a, int[] b){
int i = 0;
int j= 0;
int k = 0;
while(i < a.length && j < b.length){
if(a[i] == b[i]){
j++;
}else {
}
}

while(i < a.length){
}

while(j < b.length){
}
}

public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};

union(a, b);
uniqueSet.forEach(System.out::println);
}
}``````

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

``````import java.util.Arrays;
import java.util.TreeSet;

public class Test {
public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};
var union = new TreeSet();
System.out.print("Union=" + union);
}
}``````

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

``````import java.util.Arrays;
import java.util.TreeSet;

public class Test {
public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};
var union = new TreeSet();
System.out.print("Union=" + union);
}
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ import java.util.Arrays; import java.util.TreeSet; public class Test { public static void main(String[] args){ int a[] = {2, 10, 14, 19, 51, 71}; int b[] = {2, 9, 19, 40, 51}; var union = new TreeSet(); Arrays.stream(a).forEach(union::add); Arrays.stream(b).forEach(union::add); System.out.print("Union=" + union); } }
Comment hidden because of low score. Click to expand.
0
of 0 vote

``````import java.util.Arrays;
import java.util.TreeSet;

public class Test {
public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};
var union = new TreeSet();
System.out.print("Union=" + union);
}
}``````

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

``````import java.util.Arrays;
import java.util.TreeSet;

public class Test {
public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};
var union = new TreeSet();
System.out.print("Union=" + union);
}
}``````

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

import java.util.Arrays;
import java.util.TreeSet;

public class Test {
public static void main(String[] args){
int a[] = {2, 10, 14, 19, 51, 71};
int b[] = {2, 9, 19, 40, 51};
var union = new TreeSet();
System.out.print("Union=" + union);
}
}

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

``````package test1;

import java.util.Stack;

public class Test1 {

public static Stack<Integer> s = new Stack<>();

public static void main(String[] args) {
int a[] = { 2, 10, 14, 19, 51, 71 };
int b[] = { 2, 9, 19, 40, 51 };

merge(a, b);
System.out.println(s);

}

private static void merge(int a[], int b[]) {

int t1, t2 = 0;

for (int i = 0; i < a.length; i++) {
t1 = a[i];

for (int j = 0; j < b.length; j++) {
t2 = b[j];

if (s.isEmpty()) {
if (t1 < t2)
s.push(t1);
else if (t1 > t2)
s.push(t2);
else
s.push(t1);
} else {
if (t1 == t2) {
if (s.peek() < t1) {
s.push(t1);
}
} else {
if (t1 < t2) {
if (s.peek() < t1) {
s.push(t1);
break;
}
} else {
if (s.peek() < t2) {
s.push(t2);
}
}
}
}
}
}
}
}``````

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.