Amazon.com Interview Question
1,569 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer at Amazon.com:
Given two sets of numbers find the elements in A that appear in B (set intersection)
See more for this Amazon.com Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
LinkList list4 = new LinkList();
Node a = list1.head;
Node b = list2.head;
while (a != null && b != null) {
if (a.value < b.value) {
a = a.next;
} else if (a.value > b.value) {
b = b.next;
} else if (a.value == b.value){
list4.insert(a.value);
a = a.next;
b = b.next;
}
}
list4.printList();
}
}
Assuming we are using lists and the lists are already sorted, we can iterate through the lists and compare values and whenever there is a match, we can store the values in a new list.
public void setIntersection(LinkList list1, LinkList list2) {
LinkList list4 = new LinkList();
Node a = list1.head;
Node b = list2.head;
while (a != null && b != null) {
if (a.value < b.value) {
a = a.next;
} else if (a.value > b.value) {
b = b.next;
} else if (a.value == b.value){
list4.insert(a.value);
a = a.next;
b = b.next;
}
}
list4.printList();
}
}
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: