Interview Question for Software Engineer / Developers


Country: -




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

Two pointers approach does not have any limitation to linked list length. It also does not need to compute number division (n/2). This could save a little bit of CPU clock (if you care that much).
But main advantage here is that two pointers approach can be used with "infinite" length, whereas the counting approach can only use with linked list that has length within Integer range (2^32).

- Anonymous October 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

The question is to find the middle node in "one" traversal, right? That is the difference. The later approach requires two traversals obviously.

- xzy0410 September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hey..could u say wht a traversal mean fr u?? i think in the two pointer approach too v r traversing it twice.the difference is when v finish the traversal wit fast pointer ..v get the middile node immediately wit the help of the second traversal wit the slow pointer....but if u at look at it carefully u do the same amount of work in both the methods...not any more or less..i think the two pointer approach has some benefits related to CACHING..could somebody make tht part clear if possible??

- XYZ September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

few days back anon clarified similar problem here:
careercup.com/question?id=10782829

- anonymous September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The two pointer approach is better.

1. In the 2 pointer approach no of times, we check for NULL condition is always n/2, to check whether the fast pointer has reached end), whereas in other approach it is n.
2. In 2 pointer approach the middle is reached automatically when fast pointer reaches end of the list whereas in other approach, after finding the length, again we need to check each time if it has reached the middle (for i=1; i <= (n/2); i++),
here i <= (n/2), is an extra computation, which is not required in the first approach.

- Mustafa October 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You could cache n/2 value somewhere first so I don't think it really matters much about extra computation..

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

My comments for two pointer approach against single pointer:
Consider a case when we use single pointer, calculate the length, and then some other part of the application simply appends few nodes in the list (this is realistic, right?)
So when we move ahead and try to traverse to the point (calculatedLength/2), what we are getting is not the correct solution. In worst case, we can also get NPE if all the nodes from rear end to mid have been deleted before we identified middle element.

On the contrary, two pointer approach always gives results based on Linked lists current state.

- Anon October 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi..happy to receive a comment from u..i accept the two pointer approach will work fine in that case..but what if the linked list is not modified??.(i.e)no nodes are appended after calculating the length..what advantage does the two pointer method has in that case?? why i am emphasizing is because everybody goes with the two pointer approach but i don't find any advantage in it....

- XYZ October 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Need to change code. now it should work
Node* FindMiddleNoad(Node* aNode)
{
Node* MidNode=aNode;
while(aNode->Next!=null && aNode->Next->Next!=null)
{
MidNode=Midnode->Next;
aNode=aNode->Next->Next;
}
return MidNode;
}

- Vikas October 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey,
We can use skip list in this case:

1) While traversing each node in the Linked List make a skip list of the odd numbered nodes. Time: O(n) Space: O(n/2)

2) Now when you reach the end of the list divide the legnth by 2 and add 1 to get the middle element index.

3) Search for the index in the skip list O(logn) time.

So, Overall Time Complexity of the algorithm would be :

O(n)(1 traversal) + O(logn)(Searching Skip list) = O(n)
Space Complexity : O(n/2)

Please reply if this is inefficient....

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

i think two pointer approach is much faster as when the fast pointer becomes null, immediately middle element is obtained

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

i think two pointer approach is much faster as when the fast pointer becomes null, immediately middle element is obtained

- aaad December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

If extra memory is allowed ,use hash map. While traversing the list add nodes to hash map, where key will be position of node and value will be address of the node.As length is always odd so return hash[(n+1)/2].

- sumanth45 September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you're going to do that, might as well use an array. You know the keys will be from 1 to n anyway. In any event, the whole point of this problem was probably to try to solve it in a way that avoids O(n) extra memory. Otherwise, it's just trivial.

- eugene.yarovoi October 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

But clearly it needs two traversals..rite??..having two pointers and moving them in different speed doesn't make any difference in the number of traversals we make..rite??

- xyz September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It does not require two traversal. Interivewer clearly mentioned that length is alwasy odd. So that is the catch here.
We cand to it with a single traversal. So keep an extra storage array of node address.
1. Start traversing list and push the alternate node on the array. Keep a count of nodes you are pushing in array.
2. Once you reach at the end of list. Divide the count by 2 and look for that index in your array of nodes and that is the middle node. (take care of indexing starting at 0)
For example
1->2->3->4->5->Null
array will have arr[0]=Node 1
arr[1]=Node 3
arr[2]=Node 5
Total count is 3 so 3/2 gives 1 and that is the middle element which is arr[1].

- tezkrishna September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dumb question: What will you initialize the array to if don't know the number of alternate nodes?

- Novice October 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can use dynamic array initialization or you can use the concept of vectors...

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

you can use dynamic array initialization or you can use the concept of vectors...

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

you can use dynamic array initialization or you can use the concept of vectors...

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

your logic doesnt work for the below example

1->2->3->4->5->6->7->Null
array will have arr[0]=Node 1
arr[1]=Node 3
arr[2]=Node 5
arr[3]=Node 7
Total count is 4 so 4/2 gives 2 and that is the middle element which is arr[2] according to you.
but the actual answer is node 4.

- ajayrules June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

only store nodes with even position, as we need to return hash[(n+1)/2] which is even,that way we dont need to store all nodes.

- sumanth45 September 30, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorry my above statement is wrong, we need to store all nodes i guess.

- sumanth45 September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

nope, store only odd notes and n+1/2 is the answer.

- Krishna September 30, 2011 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't work. It just finds the third to last node.

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

It will work correctly.... try with some example..

- padmaja October 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

It will work correctly.... try with some example..

- padmaja October 02, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

this will always return the third last node and not the middle node

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

Node* FindMiddleNoad(Node* head)
{
Node* MidNode=head;
while(head->Next!=null && head->Next->Next!=null)
{
MidNode=Midnode->Next;
head=head->Next->Next;
}
return MidNode;
}

- Vikas October 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes the solution I gave is wrong and will always give the third node from the last.
In the above solution you provided is using two node pointers and two traversals are happening.

If the question does not limit abt space and time, then array or hashing is correct. But we will loose the property of linked lists.

I dont think any such solution exists.

- madhukits@yahoo.com October 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey,

What about implementing a Skip List...

1) While Traversing the Linked List, build a level 2 linked list which will contains odd numbered nodes.

Takes 1 traversal, O(n) Time, O(n/2) Space

2) At the end of list divide the length by 2 and add 1 to get the middle element number.

3) Search for node in the skip list O(logn) time.

Overall Complexity : O(n) + O(logn) = O(n)

- RushHour October 15, 2011 | Flag


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