Honeywell Interview Question
Software Engineer / DevelopersTeam: Sharepoint COE
Country: India
Interview Type: Written Test
Scan the list and keep track of the last vowel encountered. As soon as the current element is non-vowel(consonant), just skip it and continue scanning towards the end of the list. When the current element is a vowel, remove it from the list and add it as the next element of the last encountered vowel and make this newly added element as the last encountered vowel.
How about the below algorithm:
1.Scan from the beginning of the list, ignoring consonants.
2.If you have a vowel at hand, copy it and create a new node at the beginning, update the head pointer and then delete the old node adjusting the pointers.
This would be an O(n) solution as we just need to have a single iteration over the array.
void arrange(NODEPTR* m){
if(*m == NULL){
cout << "List is empty" << endl;
return;
}
NODEPTR init,temp,p,q;
init = (isVowel((*m)->data)) ? (*m) : NULL;
q = *m;
p = q->link;
while(p != NULL){
traverse(*m);
if(isVowel(p->data)){
//adding at beginning
if(init == NULL){
temp = p->link;
p->link = *m;
q->link = temp;
*m = p;
}
else{
temp = p->link;
p->link = init->link;
q->link = temp;
init->link = p;
}
init = p;
}
else
q = p;
p = q->link;
}
}
Just Traverse the Linked List as follows (given Head pointer )
having pointer's : temp ( for traversing ) Prev ( prev of Temp )
temp = Head ;
now move one by one
case 1> if you encounter Vowel ( temp is pointing to it ) so Prev is previous of temp and
Prev -> next = temp -> next ;
temp ->next = Head ;
Head = temp ;
temp = Prev ->next ;
and continue
case 2> if not a vowel ...continue ....
- arun June 29, 2012