tauqir0007
BAN USER- 0of 0 votes
AnswersHow do a free() knows how much memory has to be free.Suppose
- tauqir0007 in India
int *p=(int*)malloc(sizeof(int)*100);
after some operation,
free(p);
Now how free() function knows from where to where memory has to be free?| Report Duplicate | Flag | PURGE
Amazon Developer Program Engineer C# - 0of 0 votes
Answerswrite a function for a BST to implement best case search.If exact search key not available in BST then return best suited key.Ex- if a tree has keys 21 15 26 30 55 7 and if my search key is 25 then this function should return 26.
- tauqir0007 in India| Report Duplicate | Flag | PURGE
Amazon SDE-2 C#
Thanks Antoni for your respnse.....
Can we(as a programmer) access that structure/information,if yes then how?
int testBst(struct tree * root)
{
if (NULL == root)
return 0;
else
{
if (!( (!root->left || root->left->key < root->key) && (!root->right || root->right->key > root->key ) ))
return 0;
testBst(root->left);
testBst(root->right);
}
return 1;
}
I think this will work on O(logn)
- tauqir0007 September 24, 2014int testBst(struct tree * root)
{
if (NULL == root)
return 0;
else
{
if (!( (!root->left || root->left->key < root->key) && (!root->right || root->right->key > root->key ) ))
return 0;
testBst(root->left);
testBst(root->right);
}
return 1;
}
Hi Aman,
Thanks for ur comment.
Actually the given code wont work in all scenario...even if you put divNum as 14 or 19 etc bcoz its going beyond the range of unsigned int.so for such cases we need to modify code.
#include<stdio.h>
#include<string.h>
void lastSwap( char * str)
{
char temp;
int len=strlen(str);
temp=str[len-1];
str[len-1]=str[0];
str[0]=temp;
}
void conSwap ( char * str)
{
int len=strlen(str),i;
char temp;
for (i=0;i< len-1;i++ )
{
temp=str[i];
str[i]=str[i+1];
str[i+1]=temp;
}
}
int main()
{
char str1[200],str2[200];
int count=0;
printf("enter 2 string");
scanf("%s%s",str1,str2);
if (strlen(str1) != strlen(str2))
{
printf("plz enter equal sized strings");
return;
}
if (!strcmp(str1,str2))
printf("both are already equal");
else
{
while (1)
{
if (!strcmp(str1,str2))
break;
else
{
if (count % 2 == 0)
{
lastSwap(str2);
count++;
}
else
{
conSwap(str2);
count++;
}
}
}
}
printf("required move:%d",count);
return 0;
}
#include<stdio.h>
unsigned int ninezero(unsigned int num1,int divNum)
{
if ( num1% divNum == 0 )
return num1;
while(1)
{
num1=num1*10+9;
if (num1 % divNum == 0)
break;
num1/=10;
num1=num1*10+0;
if (num1 % divNum ==0 )
break;
}
return num1;
}
int main()
{
unsigned int num1=9;
int divNum;
printf("enter a number");
scanf("%d",&divNum);
printf("the number is %d",ninezero(num1,divNum));
return 0;
}
int bestCase(struct tree * root,int bestKey)
{
struct tree * rPtr=NULL;
int prevKey=0;
rPtr=root;
if ( NULL == rPtr)
return 0;
while (NULL != rPtr)
{
if ( bestKey == rPtr->key )
{
prevKey= rPtr->key;
break;
}
else if ( bestKey < rPtr->key)
{
if ( NULL == rPtr->left)
break;
prevKey= (abs(rPtr->key - bestKey) < abs(rPtr->left->key - bestKey))? rPtr->key : rPtr->left->key;
rPtr=rPtr->left;
}
else if ( bestKey > rPtr->key)
{
if ( NULL == rPtr->right )
break;
prevKey= (abs(rPtr->key - bestKey) < abs(rPtr->right->key - bestKey))? rPtr->key : rPtr->right->key;
rPtr=rPtr->right;
}
}
return prevKey;
}
//This will work in O(logn)
- tauqir0007 September 24, 2014#include<stdio.h>
int search(int *arr_temp,int diff)
{
int i;
for(i=0;*(arr_temp+i);i++)
if (*(arr_temp+i) == diff)
return diff;
return 0;
}
int main()
{
int arr[]={5,7,1,50,17,2,9,11};
int x,i,diff,num2=0;
printf("enter number to be searched");
scanf("%d",&x);
for(i=0;i<sizeof(arr)/4;i++)
{
diff=x-arr[i];
if (arr[i+1] && (num2=search(arr+i+1,diff) ))
{
break;
}
}
if (num2 > 0)
printf("\nnumber found,(%d:%d)",num2,arr[i]);
else
printf("\nnot found");
return 0;