Microsoft Interview Question for Software Engineer in Tests






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

void rev(struct node **head)
{
struct node*a=*head,*b=NULL,*c;
while(a!=NULL)
{
c=b;
b=a;
a=a->next;
b->next=c;
}
*head=b;
}

//recursive
void rev(struct node**head)
{
struct node *first,*rest;
first=*head;
rest=first->next;
if(rest==NULL)
return;
rev(&rest);
first->next->next=first;
first->next=NULL;
*head=rest;
}

- rajnesh November 09, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What do you mean by reverse a linked list with single pointers then double pointers?

- Anonymous September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would check the 3 rows and 3 columns and then 2 diagonal lines.

- chinux September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

iterative, single pointers:
----
node * reverse(node* head) {
node *pre = NULL;

while (head) {
node *next = head->next;
head->next = pre;
pre = head;
head = next;
}

return pre;
}

iterative, double pointers:
-----
Who has any ideas?

recursively, not tail-recursive:
------
node *reverse(node* head) {
if (!head) return NULL;
if (!head->next) return head;

node *tail = head->next;
node *newhead = reverse(head->next);
tail->next = head;
head->next = NULL;

return newhead;
}

recursively, tail-recursive:
------
node *reverse(node *head, node *pre = NULL) {
if (!head) return NULL;

node *next = head->next;

head->next = pre;
if (!next) return head;
else return (next, head);
}


What do you do if it has a loop
---------------
If the linked list has a loop, the recursive solution will not work. It runs forever until all memory is used up.
The Iterative solution will still give a result. it depends on the user's opinion whether the result is correct or not.
Generally speaking, I would think that to reverse a linked list with a loop doesn't make much sense.

- Jason September 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

recursively, tail-recursive:
------
node *reverse(node *head, node *pre = NULL) {
if (!head) return NULL;

node *next = head->next;

head->next = pre;
if (!next) return head;
else return (next, head);
}

this will not work since it will make all the previous nodes null ... it need minor adjustments.
-------------------------------------------------
recursively, tail-recursive:
------
node *reverse(node *head, node *pre) {
if (!head) return NULL;

node *next = head->next;

head->next = pre;
if (!next) return head;
else return (next, head);
}

just while calling it for the first time call using the head and null
--------------
head = current head and pre = null
node *reverse(head, pre)

- adr September 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what's tail-recursive,and what's not? thanks

- Anonymous September 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

any recursive solution which end with calling the same function and no other instructions after that is called tail recursive. it saves stack space and take advantage of the in built compiler function which over writes the current activation record

- adr September 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For tail-recursive, this blog has a good explanation
http://blogs.msdn.com/chrsmith/archive/2008/08/07/understanding-tail-recursion.aspx

- onion834 September 29, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void ReverseLinkList()
{
NODE *pNode , *pTemp ;

cout << " REVERSE LINK LIST CALLED " << endl;
if(pHead != NULL)
{
pNode = pHead;

while(true)
{
if(pNode->pPrev == NULL)
{
cout << " HEAD " << endl;
cout << " Current Value " << pNode->nData << endl;
pNode->pPrev = pNode->pNext;
pNode->pNext = NULL;
pTail = pNode;
pNode = pNode->pPrev;
}
if(pNode->pNext != NULL)
{
cout << " Current Value " << pNode->nData << endl;
pTemp = pNode->pNext;
pNode->pNext = pNode->pPrev;
pNode->pPrev = pTemp;
pNode = pNode->pPrev;
}
if(pNode->pNext == NULL)
{
cout << " Current Value " << pNode->nData << endl;
cout << " TAIL " << endl;
pNode->pNext = pNode->pPrev;
pNode->pPrev = NULL;
pHead = pNode;
break;
}
}
cout << " OUT OF WHILE " << endl;
}
else
cout << " No element in the linklist" << endl;
}

- Anonymous November 10, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

[CODE]

void reverse(struct node **head)
{
struct node* temp = *head;
struct node* result = null;
struct node* next;

while(temp!=null)
{
next = temp->next;
temp->next = result;
result = temp;
temp = next;
}
*head = result;
return *head
}
[/CODE]

- Karthik February 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 0 vote

Hi,
Can anyone tell me the best way as how we can solve the first problem ? TicTacToe problem.

Thanks

- MildGeek September 23, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.


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