Bloomberg LP Interview Question
Financial Software DevelopersCorrect me if i'm wrong but since we'r trying to find the element thats X from the end, wont we use N-X in the while loop?
while (c++< (N-X))
and i'm assuming that that while loop encompasses the following while(start) in its loop?
2 cases :
if lenght N is given as in q above :
struct node*ptr=head;
int c=0;
while(c++<N-X)
{
if(ptr->next==NULL)return NOT FOUND;
ptr=ptr->next;
}
return ptr;
if N is not given we use 2 ptrs.
struct node* ptr=head;
struct node* retv=head;
int c=0;
while(c++<X)
ptr=ptr->next;
while(ptr->next!=NULL)
{
ptr=ptr->next;
retv=retv->next;
}
return retv;
list *findX(list *root, int N, int X) {
- ked February 26, 2008int cnt=1;
// Assign start to root.
list *start=root;
list *node=root;
// Assuming that lenght is N and X < N, root will not be NULL
while(cnt++ < X) start=start->next;
while(start) {
start=start->next;
node=node->next;
}
// now node will be pointing to X the node
return node;
}