Amazon Interview Question for Software Engineer / Developers


Team: Kindle
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
14
of 18 vote

By assuming S1 and S2 are two Dynamic Link List. Append Tail S1 with head of S2. Which is of O(1).

Node * Union(Node *S1, Node *S2) {
    TS1 =  S1.Tail();
    TS1->Next = S2;
    S1.tail = S2.tail;
    return S1;
}

- SRB December 10, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Here s1 and s2 are sets . union also should be a set .. means all the elements in set should be unique.

- bharat December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

two disjoint sets means separate as mentioned in the question. The ans is perfectly acceptable compared to urs.
It does in O(1) unlike urs.

- Bevan December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the above solution is correct as Disjoint sets means S1 intersection S2 is empty set

- MVVSK December 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suspect that tail works in O(N) where N is the size of the list. So in turn the above code should be O(N) not O(1) .. what you say?

- Subrahmanyam December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@subrahmanyam Agree with your point. Since it is a dynamic link list, we can assume that we always have head and tail pointer of the list

- rahul December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This works only with assumption that you have pointers handy.

- thesimplecoder December 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

ok if suppose the two sets were not disjoint ; then this solution won't work! right ?

- ankit January 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

bt will it work when some elements are common in both set.if we combine like that,then there should be duplicate item in the resultant set.

- singh.pramod February 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about having circular linked list, you dont need to find tail to join them. If two lists are disjoint, Circular Linked list can be joined in O(1) time.

- hareeshsarma December 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public MyCircularDoubleLinkedList union(MyCircularDoubleLinkedList list1,MyCircularDoubleLinkedList list2){
		
		
		MyCircularDoubleLinkedListNode temp1,temp2,temp3;
		
		
		temp1=list1.headNode.prev;
		
		//System.out.println("\n temp1 value"+temp1.getValue());
		
		temp2=list2.headNode.prev;
		//System.out.println("\n temp2 value"+temp2.getValue());
		
		//System.out.println("\n head2s next  value"+list2.headNode.getNext().getValue());
		
		temp3=list2.headNode.next;
		
		//System.out.println("\n temp3 value"+temp3.getValue());
		
		
		temp1.next=temp3;
		temp3.prev=temp1;
		
		temp2.next=list1.headNode;
		list1.headNode.prev=temp2;
		
		return list1;
		
	
		
		
	}

- Subrahmanyam December 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

maintain 2 hashmaps on each set. when ever a element comes from set s1, check whether the element already exists in s2, if not keep in union set.

- bharat December 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

O(1) is required.

- Bevan December 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. Its not O(1) .. its O(N).

- Subrahmanyam December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It looks like we can use doubly linked circular linked list to represent the problem.

Then we can perform this operation in O(1) whatever be the size of the two given sets.

Ofcourse I am not taking space constraints into consideration.

- Subrahmanyam December 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

comment

- catch.rajatahuja December 10, 2012 | Flag
Comment hidden because of low score. Click to expand.


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