• No results found

Minimum Value of a Binary Search Tree

In document Ibm Aptitude Numerical Series (Page 94-98)

for(i=0;i < len;i++) {

if(*(temp+i)==1)

printf("%c",*(str+i));

}

printf("\n");

} }

8)

Mergesort:a is an array of n intergers,temp is just a temporary array.low is initially 0 and high is n-1.

void mergesort(int *a,int *temp,int low,int hi){int mid;if(hi==low){return;}else{mid=(low+hi)/

2;mergesort(a,temp,low,mid);printf("1\n");mergesort(a,te mp,mid+1,hi);printf("2\n");merge(a,temp,low,mid+1,hi);}}

void merge(int a[],int temp[],int left,int rig,int r) {printf("%d %d %d\n",left,rig,r);int

i,l,n,k;l=rig-1;k=left;n=r-left+1;while(left<=l&&rig<=r) {if(a[left]<=a[rig]

)temp[k++]=a[left++];elsetemp[k++]=a[rig++];}while(left<=l) temp[k++]=a[left++];while(rig<=r) temp[k++]=a[rig++]; for(i=0;i

< n;i++,r--) a[r]=temp[r];}

Quicksort:a is an array of n integers.left is initially 0 and right is n-1.

void quick(int *a, int left, int right){int

temp,i,k;temp=left;if (left>=right){ return;}k=(left+right)/

2;swap(a,left,k);for (i=left+1;i<=right;i++){if (a[i] < a[left]

){swap(a,++temp,i);}}swap(a,left,temp); //swap a[left] and a[temp]quick(a,left,temp-1);quick(a,temp+1,right);}

Minimum Value of a Binary Search Tree

Posted: Thu, 29 Nov 2007 10:10:14 -0600

Question:Write a C program to find the minimum value in a binary search tree.Solution:

#include<stdio.h>struct binarysearchtree{int data;struct

binarysearchtree* left;struct binarysearchtree* right;};typedef struct binarysearchtree* tree;tree min(tree T){if (T==NULL) return NULL;else{if(T->left==NULL)return T;elsereturn

min(T->left);}}

24.Given two binary trees, write a compare function to check if they are equal or not. Being equal means that they have the same value and same structure.

Solution:The following is a function to check if the two trees are similar or not.It returns true if they are similar else false.

int compareTree(struct node* a, struct node* b) { if (a==NULL && b==NULL)

return(true);

else if (a!=NULL && b!=NULL) { return(

a->data == b->data &&

compareTree(a->left, b->left) &&

compareTree(a->right, b->right) );

}

else return(false);

}

25.Implement put/get methods of a fixed size cache with LRU replacement algorithm.

Solution:Each cache unit consists of an id,data and its age.In the Least recently used algorithm if the cache is full and we need to put some data, we replace it an the unit whose age is the least.

Getting some data is just a search for the data thereby incrementing it age and resorting the cache units.

get(id) {

z=search(id);

data=cache[z].data;

cache[z].age++;

sort(cache);

return(x);

}

put(id,x) {

if(top==cachesize) //if cache is full

top--cache[top].id=id;

cache[top].data=x;

cache[top].age=0;

top++;

}

26 You are given with three sorted arrays ( in ascending order), you are required to find a triplet ( one element from each array) such that distance is minimum.

Distance is defined like this :

If a[i], b[j] and c[k] are three elements then

distance=max(abs(a[i]-b[j]),abs(a[i]-c[k]),abs(b[j]-c[k]))"

Please give a solution in O(n) time complexity

Solution:Point to the first elements of the three arrays, namely a[0],b[0],c[0].

Find the smallest and second smallest of the three.Let us say that a[0] is the smallest and b[0] is the second smallest. Increment the pointer of a until you find a[i]>b[0]. Calculate the difference between a[i-1] and c[0] and store it as current min. Now,again find the smallest and second smallest between a[i], b[0], and c[0] and repeat the above process. If the new difference is smaller than current min,update the value of current min.

Repeat the above process until one of the arrays are finished.

27.Classic - Egg Problem

You are given 2 eggs.You have access to a 100-storey building.

Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100 th floor.Both eggs are identical.You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking.

Now the question is how many drops you need to make. You are allowed to break 2 eggs in the process.

Solution:Let d be the number of drops required.

Now we need to find an optimal solution no matter at which floor the egg breaks.

So we find d such that it doesn't depend on the floor number.

Let us break the egg at floor d. If the egg breaks then we have atmax d-1 floors to test for the highest floor,thus making it d breaks in total.

If the egg doesn't break at floor d,then proceed to floor 2d-1,where we make the 2nd attempt.If it breaks here we have d-2 breaks in the worst case to find the highest floor.

We proceed in this fashion,till we reach the 100th floor.

Thus we break at d,2*d-1,3*d-2,....

thus d+(d-1)+(d-2)+.... 1 <=100

=>d=14

Thus we need atmax of 14 attempts for any case.

Solutions to the remaining puzzles will be posted soon. If we have a solution then please comment it.

● How do you reverse a singly linked list? How do you reverse a doubly linked list? Write a C program to do the same.

● Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?

● How do you sort a linked list? Write a C program to sort a linked list.

● How to declare a structure of a linked list?

● Write a C program to implement a Generic Linked List.

● How do you reverse a linked list without using any C pointers?

● How would you detect a loop in a linked list? Write a C program to detect a loop in a linked list.

● How do you find the middle of a linked list? Write a C program to return the middle of a linked list.

● If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

● How to compare two linked lists? Write a C program to compare two linked lists.

● How to create a copy of a linked list? Write a C program to create a copy of a linked list.

● Write a C program to free the nodes of a linked list.

● Can we do a Binary search on a linked list?

● Write a C program to return the nth node from the end of a linked list.

● How would you find out if one of the pointers in a linked list is corrupted or not?

● Write a C program to insert nodes into a linked list in a sorted fashion.

● Write a C program to remove duplicates from a sorted linked list.

● How to read a singly linked list backwards?

● How can I search for data in a linked list?

In document Ibm Aptitude Numerical Series (Page 94-98)