A tree will have a root node on its top and it has child and child have child.
Node with out child are called leaf.
A Binary Tree contains nodes with two children always.Each node has left node and right node.
Binary search tree is a binary tree having specific ordering. Here always left node contains lesser and right node...
Thursday, 21 September 2017
Wednesday, 20 September 2017
Java Algorithms Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
Getting each element one by one and comparing it with adjacent elements and swapping the positions.
public void bubbleSort() {
int temp, firstValue, secondValue;
int[] arr = { 1, 90, 25, 22, 4, 50 };
printArray(arr);
for (int i = 0; i < arr.length; i++) {
...
Java Algorithms Linear Search
A linear search traverse down a list, one item at a time, without jumping.
public String linearSearchForValue(int value) {
String indexesWithValue = "";
for (int i = 0; i < size; i++) {
if (theArray[i] == value) {
indexesWithValue += i + " ,";
}
}
return indexesWithValue;
}
.sk-code {
background-color: #ebebeb !important;
padding: 0px 6px 5px 18px !important;
...
Travesrse Matrix Diagonally in Java
A matrix will be in M x N format.So what we need to do is, get value of indexes from 0,0 to M,N.
Itreate the two dimensional array in diagonally as follow
50 36 22
31 88 87
27 73 95
After diagonal traverse the output will be.
50
31 36
27 88 22
73 87
95
Consider the above output
50
31 36
27 88 22
These elements start from left to right top
Need to iterate for 0 to M-1 using for loop and display corresponding...
Tuesday, 19 September 2017
Traverse the matrix as spiral in Java

A matrix will be in M x N format.So what we need to do is, get value of indexes from 0,0 to m,n.
Itreate the two dimensional array in spiral mode as follow
we need to traverse matrix and remove the row/column which already traversed.
We are not going to delete the elements instead we will limit it by defining boundaries.
Boundaries...
Monday, 18 September 2017
Traverse the matrix in Java
Traverse the matrix in Java
A matrix will be in M x N format.
So what we need to do is, get value of indexes from 0,0 to m,n.
Itreate the two dimensional array as follow
take 0(i) and traverse j till j < N
take 1(i) and traverse j till j < N
.
.
till M(i) and traverse j till j < N
PrimeNumberCheck.java
...
Check number is prime or not
To check the given number is prime or not.
Prime Numbers:
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
It means the remainder of divison of N with numbers from 2 to N/2 should be grater than zero.
Lets take 6
6%2=0, 6%3=0...from 2 to 3(2 to 6/2)
here remainder is zero. So 6 is not prime number.
Lets take 5 now
5%2=1
here remainder is greater...
Friday, 15 September 2017
Order of Execution in java Inheritance
Order of Execution
Parent Static Block
Child Static Block
Parent Normal Block
Parent Constructor
Child Normal Block
Child Constructor
Child method invoked
Animal.java
public class Animal {
static {
System.out.println("Animal Static Block ");
}
public Animal() {
System.out.println("Animal Consuctor");
}
{
System.out.println("Animal Normal Block");
}
public...
Java Spring Interview Questions
Load application Context on Spring?
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Autowired
@Autowired
private ApplicationContext appContext;
Spring bean scopes: session and globalSession
GlobalSession is something which is connected to Portlet applications. When your application works in Portlet container it is built of some amount of portlets. Each portlet has its...
Write a code to create deadlock in java
public class DeadLockEx {
private String str1 = "SPRING";
private String str2 = "HIBERNATE";
public static void main(String[] args) {
DeadLockEx app = new DeadLockEx();
app.t1.start();
app.t2.start();
}
Thread t1 = new Thread("Thead 1") {
public void run() {
while (true) {
synchronized (str1) {
synchronized (str2) {
System.out.println(str1 + str2);
...
Java Oops Interview Questions
Implementing two interfaces in a class with same method. Which interface method is overridden?
interface A{
int f();
}
interface B{
int f();
}
class Test implements A, B{
public static void main(String... args) throws Exception{
}
@Override
public int f() { // from which interface A or B
return 0;
}
}
If a type implements two interfaces, and each interface define a method that has...
Tuesday, 12 September 2017
Two Sum on arrays
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
Example:
Given nums = [ 3, 4, 5, 2, 10, 8, 9 ]
target = 19
Because nums[4] + nums[6] = 10 + 9 = 19, return [4, 6]
Approach:
step1. take 3 and then add 3 with all other elements
step2. take 4 and then add 4 with all other elements.Continue same till 9
TwoSum.java
...
Monday, 11 September 2017
Java JDBC Datasouce

Data Source Objects is a factory of connections to physical data sources.
To use datasource, We need to configure the data source in the naming service of JBoss or Tomacat. Webapplication can use JNDI lookup to get the datasource and using that data source we can create the connections.
The interface Datasource is packaged inside javx.sql...
Friday, 8 September 2017
Switching Case of String
All the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase
StringCaseSwith.java
import java.util.Scanner;
/**
* all the uppercase letters should be converted to lowercase and all the
* lowercase letters should be converted to uppercase
*/
public class StringCaseSwith {
public static void main(String[] args)...
Thursday, 7 September 2017
Find missing number between 1 to N with low time complexity
Need to find missing number between 1 to N with low time complexity O(n).
Step1: Get sum of natural numbers from 1 to N as N x(N+1)/2
Step2: Get sum of array elements
Step3: Missing number = natural numbers sum - array sum
FindMissingNumber.java
public class FindMissingNumber {
private static final int END_NUMBER = 10;
private int getMissingNumber(int[] arr) {
int missingNum = 0;
...
When to use Abstract class and When to use Interface in Java
Abstraction is one among the oops concepts and it means hiding the implementation details.In java abstraction can be achieved by two methods.
1. Abstract Class
2. Interface
Abstract Class
Abstract class contains both unimplemented and implemented methods. Abstratct class must have the keyword abstract and it may not contains any abstract method.
If it acontains any abstract method then all...
Set Implementation in Java
Set conatins only unique elements.
HashSet achieves uniqueness of its elements by using hashmap
Whenever we create an object of hashset inernally it will create an object of hashmap.
If we add an element into a set it will store as key for map. But same time we need a value for key. That value can be Object instance.So whenever we add an element to set it will create Hash map entry with key as added element and value...
Wednesday, 6 September 2017
Java Hashmap Implementation

.sk-code {
background-color: #EFF0F1 !important;
padding: 0px 6px 5px 18px;
display: block;
white-space: pre-wrap;
}
Hashmap stores key value pairs
HashMap uses hash to insert and get elements. hash = key.hashCode()%BUCKET_ARRAY_SIZE]
Its an array of Entry LinkedList...
Lifecycle and States of a Thread
.sk-code {
background-color: #EFF0F1 !important;
padding: 0px 6px 5px 18px;
display: block;
white-space: pre-wrap;
}
A thread in Java at any point of time exists in any one of the following states.
New
Runnable
Blocked
Waiting
Timed Waiting
Terminated
...