Monday, July 22, 2013

Select Max k elements from a given array of n elements.

Data Structure Problem 1.
Select Max k elements from a given array of n elements.

Solution :
This problem can be done with 5 different run time algorithmic approach.
1.  O (n^2) .
2. O(nk)
3.  O(n log n).
4.  O(n log k).
5.  O(n).

1. O(n^2).

void selectMaxK(int[] array, in k){
if(n<k){
System.out.println("Array length " + n + " is shorter than " + k);
} else{
int arrayLength = array.length();
HashMap k = new HashMap();
 for(int i=0; i<arrayLength; i++){
 int max=array[i];
  for(j=i+1; j>arrayLength; j++){
 if(array[j]>max){
max=array[j]
}
if(!k.get(max)){
k.put(max,1);
}
}

 }
}
}


Wednesday, June 26, 2013

Data Structure Problem 1 :
Select Max k elements from a given array of n elements :

This problem can be done in 5 different types of algorithmic time runs.
1. O(nxn)

2. nk

Simply scan the array for each element and compare with other elements.
3. O(nlogn)

sort the array and select first k elements.

4. O(nlogk)

create a min heap and insert first k elements in to it. Now for each next element compare the getMin() of heap. If this getMin() is less than the element being compared. replace getMin() element with the new element, else disregard new element. In the end we will have a heap of k max elements form the array.

5. The solution number 4 can further be improved by using the heapifying an array method. which takes O(n) time.

I have not posted the code for readers to give it a try themselves. If you nee pseudocode or code for any of the above solution let me know. I will try to get back to you when time allows. 

Saturday, April 27, 2013

Binary Search in Java


public class BinarySearch {
   
    public static int[] getArray(){
       
        int[] array = {4,5,6,9,45,65,83,88,92,99};
        //for random number
      //  for(int i=0; i<10;i++){
        //    array[i]= new java.util.Random().nextInt();
        //}
        return array;
    }
   
    public static void printArray(int[] array){
        for(int i=0; i<array.length-1;i++){
            System.out.print("|"+array[i]);  
        }
        System.out.println();
    }
   
    public static void binarySearch( int[] array , int val){
        int low=0;
        int high=array.length-1;
      
       
  while(low<high){
      int mid= (low+high)/2;
      if(array[mid]<val){
          low=mid-1;
      }else if(array[mid]>val){
          high=mid+1;
      }else if(array[mid]==val){
          System.out.println("Value found at : " +mid);
          low=high+1;
      }
  }
       
    }
   
    public static void main(String[] args){
       
        int[] theArray = getArray();
        System.out.println("CurrentArray : ");
        printArray(theArray);
        int val=5;
        binarySearch(theArray,val);
    }

}

Thursday, April 4, 2013

More Java interview questions

These are very basic questions but its good to review them

1. Explain Full Join, Right outer join, left outer join, with difference in them.
2. What will happen if I do a Right Outer join and there is no common data in second(left) table
Ans: all right table selected rows will be returns will left table selected data rows with null values.
3. What is outer and inner join.
4. difference in union and union all.
5. difference in truncate and delete.
6. if we have a table with a column named "Days" and this column have days data, count number of days for each type of day
Ans : select count(day) from table name group by day
7. What is inversion of control.
8. What is inversion of control by getter, setter and what is inversion of control by constructor.
9. difference in ibatis and hibernate(if you have worked on both).
10. how can you make a class immutable. (make class final, make methods final, and don't provide any setter for static or constant variables. )
11. What is abstract class, explain it.
12. Difference between hash table and hash map.

Monday, February 4, 2013

Important Java Interview Questions.

I will try to write a series of some of the really good and interesting questions which every Java programmer should know from interview's prospective as well as from coding's prospective.

Here is a list. I will try to write answers as I get free time.

1. What are clustered and non-clustered index.
2. Difference between trigger and stored procedure.
3. what is singleton, synchronize singleton. write code for singleton, walk through code and show how multiple instance will be created in case of multiple threading. synchronize singleton, double check synchronized singleton. lazy loading, eager loading in singleton. volatile keyword and use it in singleton. singleton with public static.
4. difference between hashmap and hashset. which one is derived from which.
5. what is jdbc template--spring.
6. what is IOC.
7. what is dependency injection.
8. what is callable interface.explain it and how and where to use it.
9.write code for producer-consumer scenerio. producer have a pool of 10 threads, consumers haev a pool of 10 threads, producer are constantly producing and consumers are consuming. if consumer is not consuming fast enough, producer should wait for producing till there is a slot available.
10. what is session, session factory--hibernate.
11. builder design patters, factory design patter, difference between them.
12. explain how hashmap works in java.
13. explain memory model and garbage collection in java.(how to do memory optimization in java for out of memory errors)(full memory model, why memory model is devided in to different parts, like young generation, old generation, permgen, why not have a single big heap.)
14. if there is a building with 100 floors , find the higest floor to drop eggs safely (binary tree. start with 100th floor,  then 50th floor.)
15. wrote code to find biggest bcd for 2 number.
16. what are OPPS concept, what is Object Oriented.
17. give examples of how multi-threading is being used in your application.(with sample code)
18. notify, notifyall,
19. Explain one to one and many to one mappings in Hibernate.
20. Difference between inner join, outer join.
21. If there are two transactions and if any of them fails, We do not want to save their data. How will you handle this.
22. Initiate a transaction in spring once and then commit two transactions or mulitple transactions without initiating another transaction session.