Microsoft Interview Question for Software Engineer in Tests






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

I assume that the two LL intersect by address.
If that is true then from a single address only one next pointer can come out.
So if you check whether the last element address is the same then its a sufficient check.
It will be shaped like a Y where if intersection happens in between and like a V where last element intersect, so in either case you have the last element address common.

- Gamodg October 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

but what if they intersect like X??

- hi December 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

X like intersection is not possible.
becuase in linear linked list only each node has only ONE "next" pointer.
think about it, to create X like intersection, you will need TWO "next" pointers on the crossing node.

- djebel January 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use counting sort which runs O(n)
pick one element from the linklist and in the array tally each element as soon as you find a number twice you can stop
one can also use an hashing function i.e. you remember what you have encountered
in the worst case you will take O(m+n) and space of O(m+n)

- digvijaysinghshaktawat October 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

as per my understanding, the node of linklist has 1 pointer which can point to only one element. in that case how can there be a node which is present on 2 linklists?

- b November 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Method 1(Simply use two loops)
Use 2 nested for loops. Outer loop will be for each node of the 1st list and inner loop will be for 2nd list. In the inner loop, check if any of nodes of 2nd list is same as the current node of first linked list. Time complexity of this method will be O(mn) where m and n are the number of nodes in two lists.

Method 2 (Mark Visited Nodes)
This solution requires modifications to basic linked list data structure. Have a visited flag with each node. Traverse the first linked list and keep marking visited nodes. Now traverse second linked list, If you see a visited node again then there is an intersection point, return the intersecting node. This solution works in O(m+n) but requires additional information with each node. A variation of this solution that doesn’t require modification to basic data structure can be implemented using hash. Traverse the first linked list and store the addresses of visited nodes in a hash. Now traverse the second linked list and if you see an address that already exists in hash then return the intersecting node.

Method 3(Using difference of node counts)
1) Get count of the nodes in first list, let count be c1.
2) Get count of the nodes in second list, let count be c2.
3) Get the difference of counts d = abs(c1 – c2)
4) Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes.
5) Then we can traverse both the lists in parallel till we come across a common node. (Note that getting a common node is done by comparing the address of the nodes)
Time Complexity: O(m+n)
Auxiliary Space: O(1)

Method 4(Make circle in first list)
Thanks to Saravanan Man for providing below solution.
1. Traverse the first linked list(count the elements) and make a circular linked list. (Remember last node so that we can break the circle later on).
2. Now view the problem as find the loop in the second linked list. So the problem is solved.
3. Since we already know the length of the loop(size of first linked list) we can traverse those many number of nodes in second list, and then start another pointer from the beginning of second list. we have to traverse until they are equal, and that is the required intersection point.
4. remove the circle from the linked list.

Time Complexity: O(m+n)
Auxiliary Space: O(1)

Method 5 (Reverse the first list and make equations)
Thanks to Saravanan Mani for providing this method.

1) Let X be the length of the first linked list until intersection point.
Let Y be the length of the second linked list until the intersection point.
Let Z be the length of the linked list from intersection point to End of
the linked list including the intersection node.
We Have
X + Z = C1;
Y + Z = C2;
2) Reverse first linked list.
3) Traverse Second linked list. Let C3 be the length of second list - 1.
Now we have
X + Y = C3
We have 3 linear equations. By solving them, we get
X = (C1 + C3 – C2)/2;
Y = (C2 + C3 – C1)/2;
Z = (C1 + C2 – C3)/2;
WE GOT THE INTERSECTION POINT.
4) Reverse first linked list.
Advantage: No Comparison of pointers.
Disadvantage : Modifying linked list(Reversing list).

Time complexity: O(m+n)
Auxiliary Space: O(1)

Method 6 (Store the first node and reverse the first list) This method is only to detect if there is an intersection point or not. Below algorithm will return 1 if there is an intersection else 0. (Thanks to Hari Prasad Perabattula for suggesting this)

1). Store the header node of list l1.
2). Reverse the first list l1.
3). Traverse the second list until reaches NULL.
4). Check if the node just before NULL is the same as the header in step (1).
If it is then there is an intersection(return 1), otherwise not (return 0).
5).Reverse the list l1 to make it restore the original list.
Time complexity of this method is O(m+n) and used Auxiliary space is O(1)

source: geeksforgeeks

- djebel January 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You cheater cock. Teri maa ki choooooot (badi waali)

- Anonymous February 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1) go to the last element of the first
2) go to the last element of the second
3)if they are equal the they intersect ( once they intersect they have the same nodes until the finish inclusive the last node.

- Marius March 04, 2012 | 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