Amazon Interview Question
Software Engineer / Developers4 >
bool CheckBST ( node* T , int min , int max )
{
if ( (T-> data < min ) || (T-> data >= max ) )
return false;
bool p=true,q=true;
if( T-> left !=NULL )
p=CheckBST ( T->left , min, T->data) ;
if( T-> right !=NULL )
q=CheckBST ( T->right , T->data , max ) ;
return p&q ;
}
bool WildCardMatch ( char* pat , char* text )
{
while ( *text )
{
switch ( * pat )
{
case '?' :
if( *text=='.' )return false;
break;
case '*' :
{
while( * pat== '*' )pat++;
if( !*pat )return true ;
while ( *text )
if( WildCardMatch( pat,text++) )return true ;
return false;
}
default :
if( *pat != *text )return false ;
}
pat++;
text++;
}
while( *pat=='*' )
pat++;
return !(*pat) ;
}
1> Implement the Dictionary using TST .
2> Using BFS we can determine the shortest path between one word to another if we consider all words to be nodes .
15 >
void Delnode( node* ptr )
{
if( ptr==NULL )return ;
if( ptr->next==NULL ) return ; /* We cannot do anything :-( */
node* t=ptr->next;
copy( ptr,t ); /* Copy data of t into ptr */
ptr->next=t->next;
t->next=NULL;
free(t);
}
15>
void DelNode(node **head, node *del_ptr)
{
node *temp;
if (*head == NULL || del_ptr == NULL) return;
if (*head->next == del_ptr) {
*head->next = del_ptr->next;
free(del_ptr);
}
temp = *head;
while(temp->next) {
if (temp->next == del_ptr) {
temp->next = del_ptr->next;
free(del_ptr);
return;
}
temp = temp->next;
}
}
with no silly bugs ;)
void DelNode(node **head, node *del_ptr)
{
node *temp;
if (*head == NULL || del_ptr == NULL) return;
if (*head == del_ptr) {
*head = del_ptr->next;
free(del_ptr);
return;
}
temp = *head;
while(temp->next) {
if (temp->next == del_ptr) {
temp->next = del_ptr->next;
free(del_ptr);
return;
}
temp = temp->next;
}
}
1> p= XOR 1 to n
- JD February 02, 2010for i=1 to n-1
p=p XOR a(i)
p is the ans .