Microsoft Interview Question for Software Engineer / Developers






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

One approach is to reverse the linked list which takes O(n) and print the list which takes another O(n). In total O(2*n) which is nothing but O(n).

But for reversing we need two variables to hold the address of the nodes while reversing. If we approximate the use of the two variables as O(1)memory, this approach sounds plausible.

Please discuss other approaches if any.

- srihari January 04, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

node *reverse(node *head)
{
   node *pre = NULL;
   node *cur = head;
   node *next = cur->next;
   while(next) 
   {
      cur->next = pre;
      pre = cur; 
      cur = next;
      next = cur->next;
   }
   cur->next = pre;
   return cur;
}

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

Onother approach is to use stack.

1: Travel thru the list from the begining to the end and push each data in stack.

2: Just pop up the data from the stack and print them.

- Korea!!!!! January 20, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Korea!*

Interviewers are NOT fools. It is EXPLICITLY mentioned O(1) memory i.e. without recursion.
That does not give you the permission to create your own stack

- TheGreatOne September 08, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a stack is a creative way of doing it, but highly inefficient since it duplicates the data. If we have 500 nodes, then you get 1000 nodes.

You don't always need temp vars. Since ptrs contain integer values, it would seem a good idea to do in-place swap.

- Jack January 21, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can reverse a linked list in O(n) time with O(1) temp memory. (3 variables, i believe)

The major trick in this problem was just the recognition that, while the linked list must stay intact before and after the operation, it doesn't need to stay intact during the operation. You can reverse it, print it, then reverse it back.

- Chao February 19, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good.

- XYZ December 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You mean first reversing it, then print the numbers and then reverse it again.

- Noname June 29, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good.

- xyz December 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Node* ReverseList( Node ** List )

{

Node *temp1 = *List;

Node * temp2 = NULL;

Node * temp3 = NULL;

while ( temp1 )

{

*List = temp1; //set the head to last node

temp2= temp1->pNext; // save the next ptr in temp2

temp1->pNext = temp3; // change next to privous

temp3 = temp1;

temp1 = temp2;

}

return *List;

}

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

Good.

- XYZ December 04, 2008 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Swap the Head and Tail pointers.

- kitcha January 21, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Swapping the Head and Tail Pointers doesnt work for Single Linked List.

- Ajay February 17, 2006 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I already solved this in another section of the site. You can do it in O(n) with a few temp vars.

- Jack February 17, 2006 | 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