Microsoft Interview Question for Software Engineer in Tests






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

C++:
class Node
{
char* value;
Node* next;
public:
Node** RemoveNth (Node** list, int n);
};

Node** RemoveNth (Node** list, int n)
{
int i;
Node* traverse, follow;
traverse = *list;
if(!list)
{
cout<<"The list is empty";
return(NULL);
}
follow->NULL;
for(i=1;i<=n && traverse->next!=NULL;i++)
{
follow = traverse;
traverse=traverse->next;
}
if(i!=n)
{
cout<<"The list does not contain "<<n<<" elements. ";
return(NULL);
}
follow->next = traverse->next;
return(list);
}
Am i missing something here??

- S March 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If (n==1) you need a special condition to handle it.

temp = *list;
if (n==1)
{
(*list) = (*list)->next;
free(temp);
}

- harish March 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C#:

class Node
{
string value;
Node next;
}
Node RemoveNth(Node list, int n)
{
	Node pre=list;
	for (int i=0;i<n-1;i++)pre=pre.next;
	pre.next=(pre.next).next;
	if(n==1)list=pre;
	return list;
}

- ryan March 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can anyone write atleast 5 scenarios to test the above function?

- abhays.1984 March 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. 'n' is somewhere in the middle of the list. //free memory & remove it from list.
2. 'n' = 0. Removing the first element. // update header to next & free up 1st element.
3. 'n' = last element. // update the list - last element.
4. list has only 1 element. and n = that element. // free it & make list null.
5. 'n' not in the list. // Return the same list
6. what if list is circular & 'n' is not in the list? // write some detectCircular() method to check this.

- B March 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

add one more test case to B's

7. test when n is a negative number. Sound ridiculous but we can not guarantee n > -1.

- Anonymous May 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

8. For extra credit don't need to test for n is null in case the programmer forget to initialize n since the compiler should do it job well.

- Anonymous May 18, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey S, good job except one error in the for loop..
In the condition check, you traverse to next element in the list which is not necessary as you already have traverse->next!=NULL just before loop..traverse!=NULL is needed there.. so you for loop will be..

for(i=1;i<=n && traverse!=NULL;i++)
{
follow = traverse;
traverse=traverse->next;
}

- veb October 18, 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