Interview Question


Country: India




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

I guess following will work:
Here k should be some given no say, 5 or 10 and this is the difference of the length of two linked lists. So both lists will make a Y shape where one list has K node more than the other one. Here traversing complete list is not allowed so we can't find which list is longer one (If allowed, traverse K node 1st one longer list, then traverse both list together node by node and keep checking if they are same which will be the merged node)

So we need to try this on both list together where we can have two pairs of pointers say P1, P2 and Q1, Q2 (P1, Q1 pointing to one list and P2, Q2 pointing two another).
1. Move P1 and P2 upto K nodes
2. Now move all 4 pointers node by node, keep comparing equality on P1, Q2 and P2, Q1. One pair will point to merged node when they reach node p and that will give the answer.

- Anurag Singh December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I am not sure how your logic would work in case there is no difference of length . And also you need to move the pointer K+1 time to get to the merged node. Correct me if I am wrong

- Anonymous December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the answer is correct, it does nto matter if both are the same length because you'll need to move p1,p2 zero times and p1 will move in parallel to q1 the same case for p2 and q2

- calculator.fcis December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

could you please explain what is this k.

- sonu.hzb.bhr December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

do you mean both the complexity and space size to be k but wht is k here??

- probs December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How abt this

Use an hashmap to store the address of each node with address as key and node->data as value.
Now start iterating from head of each list. Store the address of both the head ptrs . So the smaller list will reach the merge point earlier .In each iteration check whether such an address exist in the hashmap. If yes they merge at that point .
Though it will take more k space but cant think of any other solution :(

- Ankur December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think its need space O(2k) -> O(k)

- codeKiller December 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I am not sure how your logic would work in case there is no difference of length . And also you need to move the pointer K+1 time to get to the merged node. Correct me if I am wrong

- Anonymous December 15, 2011 | 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