Posts

Showing posts from February, 2015

Linked list - Reverse without recursion - java

public class Reverse { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); Node n6 = new Node(6); n1.setNext(n2); n2.setNext(n3); n3.setNext(n4); n4.setNext(n5); n5.setNext(n6); n6.setNext(null); Node nth = reverse(n1); System.out.println(nth.getData()); } private static Node reverse(Node head){ Node temp = null; Node nextNode = null; while(head != null){ nextNode = head.getNext(); head.setNext(temp); temp = head; head = nextNode; } return temp; } } ----------------------------------------------- o/p: 6

Linked list - If loop exists get starting of loop - Java

public class Cirsle { /** * @param args */ public static void main(String[] args) { Node n1 = new Node(1); Node n2 = new Node(2); Node n3 = new Node(3); Node n4 = new Node(4); Node n5 = new Node(5); Node n6 = new Node(6); n1.setNext(n2); n2.setNext(n3); n3.setNext(n4); n4.setNext(n5); n5.setNext(n6); n6.setNext(n3); Node nth = circle(n1); System.out.println(nth.getData()); } private static Node circle(Node root){ Node slwPtr = null; Node fasPtr = null; boolean loopExists = false; if(root != null){ slwPtr = root; fasPtr = slwPtr; } while(fasPtr.getNext() != null && fasPtr.getNext().getNext() != null){ slwPtr = slwPtr.getNext(); fasPtr = fasPtr.getNext().getNext(); if(slwPtr == fasPtr){ loopExists = true; break;    } } if(loopExists){ slwPtr = root; while(slwPtr != fasPtr){ slwPtr = slwPtr.getNext(); fasPtr = fasPtr.getNext(); ...

Find pair whose sum is k - java

public class PairSum {     public static void main(String[] args) {         pair(new int[]{1,2,4,5,6},6);     }         private static void pair(int[] nums, int k){         int i = 0;         int j = nums.length -1;         while(i < j){             if(nums[i] + nums[j] == k){                 System.out.println(nums[i] +"--------"+nums[j]);                 i++;                 j--;             }else if(nums[i] + nums[j] > k)                 j--;    ...

Sum of all nodes in a tree - java

package trees; public class SumOfNodes {     public static void main(String[] args) {         Node rootnode = new Node(25);           System.out.println("Building tree with rootvalue " + rootnode.getNodeValue());           System.out.println("================================");           rootnode.insert(rootnode, 11);           rootnode.insert(rootnode, 15);           rootnode.insert(rootnode, 16);           rootnode.insert(rootnode, 23);           rootnode.insert(rootnode, 79);           System.out.println("Total Count");           System.out.println("=================================");      ...

Height of a binay tree - java

package trees; public class Height {     public static void main(String[] args) {         Node rootnode = new Node(25);           System.out.println("Building tree with rootvalue " + rootnode.getNodeValue());           System.out.println("================================");           rootnode.insert(rootnode, 11);           rootnode.insert(rootnode, 15);           rootnode.insert(rootnode, 16);           rootnode.insert(rootnode, 23);           rootnode.insert(rootnode, 79);           System.out.println("Height");           System.out.println("=================================");         ...

Find next greater number of set digits - Java

import java.util.Arrays; public class NextGreater {     public static void main(String[] args) {         int [] merg = nextGreater(new int[]{5,3,4,9,7,6});         for(int i = 0; i <= merg.length -1; i++)             System.out.println(merg[i]);     }         public static int[] nextGreater(int[] num){         int invIndex = findInvEle(num);         int nexInvToSwap = findNexInv(invIndex, num);         int[] nums = swap(invIndex, nexInvToSwap, num);         int[] merg = sort(nums, invIndex + 1, nums.length -1);                 return merg;     } //find number which smaller while traversing from left to right ...

Longest Palindrome - O(n^2) time and O(1) extra space

public class LongestPalindrome {     public static void main(String[] args) {         lonPal("fasxabccba");     }     public static int lonPal(String s){         int maxlen = 1;                 int start = 0;         int len = s.length();                 int low, high;                 // for even length         for(int i = 1; i <= len; i++){             low = i;             high = i + 1;                         while((low >= 0 && high ...

Quick Sort - Java

import java.util.Arrays; public class QuickSort {     private static int numbers[];        public static void main(String[] args) {         numbers = new int[]{4,5,2,3,6,1};         sort(numbers);         System.out.println(Arrays.toString(numbers));     }        public static void sort(int[] numbers){         if(numbers.length == 0 || numbers.length == 1)             return;                int length = numbers.length;         quicksort(0, length -1);     }        public static void quicksort(int low, int high){         int i = low;         ...

Concurrent Hashmap Internal Working

Source: stackoverflow Multiple partitions which can be locked independently. (16 by default) Using concurrent Locks operations for thread safety instead of synchronized. Has thread safe Iterators. synchronizedCollection's iterators are not thread safe. Does not expose the internal locks. synchronizedCollection does. ------------------------------------------------------------------------ HashTable vs ConcurrentHashMap Hashtable’s offer concurrent access to their entries, with a small caveat, the entire map is locked to perform any sort of operation. While this overhead is ignorable in a web application under normal load, under heavy load it can lead to delayed response times and overtaxing of your server for no good reason. This is where ConcurrentHashMap’s step in. They offer all the features of Hashtable with a performance almost as good as a HashMap. ConcurrentHashMap’s accomplish this by a very simple mechanism. Instead of a map wide lock, the collect...

Inversion of Control vs Dependency Injection

IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application. DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object will 'depend' on in order to behave correctly.