Yahoo Interview Question for Software Engineer / Developers






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

@S
The question itself says that you need to create a new linked list.

Solution:
Keep two pointers at the start of the both linked list.
1. Compare two values. If they are equal copy this value to new linked list.
2. if values are not equal then move the pointer of the linked list having the lower value to its next value .
go to step 1. untill any linked list ends.

- Manish September 18, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

yes.. i think it will work. it is just as the merging 2 sorted list.

- Mridul Malpani October 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

what happens if the linked list is non unique? your solution will copy the same value more than once..and another doubt that i have is that this soln will take o(n^2) time right?...cant we optimize it?

- swarup.avra March 09, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public linkedlist intersection(linkedlist head1,linkedlist head2){

Node current1 = head1;
Node current2 = head2;
linkedlist returnlist = new linkedlist();
while(current1 !=null && current2 !=null){
if(current1.data==current2.data)
returnlist.add(new node(current1.data))
else if(current1.data>current2.data)
current2 = current2.next
else current1 = current1.next
}

return returnlist ;

}

- Anonymous September 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's the clean solution. BUT..you forgot to add these 2 lines:
current1 = current1.next
current2 = current2.next

inside the if : if(current1.data==current2.data)
otherwise, you'll keep adding this node forever in the result linked list.

- moh.ezz8 March 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is sample programme to get intersection os two sorted list

private Node getIntersectedLinkedList(Node first, Node second){
		Node ptr = first;
		Node ptr1 = second;
		Node previous = null;
		Node firstIntersectedNode = null;
			while (ptr != null) {
				ptr1=second;
				while (ptr1!= null && ptr.getValue() != ptr1.getValue() ) {
					ptr1 = ptr1.getNext();
				}
				if(ptr1 !=null){
					Node node = new Node();
					if (firstIntersectedNode == null) {
						firstIntersectedNode = node;
					}
					node.setValue(ptr.getValue());
					if (previous != null) {
						previous.setNext(node);
					}
					previous = node;
				}
				ptr = ptr.getNext();
			}
		
		return firstIntersectedNode;

}

- Mritunjay Kumar September 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't think we need other list. :)

- S September 15, 2010 | 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