Microsoft Interview Question
Software Engineer in Tests1. '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.
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.
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;
}
C++:
- S March 19, 2010class 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??