Microsoft Interview Question for Software Engineer / Developers






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

ydxr's approach is good but requires O(n) memory. If you are short on memory [interviewers usually ask the classic trade-off alternative i.e. performance/extra computation over memory]

Assuming the size of list is not maintained in the list data structure.

1) Traverse the first list & determine its size lets say szList1.
2) Traverse the 2nd list & determine its size lets say szList2.
3) Find the difference in the size of the list. difference = szList1 - szList2;
4) Once you have the difference, iterate the 1st list or 2nd list for 'difference' number of nodes [if difference < 0, iterate list2 else iterate list1].Intersection will start only after the 'difference' number of nodes. Say if difference = 3, intersection can be at 4th node,..... to Nth node.
5) Once you reach difference number of nodes, break out of the loop.
6) Now both lists have same number of elements to be iterated. In another loop iterate both lists and compare their nodes. You can get the intersection.

Running time = O(n) + O(m) + O( greaterof(n,m))
Worst case = Last element of both lists are same i.e
2O(n) + O(m) if n > m else
2O(m) + O(n) if m > n

Memory = O(1)

- The Hercules November 21, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

awesome

- Anonymous August 23, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hercules, Can you please explain the logic in detail?

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

Scan through the first list and insert each node pointer into a hashmap. Now for each node pointer of the second list check if it is in the hashmap. If the pointer is present in the hashmap then its the point at which they merge. This will take O(n) time.

- ydxr July 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Hercules : Are you assuming that the lists are sorted. If no, I dont get your solution. Why would the intersection start only after difference number of nodes. Lists could be like :

1->2->3.

6->3->5->8->6
In this case your answer would be the lists dont intersect ? Could you please explain your approach in a bit detailed way ?

- Sam May 27, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

silly

- Anonymous August 19, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sam
so in your mind, what is merge?

- sunbow.xs@hotmail.com October 11, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Sam
In this case the node with value 3 would not be like a node of Singly linked list. Either 3's next can be 5 or NULL, but not both.

- varun_agg June 13, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hercules: Big O notation.
if n > m, the algorithm takes n+m+max(n+m), the the algorithm is O(n) not O(n)+O(m)+O(max(n,m)).
2 x O(n) is still O(n).
Your algorithm may not work if they merge at some point then seperate again or they start at the same and then seperate.

Say: 1->2->3->4->5->6
and 7->3->4->8->9->10
and the number of nodes are all 6.

Or say: 1->2->3->4->5->6
and 1->2->7->8->9->10
and the number of nodes are all 6.

- kulang October 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have a sol in linear order of n,m

As Ravi mentioned find out the list which is shorter from the merge pt (can be done by computing the individual lengths and then taking the difference)
So for first list the length is
x + z
for second list its
y + z.
Total lenght x + y + 2z (1)

Now reverse the list which is shorter (say it started from head2 ).

Now if you traverse the list from head 1, u will reach head 2. (Draw the diagram and it would be clear )
The lenght now would be x + y. (2)
Using 1, 2 you can compute the z and the pt where they merge.

- knap May 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have a sol in linear order of n,m

As Ravi mentioned find out the list which is shorter from the merge pt (can be done by computing the individual lengths and then taking the difference)
So for first list the length is
x + z
for second list its
y + z.
Total lenght x + y + 2z (1)

Now reverse the list which is shorter (say it started from head2 ).

Now if you traverse the list from head 1, u will reach head 2. (Draw the diagram and it would be clear )
The lenght now would be x + y. (2)
Using 1, 2 you can compute the z and the pt where they merge.

- knap May 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt this solution?

1. Reverse the list 1
2. Reverse the list 2
3. Then start comparing each node of list 1 with list 2 till min(l1.size,l2.size).The prev node at which the differ is the merging point..

- Sandy August 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt this solution?

1. Reverse the list 1
2. Reverse the list 2
3. Then start comparing each node of list 1 with list 2 till min(l1.size,l2.size).The prev node at which the differ is the merging point..

- Sandy August 28, 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