Showing posts with label Interview Programs. Show all posts
Showing posts with label Interview Programs. Show all posts

Tuesday, 12 November 2013

Some Useful Data structure interview questions

Remove the leave Node

struct Node * romoveLeafList(struct Node * root){
    if(root==NULL) return NULL;
    if(root->left==NULL && root->right==NULL){
        return NULL;
    }
    root->left=romoveLeafList(root->left);
    root->right=romoveLeafList(root->right);
    return root;
 
}


Remove all the Node which has sum is less than k 

struct Node * sumofnode(struct Node *root, int k, int *sum)
{
    if(root==NULL)return NULL;
    int leftsum=*sum+root->data;
    int rightsum=leftsum;
    root->left=sumofnode(root->left,k,&leftsum);
    root->right=sumofnode(root->right,k,&rightsum);
    *sum=leftsum>rightsum?leftsum:rightsum;
    if(*sum<k)
    return NULL;
    return root;
}

Thursday, 17 October 2013

Sub String Program

Given two String, s1 and s2. 
to find the longest substring which is prefix of s1, as well as suffix of s2. 
Complexity O(n)


#include <stdio.h>
#include <string.h>

main()
{
  char str1[]="abcdeee";
  char str2[]="sssssabc";
  int i=0,j,k,l;
  int n=strlen(str2);
  if(strlen(str1)<strlen(str2)){
      n=strlen(str1);
  }
  k=0;
  l=0;
  for(i=0;i<n;i++){
      j=strlen(str2)-n+i;
      if(str1[l++]!=str2[j]){
        i=k;
        k++;
        l=0;
      }
    }
    printf("%s",str2+strlen(str2)-n+k);

}

Wednesday, 25 September 2013

program to find the square root of a given number without using library function

    #include<stdio.h>
    float SquareRoot(float num);
    void main()
    {
    float input, ans;
  
    ans = SquareRoot(9);
    printf("\n Square Root : %f", ans);
  
    }
    float SquareRoot(float num)
    {
    if(num >= 0)
    {
    float x = num;
    int i;
    for(i = 0; i < 20; i ++)
    {
        x = (((x * x) + num) / (2 * x));
    }
    return x;
    }
    }



Tuesday, 24 September 2013

Given an array , find the nth largest and nth smallest number. Sorting should not be used

This is for 6 number you can make it as you want

void main(){
int a[]={8,4,6,7,4,1},i,l=0,s=0,count=1,flag=0,largest,smallest,ps,pl;
for(i=0;i<6;i++){
    if(a[l]<a[i]){
        if(flag){
            if(a[pl]>a[i])
            l=i;
        }
        else{
        l=i;
        }
    }
    if(a[s]>a[i]){
        if(flag){
            if(a[ps]<a[i])
            s=i;
        }
        else{
        s=i;
        }
    }
    if(i==5){
        count--;
        if(flag==0){
            smallest=s;
            largest=l;
            
        }
        if(count==0){
            break;
        }
        else{
            flag=1;
            pl=l;
            ps=s;
            l=smallest;
            s=largest;
            i=-1;
        }
       
    }
}
        
    
printf("small=%d big=%d",a[s],a[l]);
}



Kindly share your ideas...