Monday, October 12, 2015

Understanding Mutability and Immutability in Java


Mutable Class :

Class which can be changed is mutable. Most of Concrete classes are Mutable only.

Immutable Class :

Which can not be changed is immutable. Standard Example : String Class.

Ques. Why String is Immutable ?

String country ="India";
 Here, India is stored in String Constant Pool.

Again, 
String countryName ="India";
Here, countryName is also referring to same Memory location where India is stored in String Constant pool.

String countryName = countryName + "My Country";



Ques : How can we make our custom class which is Immutable ? (Standard Interview Question)

  


Sunday, September 8, 2013

Remove K nodes from back and add in front in Link List.

WAP such that remove K elements from back and add in front.
Example:
Sample Input : 
Link list :10->20->100->30->50->5->70->234
Given K : 3
Sample Output:
5->70->234->10->20->100->30->50
Algo:
  1. Take two pointer both pointing head at beginning.
  2. Move firstPointer to K node.
  3. Now move both first and second pointer while firstPointer != null.
  4. Here secondPointer will point to Kth Node from Last.
  5. Make node of secondPointer as head of Link list.

Source Code:

Addition of integers given in two linklist.

Problem: You have two numbers represented by a linked list, where each node contains a sin-gle digit The digits are stored in reverse order, such that the 1s digit is at the head of the list Write a function that adds the two numbers and returns the sum as a linked list
EXAMPLE 
Sample Input: (3 -> 1 -> 5) + (5 -> 9 -> 2)
Sample Output: 8 -> 0 -> 8

Sample Input:
Integer 1: 1 1 5 
Integer 2: 1 9 3 
Sample Output:
Result: 2 0 9

Algorithm:

Source Code:

Reversing a sentence.

Problem: WAP reverse a given sentence word by word.

Sample Input       : India is in Asia.
Sample Output    : Asia in is India.
Algorithm:
  1. Reverse each word.
  2. After that Reverse Full sentence.
Source Code:

Junit Test case for above Program:

Program to convert Integer to Roman literals form.(Only applicable for 1 to 1000)

Algorithm:
  1. Make a Tree map of basic elements which ll store Roman literals corresponding to Basic elements like 1 ,4,5,9,10,40,50,90,100 and so on..
  2. Get floor value for input  from map and print corresponding roman value.
  3. Subtract floor value from input value and do step 2 and step 3 until input value > 0
Source Code:
package com.learning;

import java.util.Scanner;
import java.util.TreeMap;

public class IntToRomanConversion {

 private static Scanner s;

 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println("Enter the No to Convert :");
        s = new Scanner(System.in);
        int num = s.nextInt();
  converter(num);
  
 }

 // Method for converting Decimal to Roman
 
    static void converter(int input){
 
 
         TreeMap basicElement = new TreeMap();
          
          basicElement.put(1,"I");
          basicElement.put(4,"IV");
          basicElement.put(5,"V");
          basicElement.put(9, "IX");
          basicElement.put(10,"X");
          basicElement.put(40, "XL");
          basicElement.put(50,"L");
          basicElement.put(90, "XC");
          basicElement.put(100,"C");
          basicElement.put(400, "CD");
          basicElement.put(500,"D");
          basicElement.put(900, "CM");
          basicElement.put(1000,"M");
          
          while(input>0){    
           Integer lower=basicElement.floorKey(input);
           System.out.print(basicElement.get(lower));
              input=input-lower;
          }      
     } 
}

Sample Input:
Enter the No to Convert :
123
Sample Output: 
CXXIII

Saturday, September 7, 2013

Dynamic Programming: Finding Largest Integer Sequence.

Check is given BST balanced or not.

Algorithm:

  1. Recursively find max height and min height of tree.
  2. Subtract max height-min height.
  3. If result is 0 or 1 then tree is balanced else not.
Source Code:(Java)
package com.tree.exercise;

public class CheckTreeBalanced {

 
 private static int  findMaxHeight(Node root){
  
  if(root==null){
   return 0;
  }
  else
  {
   return 1 + Math.max(findMaxHeight(root.lChild), findMaxHeight(root.rChild));
  }
  
 }
 
private static int  findMinHeight(Node root){
  
  if(root==null){
   return 0;
  }
  else
  {
   return 1 + Math.min(findMaxHeight(root.lChild), findMaxHeight(root.rChild));
  }
  
 }

public static boolean isBalancedTree(Node root){
  System.out.println(findMaxHeight(root));
  System.out.println(findMinHeight(root));
 if(Math.abs(findMaxHeight(root)-findMinHeight(root)) <= 1)
  return true;
 else
  return false;
}

public static void main(String args[]){
 
 Tree newTree = new Tree();
 int[] inputValue = new int[]{5,2,3,6,8,7,1,9};
  for(int i=0;i< inputValue.length;i++){
    newTree.insert(inputValue[i]);
  }
 
 System.out.println(isBalancedTree(newTree.root));
}

}