Wednesday 14 May 2014

Interview Java Programs solved

1. Find the count of largest common subsequence in the given two strings.
Eg. "Hetlloxhihi" , "jeyllorhiki"
largest common sequence is "llo".
So output is 3.

public class longestcommonsub {
    
    public static void main(String args[])
    {
        char arr1[]="howareue".toCharArray();
        char arr2[]="howsocrue".toCharArray();
        int count2=0,count1=0;
        int j=0,k=0;
        for(int i=0;i<arr1.length;i++)
        {   
            if(count2==count1)
            {
            j=k;
            }
            else
            {
                count2=count1;
            }
            while(j<arr2.length)
            {
                if(arr1[i]==arr2[j])
                {
                count1++;
                    j++;
                    k=j;
                    break;
                }
                else
                    j++;
            }
        }
        System.out.println("output is" +count1);
        
    }
}


output:

output is 5.


2. Find the Count of largest increasing sequence of number in the given array of number.
       Eg. input: 0,2,13,4,6,5
            Output : 4 ( bcoz - 0,2, 4,5)

public class LogestSeq {
 
    public static void main(String args[])
    {
        int arr[]=new int[]{0,2,13,4,6,5};
        int arr1[]=new int[15];
       int temp;
        int len=arr.length;
        int pos=0;
        for(int i=0;i<len;i++)
        {
            arr1[i]=arr[i];
        }
         for(int i=0;i<len;i++)
        {
            for(int j=0;j<len;j++)
            {
               if(arr1[i]<arr1[j])
               {
                   temp=arr1[i];
                   arr1[i]=arr1[j];
                   arr1[j]=temp;
               }
                 
            }
        }
         for(int i=0;i<len;i++)
             System.out.print(arr[i]+",");
         System.out.print("\n");
         // for(int i=0;i<len;i++)
            // System.out.print(arr1[i]+",");
        int count=0,j,max=1;
        //j=arr[0];
        for(int k=0;k<len;k++)
        {
        for(int i=k;i<len;i++)
        {
            temp=arr1[i];
            for(j=0;j<len;j++)
            {
                if(temp==arr[j])
                {
                    break;
                }
                 
            }
         
            if(j>=pos)
            {
               count++;
            pos=j;
            }
        }
        if(count>max)
        {
            max=count;
            count=0;
        }
        }
       System.out.println("\n"+max);
     
    }
}


Output: 
4

3. Program to find the  maximum count of number of continuously occurring character in the given string.

For eg.

Input : " aaaxxbbbbbyyyy"
Output: "5

public class max_letter {
    public static void main(String args[])
    {
        char arr[]=new char[100];
        String x="hhee@@@iiiiiii";
        arr=x.toCharArray();
        int len=arr.length;
        int i=0,count=1,index=0,max=0;
        while(i<len-1)
        {
         
            if(arr[i]==arr[i+1])
            {
                count++;
                if(i==len-2)
                {
                if(max<count)
                {
                    max=count;
                    count=1;
                    index=i;
                 
                }
                }
            }
            else
            {
                if(max<count)
                {
                    max=count;
                    count=1;
                    index=i;
                 
                }
            }
             
         
         
            i++;
         
        }
        System.out.println("max="+max+"letter="+arr[index]);
}
}

output: max=7   letter=i


4. Program to find the longest substring in the given line of string.

Eg. Input:   " Hi i am sangeeth"
      Output: sangeeth   ( as sangeeth is of 8 length)

public class strnl {
    
    public static void main(String args[])
    {
        String arr="Hell hr are umberlajhj";
        char arr1[];
        arr1=arr.toCharArray();
        int i=0,max=0,count=0,index=0;
        //System.out.println(arr1.length);
        while(i<arr1.length)
        {
            if(arr1[i]!=' ' )
            {
                i++;
                count++;
                if(i==arr1.length-1)
                {
                 if(max<count)
                {
                    max=count+1;
                    index=i-count;
                }
                }
            }
            else if(arr1[i]==' ')
            {
                if(max<count)
                {
                    max=count;
                    index=i-count;
                }
                count=0;
                i++;
            }
                         
        }
System.out.println("max="+max);
for(int j=0;j<max;j++,index++)
    System.out.print(arr1[index]);

    }
    }
output: max=10
             umberlajhj

5. Find the Nth Largest number in the given set of Array.

ex. Input : { 32,2,100,7,3}
N= ? = 3.
OUTPUT: 7 ( 3RD LARGEST NUMBER)

public class NthLarge {
    public static void main(String args[])
    {
        int arr[]=new int[]{1,45,6,3,99,100,1000,409};
        int n,max=0,j=0;
        int temp=0;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter the Nth value");
        n=in.nextInt();
        while(j<n)
        {
            max=arr[0];
         
            for(int i=0;i<arr.length;i++)
            {
            if(j==0)
            {
                if(arr[i]>max)
                    max=arr[i];
            }
            else
            {
                if(arr[i]>max && arr[i]<temp)
                    max=arr[i];
            }
             
            }
            temp=max;
         
            j++;
        }
        System.out.println("max="+max);
    }
}

Output:
Enter the Nth value
3

max=100

6. Print the numbers of array in the given order after partitioning it in the given size.
// enter partition in array. then partition order to print output.
// eg. 12345 - size =2 , partiton={12},{34},{5} .order = 3,2,1=> o/p => 53412

import java.util.Scanner;


public class Programming {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
         int arr[]=new int[10];
         System.out.println("Enter size and then array elements");
         Scanner in=new Scanner(System.in);
         double h=(double)in.nextInt();
       for(int i=0;i<(int)h;i++)
       {
         arr[i]=i+1;
       }
       int arr2[]=new int[6];
     
      int p,q;
      q=in.nextInt();
      double x=Math.ceil(h/q);
      System.out.println(x);
   
       System.out.println("enter order up to"+(int)x);
      for(int i=0;i<(int)x;i++)
      {
       
          arr2[i]=in.nextInt();
      }
       //System.out.println("enter order");
      int intx=(int)x;
       int temp=(intx*q)-q;
       for(int i=0;i<intx;i++)
       {
         int j=arr2[i];
        p=((j*q)-q);
        //System.out.println("j="+j+" p="+p);
     
          if(p==temp && (((int)h%q)!=0))
          {
        for(int k=0;k<((int)h%q);k++)
        {
              System.out.print(arr[p++]);
        }
       
           }
          else
          {
        for(int k=0;k<q;k++)
        {
              System.out.print(arr[p++]);
        }
       }
System.out.println("-");
    }
    }
 
}

output: 

Enter size and then array elements are assigned automatically
8
Enter partitoning size
3
enter order up to3
3
1
2
78-
123-
456-

7. Remove the even elements in the given array by replacing it with zeros and print the modified sequence by moving zero to tail.

Input: 1 ,2 ,4, 5,7
Output: 15700

public class even_rem {
    public static void main(String args[])
    {
        int arr[]=new int[100];
        int len;
        int i,j=0;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter no of elements");
        len=in.nextInt();
        for(i=0;i<len;i++)
          arr[i]=in.nextInt();
        for(i=0;i<len;i++)
        {            
           if(arr[i]%2==0)
            {
                arr[i]=0;
             
             }
            else
            {
                arr[j]=arr[i];
                if(i!=j)
                {
                 arr[i]=0;
                }
                 j++;
            }
     
        }
         for(i=0;i<len;i++)
        {
           System.out.print(arr[i]);
        }
    }
}

8.  Print the matrix in spiral pattern.

public class spiralArray {
   
    public static void main (String[] args)
    {
        System.out.println("hello");
        int arr[][]=new int[25][25];
        int i,j;
        Scanner in=new Scanner(System.in);
        System.out.println("Enter no of elements- 3 or 5 or 7");
        int len=in.nextInt();
        for(i=0;i<len;i++)
        {
            for(j=0;j<len;j++)
            {
                arr[i][j]=(i+1)*(j+1);
                     
             }
        }
        System.out.println("Array elements");
        for(i=0;i<len;i++)
        {
            for(j=0;j<len;j++)
            {
                System.out.print(arr[i][j]+" ");
                     
             }
            System.out.println();
        }
        i=len/2;
        j=len/2;
        int count=1;
        System.out.println("Spiral print");
         System.out.println(arr[i][j]);
         
        while(i!=0&&j!=0)
        {
         j--;count=count*2;
        for(int ctr=0;ctr<count;ctr++,i++)
        {
        System.out.print(arr[i][j]+" ");
       
        }
        i--;j++;
        System.out.println("...");
       
       // System.out.println("i"+i+",j"+j);
       
       for(int ctr=0;ctr<count;ctr++,j++)
        System.out.print(arr[i][j]+" ");
         System.out.println("...");
         i--;j--;
        // System.out.println("i"+i+",j"+j);
       
         for(int ctr=0;ctr<count;ctr++,i--)
        System.out.print(arr[i][j]+" ");
         System.out.println("...");
         i++;j--;
        // System.out.println("i"+i+",j"+j);
       
        for(int ctr=0;ctr<count;ctr++,j--)
        System.out.print(arr[i][j]+" ");
         System.out.println("...");
         j++;
         //System.out.println("i"+i+",j"+j);
    }
    }
   
}

Enter no of elements- 3 or 5 or 7
5
Array elements
1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 
4 8 12 16 20 
5 10 15 20 25 

Spiral print
9
6 8 ...
12 16 ...
12 8 ...
6 4 ...
2 3 4 5 ...
10 15 20 25 ...
20 15 10 5 ...
4 3 2 1 ...

No comments:

Post a Comment