├── .gitignore ├── .classpath ├── src └── com │ └── osfg │ ├── utils │ ├── Swapper.java │ ├── BaseNEncodeDecode.java │ └── BTreePrinter.java │ ├── questions │ ├── RotatedString.java │ ├── PrintAllStringPermutations.java │ ├── DoubleToRational.java │ ├── GCDFinder.java │ ├── PrimeNumberPrinter.java │ ├── BSTVerifier.java │ ├── ParenthesisCombinationPrinter.java │ ├── ShortURLGenerator.java │ ├── SquareRoot.java │ ├── LCAFinder.java │ ├── RotatedPalindrome.java │ ├── QueueWithStack.java │ ├── BTreeTraversal.java │ ├── IndexInSortedArray.java │ ├── StairwayClimbWaysFinder.java │ ├── LinkedListMidFinder.java │ ├── ThreadPoolManager.java │ ├── SortedArraysMerger.java │ ├── PairsWithSumFinder.java │ ├── LinkedListReversal.java │ ├── StringExpressionEvaluator.java │ ├── IntegralPointsOnCirle.java │ ├── SortedOccurenceCounter.java │ ├── PrintOddEven.java │ ├── MPlusNArrayMerge.java │ ├── FormLargestNumber.java │ └── LinkedListPalindromeFinder.java │ ├── models │ ├── BTreeNode.java │ ├── ListNode.java │ └── BlockingQueue.java │ └── factory │ └── DSFactory.java ├── .project ├── .settings └── org.eclipse.jdt.core.prefs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/com/osfg/utils/Swapper.java: -------------------------------------------------------------------------------- 1 | package com.osfg.utils; 2 | 3 | public class Swapper { 4 | 5 | public static void swap(char[] array, int x, int y){ 6 | char temp = array[x]; 7 | array[x] = array[y]; 8 | array[y] = temp; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | InterviewQuestions 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/com/osfg/questions/RotatedString.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Verify if strings are rotations of each other or not 7 | */ 8 | public class RotatedString { 9 | 10 | public static boolean isRotation(String a, String b) { 11 | 12 | String temp = a + a; 13 | if(temp.contains(b)) { 14 | return true; 15 | } 16 | return false; 17 | } 18 | 19 | // main method -tests 20 | public static void main(String args[]) { 21 | 22 | System.out.println(isRotation("AACD","ACDA")); 23 | System.out.println(isRotation("AACD","AAAA")); 24 | 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.8 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /src/com/osfg/questions/PrintAllStringPermutations.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.utils.Swapper; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Question : String Permutation 9 | */ 10 | public class PrintAllStringPermutations { 11 | 12 | 13 | public static void pringStringPermutation(char[] array, int index) { 14 | 15 | if(index == array.length-1) { 16 | System.out.println(new String(array)); 17 | return; 18 | } 19 | 20 | pringStringPermutation(array, index+1); 21 | 22 | for(int i=index + 1; i 1/8 8 | */ 9 | public class DoubleToRational { 10 | 11 | public static String getRational(double no) { 12 | int tenPower = 10; 13 | while(no*tenPower - (int)(no*tenPower) != 0) { 14 | tenPower = tenPower * 10; 15 | } 16 | 17 | int divident = (int)(no*tenPower); 18 | int divisor = tenPower; 19 | 20 | int gcd = GCDFinder.findGCD(divident, divisor); 21 | divident = divident / gcd; 22 | divisor = divisor / gcd; 23 | return divident + "/" + divisor; 24 | } 25 | 26 | //main method - tests 27 | public static void main(String args[]) { 28 | System.out.println(getRational(0.125)); 29 | System.out.println(getRational(0.5)); 30 | } 31 | 32 | 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/com/osfg/questions/GCDFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Finding GCD of two numbers 7 | */ 8 | public class GCDFinder { 9 | 10 | /** 11 | * recurssive approach 12 | * @param a 13 | * @param b 14 | * @return gcd of a and b 15 | */ 16 | public static int findGCD(int a, int b) { 17 | 18 | int remainder = a % b; 19 | 20 | if(remainder == 0) { 21 | return b; 22 | } 23 | else { 24 | return findGCD(b, remainder); 25 | } 26 | 27 | } 28 | 29 | public static void main(String args[]) { 30 | System.out.println("GCD of 10 and 4 : " + findGCD(10,4)); 31 | System.out.println("GCD of 100 and 10: " + findGCD(100,10)); 32 | System.out.println("GCD of 3 and 9 : " + findGCD(3,9)); 33 | System.out.println("GCD of 11 and 24 : " + findGCD(11,24)); 34 | } 35 | 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/com/osfg/models/BTreeNode.java: -------------------------------------------------------------------------------- 1 | package com.osfg.models; 2 | 3 | 4 | /** 5 | * 6 | * @author athakur 7 | * Model class for tree node 8 | */ 9 | public class BTreeNode { 10 | 11 | int data; 12 | BTreeNode left; 13 | BTreeNode right; 14 | 15 | public BTreeNode(int data) { 16 | this.data = data; 17 | } 18 | 19 | public int getData() { 20 | return data; 21 | } 22 | 23 | public void setData(int data) { 24 | this.data = data; 25 | } 26 | 27 | public BTreeNode getLeft() { 28 | return left; 29 | } 30 | 31 | public void setLeft(BTreeNode left) { 32 | this.left = left; 33 | } 34 | 35 | public BTreeNode getRight() { 36 | return right; 37 | } 38 | 39 | public void setRight(BTreeNode right) { 40 | this.right = right; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "BTreeNode [data=" + data + "]"; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/com/osfg/models/ListNode.java: -------------------------------------------------------------------------------- 1 | package com.osfg.models; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Model class for Linked List Node 7 | */ 8 | public class ListNode { 9 | 10 | private E value; 11 | private ListNode next; 12 | 13 | public ListNode(E value) { 14 | this.value = value; 15 | } 16 | 17 | public E getValue() { 18 | return value; 19 | } 20 | public void setValue(E value) { 21 | this.value = value; 22 | } 23 | public ListNode getNext() { 24 | return next; 25 | } 26 | public void setNext(ListNode next) { 27 | this.next = next; 28 | } 29 | 30 | public String toString() { 31 | ListNode currNode = this; 32 | String ll = String.valueOf(currNode.getValue()); 33 | while(currNode.next != null) { 34 | ll = ll + " --> " + String.valueOf(currNode.getNext().getValue()); 35 | currNode = currNode.getNext(); 36 | } 37 | return ll; 38 | } 39 | 40 | 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/com/osfg/questions/PrimeNumberPrinter.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Given a number n, print all primes smaller than or 7 | * equal to n. 8 | */ 9 | public class PrimeNumberPrinter { 10 | 11 | public static void primeNumberPrinter(int n) { 12 | 13 | int[] numbers = new int[n + 1]; 14 | boolean[] primeNos = new boolean[n + 1]; 15 | 16 | for (int i = 0; i < n; i++) { 17 | numbers[i] = i; 18 | primeNos[i] = true; 19 | } 20 | 21 | for (int i = 2; i < n; i++) { 22 | if (!primeNos[i]) { 23 | continue; 24 | } 25 | for (int j = i * 2; j < n; j = j + i) { 26 | primeNos[j] = false; 27 | } 28 | } 29 | 30 | for (int i = 2; i < n; i++) { 31 | if (primeNos[i]) { 32 | System.out.println(numbers[i]); 33 | } 34 | } 35 | 36 | } 37 | 38 | public static void main(String args[]) { 39 | 40 | primeNumberPrinter(20); 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/com/osfg/questions/BSTVerifier.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | import com.osfg.models.BTreeNode; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Question : Verify if a binary tree is a Binary Search Tree (BST) 10 | */ 11 | public class BSTVerifier { 12 | 13 | 14 | public static boolean isBST(BTreeNode root) { 15 | 16 | if(root == null) { 17 | return true; 18 | } 19 | 20 | BTreeNode leftNode = root.getLeft(); 21 | BTreeNode rightNode = root.getRight(); 22 | 23 | if((leftNode!=null && root.getData() < leftNode.getData()) || (rightNode!=null && root.getData() > rightNode.getData())){ 24 | return false; 25 | } 26 | else { 27 | return isBST(leftNode) && isBST(rightNode); 28 | } 29 | 30 | } 31 | 32 | public static void main(String args[]) { 33 | 34 | System.out.println(isBST(DSFactory.getBST())); 35 | System.out.println(isBST(DSFactory.getBTree())); 36 | 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/com/osfg/models/BlockingQueue.java: -------------------------------------------------------------------------------- 1 | package com.osfg.models; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Model class for blocking queue 10 | */ 11 | public class BlockingQueue { 12 | 13 | private Queue bQueue = new LinkedList(); 14 | private int maxQueueSize; 15 | 16 | public BlockingQueue(int maxQueueSize) { 17 | this.maxQueueSize = maxQueueSize; 18 | } 19 | 20 | public synchronized void enqueue(E e) throws InterruptedException { 21 | 22 | while(bQueue.size() == maxQueueSize) { 23 | wait(); 24 | } 25 | bQueue.add(e); 26 | // notify if any thread is waiting to dequeue as data is now available 27 | notifyAll(); 28 | } 29 | 30 | public synchronized E dequeue() throws InterruptedException{ 31 | 32 | while(bQueue.size() == 0) { 33 | wait(); 34 | } 35 | E e = bQueue.remove(); 36 | notifyAll(); 37 | return e; 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/osfg/utils/BaseNEncodeDecode.java: -------------------------------------------------------------------------------- 1 | package com.osfg.utils; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Class to convert an integer to base N representation and vice versa 10 | */ 11 | public class BaseNEncodeDecode { 12 | 13 | public static List encode(int number, int baseN) { 14 | List baseNEncodedList = new ArrayList<>(); 15 | 16 | while(number > 0) { 17 | baseNEncodedList.add(0, number%baseN); 18 | number = number / baseN; 19 | } 20 | return baseNEncodedList; 21 | } 22 | 23 | public static int decode(List baseNEncodedList, int baseN) { 24 | int number = 0; 25 | for(int data : baseNEncodedList) { 26 | number = number * baseN + data; 27 | } 28 | return number; 29 | } 30 | 31 | //tests to verify encode decode 32 | public static void main(String args[]) { 33 | 34 | List base2Data = encode(42,2); 35 | System.out.println(base2Data); 36 | System.out.println(decode(base2Data,2)); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/com/osfg/questions/ParenthesisCombinationPrinter.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Print all valid combinations of groups of parenthesis 7 | * Eg for {}{} - possible combinations '{{}}','{}{}' 8 | */ 9 | public class ParenthesisCombinationPrinter { 10 | 11 | public static void printParenthesis(int leftRemain, int rightRemain, String currCombination) { 12 | 13 | if(leftRemain == 0 && rightRemain == 0) { 14 | System.out.println(currCombination); 15 | return; 16 | } 17 | 18 | if(leftRemain > 0) { 19 | printParenthesis(leftRemain-1, rightRemain, currCombination + "{"); 20 | 21 | if(leftRemain < rightRemain) { 22 | printParenthesis(leftRemain, rightRemain-1, currCombination + "}"); 23 | } 24 | 25 | } 26 | else { 27 | //leftRemain == 0 28 | printParenthesis(leftRemain, rightRemain-1, currCombination + "}"); 29 | 30 | } 31 | 32 | } 33 | 34 | //main method - tests 35 | public static void main(String args[]) { 36 | printParenthesis(3,3,""); 37 | System.out.println("-----------"); 38 | printParenthesis(2,2,""); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/com/osfg/questions/ShortURLGenerator.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | 4 | /** 5 | * 6 | * @author athakur 7 | * Question : To design a tiny URL or URL shortener 8 | * 9 | * Solution : One way to achieve this would be hashing. But there may be collision and two short 10 | * string may map to same long one. Better was is to use integer id stored in DB 11 | */ 12 | public class ShortURLGenerator { 13 | 14 | private static final String CHAR_MAP = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 15 | private static final int BASE = CHAR_MAP.length(); 16 | 17 | private static String idToShortURL(int id) { 18 | 19 | StringBuilder sb = new StringBuilder(); 20 | 21 | while(id > 0) { 22 | sb.append(CHAR_MAP.charAt(id%BASE)); 23 | id = id / BASE; 24 | } 25 | return sb.reverse().toString(); 26 | } 27 | 28 | 29 | private static int shortURLtoID(String shortUrl) { 30 | int no = 0; 31 | for(char c : shortUrl.toCharArray()) { 32 | no = no * BASE + CHAR_MAP.indexOf(c); 33 | } 34 | return no; 35 | } 36 | 37 | //tests - main method 38 | public static void main(String args[]) { 39 | 40 | String shortUrl = idToShortURL(1234); 41 | System.out.println("shortUrl : " + shortUrl); 42 | System.out.println("ID : " + shortURLtoID(shortUrl)); 43 | 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/com/osfg/questions/SquareRoot.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Get Square Root of a given number 7 | */ 8 | public class SquareRoot { 9 | 10 | /** 11 | * 12 | * @param data 13 | * @return square root of data 14 | */ 15 | public static double sqrt(double data) { 16 | 17 | if(data < 0) { 18 | return -1; 19 | } 20 | else if(data == 0 || data ==1) { 21 | return data; 22 | } 23 | 24 | double start = 0; 25 | double end = data; 26 | double precision = 0.00001; 27 | 28 | if(data <1 ) { 29 | end = 1; 30 | } 31 | 32 | while(end - start > precision) { 33 | double mid = (start + end) / 2; 34 | double sqr = Math.pow(mid, 2); 35 | if(sqr == data) { 36 | return mid; 37 | } 38 | else if (sqr < data) { 39 | start = mid; 40 | } 41 | else { 42 | end = mid; 43 | } 44 | } 45 | 46 | return (start+end)/2; 47 | 48 | } 49 | 50 | //Main method - tests 51 | public static void main(String args[]) { 52 | 53 | System.out.println("Square root of 9 : " + sqrt(9)); 54 | System.out.println("Square root of 100 : " + sqrt(100)); 55 | System.out.println("Square root of 81 : " + sqrt(81)); 56 | System.out.println("Square root of 24 : " + sqrt(24)); 57 | System.out.println("Square root of 1 : " + sqrt(1)); 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/com/osfg/questions/LCAFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | import com.osfg.models.BTreeNode; 5 | 6 | 7 | /** 8 | * 9 | * @author athakur 10 | * Question : Find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree 11 | */ 12 | public class LCAFinder { 13 | 14 | 15 | // main method - tests 16 | public static void main(String args[]) { 17 | BTreeNode root = DSFactory.getBTree(); 18 | System.out.println("LCA of 4 and 5 : " + findLCA(root, 4,5)); 19 | System.out.println("LCA of 5 and 6 : " + findLCA(root, 5,6)); 20 | System.out.println("LCA of 6 and 7 : " + findLCA(root, 6,7)); 21 | System.out.println("LCA of 2 and 5 : " + findLCA(root, 2,5)); 22 | } 23 | 24 | 25 | /** 26 | * 27 | * @param root 28 | * @param n1 29 | * @param n2 30 | * @return LCA of n1 and n2 31 | */ 32 | public static BTreeNode findLCA(BTreeNode root, int n1, int n2) { 33 | 34 | if (root == null) { 35 | return null; 36 | } 37 | 38 | if (root.getData() == n1 || root.getData() == n2) { 39 | return root; 40 | } 41 | 42 | BTreeNode leftNode = findLCA(root.getLeft(), n1, n2); 43 | BTreeNode rightNode = findLCA(root.getRight(), n1, n2); 44 | 45 | if (leftNode != null && rightNode != null) { 46 | return root; 47 | } 48 | 49 | return leftNode != null ? leftNode : rightNode; 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/com/osfg/questions/RotatedPalindrome.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * Question : Verify if a string is a rotated palindrome 7 | * 8 | * Rotated palindrome is a plaindrome which is rotated at some index so that some chars from end 9 | * are moved to the start. Eg abba is a plaindrome aabb is a rotated palindrome 10 | */ 11 | public class RotatedPalindrome { 12 | 13 | /** 14 | * 15 | * @param str 16 | * @return is str is palindrome string or not 17 | */ 18 | public static boolean isPalindrome(String str) { 19 | 20 | for(int i=0;i originalStack = new Stack<>(); 13 | private Stack reverseStack = new Stack<>(); 14 | 15 | public void enQueue(int data) { 16 | originalStack.push(data); 17 | } 18 | 19 | public int deQueue() { 20 | 21 | // reverse stack to simulate Queue - FIFO 22 | // Do this only when reverseStack is empty 23 | if(reverseStack.isEmpty()) { 24 | while(!originalStack.isEmpty()) { 25 | reverseStack.push(originalStack.pop()); 26 | } 27 | } 28 | 29 | if(reverseStack.isEmpty()) { 30 | throw new RuntimeException("No data enqueued"); 31 | } 32 | 33 | 34 | return reverseStack.pop(); 35 | } 36 | 37 | public static void main(String args[]) { 38 | 39 | QueueWithStack queue = new QueueWithStack(); 40 | queue.enQueue(4); 41 | queue.enQueue(10); 42 | queue.enQueue(2); 43 | queue.enQueue(5); 44 | System.out.println(queue.deQueue()); 45 | queue.enQueue(1); 46 | queue.enQueue(6); 47 | System.out.println(queue.deQueue()); 48 | System.out.println(queue.deQueue()); 49 | System.out.println(queue.deQueue()); 50 | System.out.println(queue.deQueue()); 51 | System.out.println(queue.deQueue()); 52 | System.out.println(queue.deQueue()); 53 | 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/com/osfg/questions/BTreeTraversal.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | import com.osfg.utils.BTreePrinter; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Question : Print BTree in pre order, post order, in order and level order 10 | */ 11 | public class BTreeTraversal { 12 | 13 | 14 | //main method - test 15 | public static void main(String args[]) { 16 | 17 | System.out.println("Pre order : "); 18 | BTreePrinter.printPreOrderTraversal(DSFactory.getBTree()); 19 | System.out.println("---------------"); 20 | 21 | System.out.println("Post order : "); 22 | BTreePrinter.printPostOrderTraversal(DSFactory.getBTree()); 23 | System.out.println("---------------"); 24 | 25 | System.out.println("In order : "); 26 | BTreePrinter.printInOrderTraversal(DSFactory.getBTree()); 27 | System.out.println("---------------"); 28 | 29 | System.out.println("Level order : "); 30 | BTreePrinter.printLevelOrderTraversal(DSFactory.getBTree()); 31 | System.out.println("---------------"); 32 | 33 | System.out.println("Height : "); 34 | System.out.println(BTreePrinter.getHeight(DSFactory.getBTree())); 35 | System.out.println("---------------"); 36 | 37 | System.out.println("Spiral order recurssive : "); 38 | BTreePrinter.printSpiralRecurssive(DSFactory.getBTree()); 39 | System.out.println("---------------"); 40 | 41 | System.out.println("Spiral order interative : "); 42 | BTreePrinter.printSpiralIterative(DSFactory.getBTree()); 43 | System.out.println("---------------"); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/com/osfg/questions/IndexInSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Question : Find the 1st index of a given number in a sorted array allowing duplicates 9 | */ 10 | public class IndexInSortedArray { 11 | 12 | /** 13 | * You can use a linear search to to return first index 14 | * This method uses a variation of binary search. Complexity is O(Log(N)) 15 | * @param array original array to search data in 16 | * @param data to be searched 17 | * @param start index 18 | * @param end index 19 | * @return index at which data occurs first 20 | */ 21 | public static int getIndex(int[] array, int data, int start, int end) { 22 | 23 | if(start > end) { 24 | return -1; 25 | } 26 | int mid = start + (end-start)/2; 27 | 28 | if(array[mid] == data) { 29 | if(array[mid-1] != data){ 30 | return mid; 31 | } 32 | else { 33 | return getIndex(array, data, start, mid-1); 34 | } 35 | 36 | } 37 | else { 38 | if(array[mid] < data) { 39 | return getIndex(array, data, mid+1,end); 40 | } 41 | else { 42 | //array[mid] > data 43 | return getIndex(array, data, start,mid-1); 44 | } 45 | } 46 | 47 | } 48 | 49 | //main methods - tests 50 | public static void main(String args[]) { 51 | int[]sortedArray = DSFactory.getArray(true); 52 | System.out.println(getIndex(sortedArray,15,0,sortedArray.length-1)); 53 | System.out.println(getIndex(sortedArray,3,0,sortedArray.length-1)); 54 | System.out.println(getIndex(sortedArray,4,0,sortedArray.length-1)); 55 | } 56 | 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/com/osfg/questions/StairwayClimbWaysFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * 7 | * Question : There are N stairs that you need to take to the kth floor. 8 | * You can either climb 1 stair at a time or 2 stair at a time. 9 | * In how many ways you can cover N stairs. 10 | */ 11 | public class StairwayClimbWaysFinder { 12 | 13 | public static int findWays(int stairsClimbed, int totalStairs) { 14 | 15 | if(stairsClimbed == totalStairs) { 16 | return 1; 17 | } 18 | 19 | if((totalStairs - stairsClimbed) >= 2) { 20 | return findWays(stairsClimbed + 2, totalStairs) + findWays(stairsClimbed + 1, totalStairs); 21 | } 22 | else if ((totalStairs - stairsClimbed) == 1) { 23 | return findWays(stairsClimbed + 1, totalStairs); 24 | } 25 | return 0; 26 | } 27 | 28 | /** 29 | * Secondary approach - it is infact a fibanacci series 30 | * f(n) = f(n-1) + f(n-2), n>2, f(1) = 1 and f(2) = 2 31 | * @param totalStairs 32 | * @return 33 | */ 34 | public static int findWays(int totalStairs) { 35 | 36 | if(totalStairs == 1) { 37 | return 1; 38 | } 39 | else if (totalStairs == 2) { 40 | return 2; 41 | } 42 | else { 43 | return ((findWays(totalStairs-1) ) + (findWays(totalStairs-2) )); 44 | } 45 | 46 | } 47 | 48 | //tests - main method 49 | public static void main(String args[]) { 50 | 51 | System.out.println("No Of ways for 2 stairs : " + findWays(0, 2)); 52 | System.out.println("No Of ways for 4 stairs : " + findWays(0, 4)); 53 | 54 | System.out.println("No Of ways for 2 stairs : " + findWays(2)); 55 | System.out.println("No Of ways for 4 stairs : " + findWays(4)); 56 | 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/com/osfg/questions/LinkedListMidFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | import com.osfg.models.ListNode; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Question : Find the middle element of a linked list 10 | * Extended Question : Find the (1/n)th element from the end of a linked list 11 | */ 12 | public class LinkedListMidFinder { 13 | 14 | 15 | /** 16 | * 17 | * @param root 18 | * @return center of the list 19 | */ 20 | public static ListNode findLLMid(ListNode root) { 21 | 22 | if(root == null) { 23 | return null; 24 | } 25 | 26 | ListNode current = root; 27 | ListNode middle = root; 28 | 29 | while(current.getNext() != null && current.getNext().getNext()!=null) { 30 | middle = middle.getNext(); 31 | current = current.getNext().getNext(); 32 | } 33 | 34 | return middle; 35 | } 36 | 37 | /** 38 | * 39 | * @param root 40 | * @param n 41 | * @return (1/n)th node 42 | */ 43 | public static ListNode findoneByNth(ListNode root, int n) { 44 | 45 | if(root == null) { 46 | return null; 47 | } 48 | 49 | ListNode current = root; 50 | ListNode middle = root; 51 | 52 | // 0 1 2 3 4 53 | 54 | for(int i=1;current != null; i++,current=current.getNext()) { 55 | if(i%n == 0) { 56 | middle = middle.getNext(); 57 | } 58 | } 59 | 60 | return middle; 61 | } 62 | 63 | public static void main(String args[]) { 64 | System.out.println(findLLMid(DSFactory.getNumericLinkedList()).getValue()); 65 | System.out.println(findoneByNth(DSFactory.getNumericLinkedList(),2).getValue()); 66 | System.out.println(findoneByNth(DSFactory.getNumericLinkedList(),6).getValue()); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/com/osfg/questions/ThreadPoolManager.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.models.BlockingQueue; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Simple thread pool implementation in Java using BlockingQueue 9 | */ 10 | public class ThreadPoolManager { 11 | 12 | //assuming size of queue to be 10 13 | private final BlockingQueue threadPoolQueue = new BlockingQueue(10);; 14 | private final int MAX_THREAD_POOL_SIZE; 15 | 16 | public ThreadPoolManager(int maxThreadPoolSize) { 17 | MAX_THREAD_POOL_SIZE = maxThreadPoolSize; 18 | initThreads(); 19 | } 20 | 21 | 22 | private void initThreads() { 23 | for(int i=0; i< MAX_THREAD_POOL_SIZE;i++) { 24 | final int threadNo = i; 25 | new Thread(new Runnable() { 26 | 27 | @Override 28 | public void run() { 29 | while(true) { 30 | try { 31 | threadPoolQueue.dequeue().run(); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } 37 | }).start(); 38 | } 39 | } 40 | 41 | public void enqueueTask(Runnable runnable) throws InterruptedException { 42 | threadPoolQueue.enqueue(runnable); 43 | } 44 | 45 | public static void main(String args[]) throws InterruptedException { 46 | 47 | ThreadPoolManager tmManager = new ThreadPoolManager(10); 48 | tmManager.enqueueTask(new Runnable() { 49 | 50 | @Override 51 | public void run() { 52 | System.out.println("Sumitting task A"); 53 | System.out.println("Completing task A"); 54 | 55 | } 56 | }); 57 | tmManager.enqueueTask(new Runnable() { 58 | 59 | @Override 60 | public void run() { 61 | System.out.println("Sumitting task B"); 62 | System.out.println("Completing task B"); 63 | 64 | } 65 | }); 66 | 67 | 68 | } 69 | 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/com/osfg/questions/SortedArraysMerger.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Question : There are two sorted arrays. 9 | * First one is of size m+n containing only m elements. 10 | * Another one is of size n and contains n elements. 11 | * Merge these two arrays into the first array of size m+n such that the output is sorted. 12 | */ 13 | public class SortedArraysMerger { 14 | 15 | 16 | /** 17 | * Move all elements to the end in m+n array 18 | * Assuming empty gaps have -1 and original array does not have -1 19 | * @param mPlusNArray 20 | */ 21 | public static void setup(int[] mPlusNArray) { 22 | 23 | int substitutionPointer = mPlusNArray.length - 1; 24 | for(int i=mPlusNArray.length - 1; i>=0; i--) { 25 | if(mPlusNArray[i] != -1) { 26 | mPlusNArray[substitutionPointer] = mPlusNArray[i]; 27 | substitutionPointer--; 28 | } 29 | } 30 | } 31 | 32 | public static void merge(int[] mPlusNArray, int[] nArray) { 33 | 34 | int mPlusNArrayDataStart = nArray.length; 35 | int nArrayStart = 0; 36 | int mPlusNArrayStart = 0; 37 | 38 | while(mPlusNArrayStart < mPlusNArray.length) { 39 | if(mPlusNArrayDataStart < mPlusNArray.length && mPlusNArray[mPlusNArrayDataStart] < nArray[nArrayStart]) { 40 | mPlusNArray[mPlusNArrayStart] = mPlusNArray[mPlusNArrayDataStart]; 41 | mPlusNArrayDataStart++; 42 | } 43 | else { 44 | mPlusNArray[mPlusNArrayStart] = nArray[nArrayStart]; 45 | nArrayStart++; 46 | } 47 | mPlusNArrayStart++; 48 | } 49 | } 50 | 51 | public static void main(String args[]) { 52 | 53 | int[] mPlusNArray = new int[]{2, -1, 7, -1, -1, 10, -1}; 54 | int[] nArray = new int[]{5, 8, 12, 14}; 55 | setup(mPlusNArray); 56 | System.out.println(Arrays.toString(mPlusNArray)); 57 | merge(mPlusNArray, nArray); 58 | System.out.println(Arrays.toString(mPlusNArray)); 59 | 60 | } 61 | 62 | 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/com/osfg/questions/PairsWithSumFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Question : Print all pairs in a sorted array that sum to M 9 | */ 10 | public class PairsWithSumFinder { 11 | 12 | 13 | /** 14 | * This is a recurssive approach but similar to two for loops one inside another 15 | * Complexity is O(N^2) which is not good. 16 | * We are not using the fact that the array is sorted 17 | * See the other method with alternate solution. 18 | * @param array 19 | * @param start 20 | * @param sum 21 | */ 22 | public static void printPairs(int[] array, int start, int sum) { 23 | 24 | if(start >= array.length-1) { 25 | return; 26 | } 27 | 28 | for(int i=start+1; i<=array.length-1;i++) { 29 | if(array[start] + array[i] == sum) { 30 | System.out.println("(" + array[start] + "," + array[i] + ")"); 31 | } 32 | } 33 | printPairs(array, start+1, sum); 34 | } 35 | 36 | /** 37 | * This is a better algorithm and cosiders the fact that the array is sorted 38 | * Complexity of this is O(N) 39 | * However note that this also does not print duplicates 40 | * Just two numbers that sum upto M 41 | * @param array 42 | * @param sum 43 | */ 44 | public static void printPairs(int[] array, int sum) { 45 | int leftIndex = 0; 46 | int rightIndex = array.length - 1; 47 | 48 | while(leftIndex < rightIndex) { 49 | int tempSum = array[leftIndex] + array[rightIndex]; 50 | if(tempSum == sum) { 51 | System.out.println("(" + array[leftIndex] + "," + array[rightIndex] + ")"); 52 | leftIndex++; 53 | rightIndex--; 54 | } 55 | else if (tempSum < 12) { 56 | leftIndex++; 57 | } 58 | else { 59 | //tempSum > 12 60 | rightIndex--; 61 | } 62 | } 63 | 64 | 65 | } 66 | 67 | public static void main(String args[]) { 68 | 69 | int[] array = DSFactory.getArray(true); 70 | printPairs(array, 0, 12); 71 | System.out.println("----------------"); 72 | printPairs(array, 12); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/com/osfg/questions/LinkedListReversal.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | import com.osfg.models.ListNode; 5 | 6 | /** 7 | * @author athakur 8 | * 9 | * Question : Reverse a Linked List 10 | */ 11 | public class LinkedListReversal { 12 | 13 | /** 14 | * Iterative approach 15 | * @param originalList 16 | * @return reversed linked list 17 | */ 18 | public static ListNode getReverseLinkedListIterative(ListNode originalList) { 19 | 20 | if(originalList == null || originalList.getNext() == null) { 21 | return originalList; 22 | } 23 | 24 | ListNode currNode = originalList; 25 | ListNode nextNode = currNode.getNext(); 26 | currNode.setNext(null); 27 | 28 | 29 | while(nextNode != null) { 30 | ListNode tempNode = nextNode.getNext(); 31 | nextNode.setNext(currNode); 32 | currNode = nextNode; 33 | nextNode = tempNode; 34 | } 35 | 36 | return currNode; 37 | 38 | } 39 | 40 | /** 41 | * Recursive approach 42 | * @param originalList 43 | * @return reversed linked list 44 | */ 45 | public static ListNode getReverseLinkedListRecursive(ListNode originalList) { 46 | 47 | if(originalList == null || originalList.getNext() == null) { 48 | return originalList; 49 | } 50 | 51 | ListNode remainingRevers = getReverseLinkedListRecursive(originalList.getNext()); 52 | ListNode curr = remainingRevers; 53 | 54 | while(curr.getNext() != null) { 55 | curr = curr.getNext(); 56 | } 57 | 58 | curr.setNext(originalList); 59 | originalList.setNext(null); 60 | return remainingRevers; 61 | 62 | } 63 | 64 | //Main method - tests 65 | public static void main(String args[]) { 66 | 67 | ListNode linkedList = DSFactory.getNumericLinkedList(); 68 | System.out.println("Original Linked List : " + linkedList); 69 | ListNode iterativeReverse = getReverseLinkedListIterative(linkedList); 70 | System.out.println("Iterative Revered Linked List : " + iterativeReverse); 71 | ListNode recursiveReverse = getReverseLinkedListRecursive(iterativeReverse); 72 | System.out.println("Recursive Linked List : " + recursiveReverse); 73 | } 74 | 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/com/osfg/questions/StringExpressionEvaluator.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | import java.util.Stack; 6 | 7 | /** 8 | * 9 | * @author athakur 10 | * Question : Write a Pseudo code to parse a String and and evaluate expressions like: 11 | * (+ 5 4 6) 12 | * (* 2 (- 7 3) 4) 13 | * (+ 3 (* 5 2) (- 6 3)) 14 | * Example: 15 | * (+ 5 4 6) -> (5 + 4 + 6) = 15 16 | * (* 2 (- 7 3) 4) -> (2 * (7 - 3) * 4) = 32 17 | * (+ 3 (* 5 2) (- 6 3)) -> (3 + (5 * 2) + (6 - 3)) = 16 18 | */ 19 | public class StringExpressionEvaluator { 20 | 21 | private static Stack dataStack = new Stack<>(); 22 | private static Stack operatorStack = new Stack<>(); 23 | 24 | 25 | private static List operands = Arrays.asList('+','-','*','/'); 26 | 27 | 28 | public static int parse(String input) { 29 | 30 | 31 | for(Character ch : input.toCharArray()) { 32 | if(isOperator(ch)) { 33 | operatorStack.push(ch); 34 | } 35 | else if(ch.equals(')')) { 36 | Integer value = Integer.parseInt(dataStack.pop().toString()); 37 | while(!dataStack.peek().equalsIgnoreCase("(")) { 38 | value = operate(Integer.parseInt(dataStack.pop().toString()), value, operatorStack.peek()); 39 | } 40 | dataStack.pop(); 41 | operatorStack.pop(); 42 | dataStack.push(value.toString()); 43 | } 44 | else { 45 | dataStack.push(ch.toString()); 46 | } 47 | } 48 | 49 | return Integer.parseInt(dataStack.pop().toString()); 50 | 51 | 52 | } 53 | 54 | private static boolean isOperator(Character operator) { 55 | if(operands.contains(operator)) return true; 56 | return false; 57 | } 58 | 59 | 60 | 61 | public static int operate(int left, int right, Character operation) { 62 | switch(operation) { 63 | case '+' : return left + right; 64 | case '-' : return left - right; 65 | case '*' : return left * right; 66 | case '/' : return left / right; 67 | default : throw new RuntimeException("Operation not supported"); 68 | } 69 | 70 | } 71 | 72 | //main method - tests 73 | public static void main(String args[]) { 74 | 75 | 76 | String test1 = "(+546)"; 77 | String test2 = "(*2(-73)4)"; 78 | String test3 = "(+3(*52)(-63))"; 79 | System.out.println(parse(test3)); 80 | } 81 | 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/com/osfg/questions/IntegralPointsOnCirle.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | /** 4 | * 5 | * @author athakur 6 | * 7 | * Give a circle with center (0,0) and a radius r (integer > 0), 8 | * find the number of points (x,y) on the circumference such that x and y both are integers 9 | * Hint : (x,y) lies on the circumference if X^2 + Y^2 = r^2 10 | * 11 | */ 12 | public class IntegralPointsOnCirle { 13 | 14 | /** 15 | * O(N^2) solution 16 | * @param radius 17 | * @return 18 | */ 19 | public static int getIntegralPointsOnCirle(int radius) 20 | { 21 | double rSqr = Math.pow(radius, 2); 22 | int count = 0; 23 | for (int x = 0; x<= radius; x++) 24 | { 25 | for(int y=0; y<=radius; y++) 26 | { 27 | double xSqr = Math.pow(x, 2); 28 | double ySqr = Math.pow(y, 2); 29 | if(xSqr + ySqr == rSqr) 30 | { 31 | count++; 32 | } 33 | 34 | } 35 | } 36 | //4 quadrants 37 | count = count * 4; 38 | //each x and y cordinates will be counted twice 39 | count = count -4; 40 | return count; 41 | } 42 | 43 | /** 44 | * Linear solution 45 | * @param radius 46 | * @return 47 | */ 48 | public static int getIntegralPointsOnCirleOptimized(int radius) 49 | { 50 | double rSqr = Math.pow(radius, 2); 51 | int count = 0; 52 | int x = 1; 53 | int y = radius -1; 54 | 55 | while(x <= y) 56 | { 57 | double xSqr = Math.pow(x, 2); 58 | double ySqr = Math.pow(y, 2); 59 | double sum = xSqr + ySqr; 60 | if(sum == rSqr) 61 | { 62 | count++; 63 | x++; 64 | y--; 65 | } 66 | else if (sum < rSqr) 67 | { 68 | x++; 69 | } 70 | else { 71 | //sum > rSqr 72 | y--; 73 | } 74 | } 75 | 76 | //1 quadrant 77 | count = count * 2; 78 | //4 quardranrts 79 | count = count * 4; 80 | //4 axis coordinates _x, -x, y, -y 81 | count = count + 4; 82 | return count; 83 | } 84 | 85 | 86 | 87 | 88 | public static void main(String args[]) 89 | { 90 | /** 91 | * radius = 5 92 | * points - (3,4) = 1 93 | * same quedrant - *2 : (4,3) : = 2 94 | * 4 quadrant : *4 = 8 95 | * (5,0) (0,5) (-5,0) (0,-5) : + 4 = 12 96 | */ 97 | System.out.println(getIntegralPointsOnCirle(5)); 98 | System.out.println(getIntegralPointsOnCirleOptimized(5)); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/com/osfg/questions/SortedOccurenceCounter.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import com.osfg.factory.DSFactory; 4 | 5 | /** 6 | * 7 | * @author athakur 8 | * Question : Count Occurrence of a given number in sorted array 9 | */ 10 | public abstract class SortedOccurenceCounter { 11 | 12 | /** 13 | * Linear approach. Complexity O(N) 14 | * @param array 15 | * @param data 16 | * @return 17 | */ 18 | public static int getOccurenceLinear(int[] array, int data) { 19 | if(array == null) 20 | return 0; 21 | 22 | int occurence = 0; 23 | for(int i=0;i endIndex) { 40 | return 0; 41 | } 42 | 43 | if(array[startIndex] > data || array[endIndex] < data) { 44 | return 0; 45 | } 46 | 47 | if(array[startIndex] == data && array[endIndex] == data) { 48 | return endIndex - startIndex + 1; 49 | } 50 | 51 | int mid = startIndex + (endIndex - startIndex)/2; 52 | 53 | if(array[mid] > data) { 54 | return getOccurenceLogarithmic(array, data, startIndex, mid - 1); 55 | } 56 | else if(array[mid] < data) { 57 | return getOccurenceLogarithmic(array, data, mid + 1, endIndex); 58 | } 59 | else {//array[mid] == data 60 | return 1 + getOccurenceLogarithmic(array, data, startIndex, mid - 1) + getOccurenceLogarithmic(array, data, mid + 1, endIndex); 61 | } 62 | 63 | 64 | } 65 | 66 | 67 | //main method - tests 68 | public static void main(String args[]) { 69 | 70 | int[] sortedArray = DSFactory.getArray(true); 71 | System.out.println("Occurence of 15(Linear) : " + getOccurenceLinear(sortedArray, 15)); 72 | System.out.println("Occurence of 3(Linear) : " + getOccurenceLinear(sortedArray, 3)); 73 | System.out.println("Occurence of 0(Linear) : " + getOccurenceLinear(sortedArray, 0)); 74 | 75 | System.out.println("Occurence of 15(Log) : " + getOccurenceLogarithmic(sortedArray, 15, 0, sortedArray.length - 1)); 76 | System.out.println("Occurence of 3(Log) : " + getOccurenceLogarithmic(sortedArray, 3, 0, sortedArray.length - 1)); 77 | System.out.println("Occurence of 0(Log) : " + getOccurenceLogarithmic(sortedArray, 0, 0, sortedArray.length - 1)); 78 | 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | `![Data Structures](https://3.bp.blogspot.com/-aQNOpvozWPY/VxDwH9I7rkI/AAAAAAAALBw/HV8HnUoejgoeg1GeVbF9lsbXpIKF6Q7mwCLcB/s1600/data-structures.png) 2 | 3 | # DataStructures Interview Questions 4 | 5 | 6 | 7 | 1. Reverse a Linked List - LinkedListReversal.java 8 | 2. Get Square Root of a given number - SquareRoot.java 9 | 3. Count Occurance of a given number in sorted array - SortedOccurenceCounter.java 10 | 4. Finding GCD of two numbers - GCDFinder.java 11 | 5. Print all valid combinations of groups of parenthesis - ParenthesisCombinationPrinter.java 12 | 6. Convert a double number to rational - DoubleToRational.java 13 | 7. String Permutation - PrintAllStringPermutations.java 14 | 8. Find the 1st index of a given number in a sorted array allowing duplicates - IndexInSortedArray.java 15 | 9. Verify if a binary tree is a Binary Search Tree (BST) - BSTVerifier.java 16 | 10. Print BTree in pre order, post order, in order and level order - BTreeTraversal.java 17 | 11. Verify if a string is a rotated palindrome - RotatedPalindrome.java 18 | 12. Print all pairs in a sorted array that sum to M - PairsWithSumFinder.java 19 | 13. Use stack (LIFO) to simulate queue (FIFO) - QueueWithStack.java 20 | 14. There are two sorted arrays. First one is of size m+n containing only m elements. Another one is of size n and contains n elements. Merge these two arrays into the first array of size m+n such that the output is sorted - SortedArraysMerger.java 21 | 15. Verify if strings are rotations of each other or not - RotatedString.java 22 | 16. To design a tiny URL or URL shortener - ShortURLGenerator.java 23 | 17. Write a Pseudo code to parse a String and and evaluate expressions - StringExpressionEvaluator.java 24 | 18. Code to find number of ways to cover n stairs when you can take 1 or 2 step at a time - StairwayClimbWaysFinder.java 25 | 19. Generic implementation of BlockingQueue - BlockingQueue.java 26 | 20. Simple thread pool implementation in Java - ThreadPoolManager.java 27 | 21. Merge an array of size n into another array of size m+n - MPlusNArrayMerge.java 28 | 22. Given a number n, print all primes smaller than or equal to n - PrimeNumberPrinter.java 29 | 23. Print BTree is spiral order - BTreeTraversal.java 30 | 24. Given an array of positive integers, arrange the numbers to form the largest possible number - FormLargestNumber.java 31 | 25. Give a circle with center (0,0) and a radius r (integer > 0), find the number of points (x,y) on the circumference such that x and y both are integers - IntegralPointsOnCirle.java 32 | 26. Find if a given Singly Linked list is a palindrome or not - LinkedListPalindromeFinder.java 33 | 27. Print even and odd numbers in order using two threads - PrintOddEven.java 34 | 28. Find the middle element of a linked list - LinkedListMidFinder.java 35 | 29. Find the Lowest Common Ancestor (LCA) of two nodes in a Binary Tree - LCAFinder.java 36 | -------------------------------------------------------------------------------- /src/com/osfg/questions/PrintOddEven.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.concurrent.ExecutionException; 4 | import java.util.concurrent.Semaphore; 5 | 6 | /** 7 | * 8 | * @author athakur 9 | * Question: Print even and odd numbers using two threads. 10 | * Print the numbers in order, while one thread only prints the even numbers and the other thread only prints the odd numbers. 11 | */ 12 | public class PrintOddEven { 13 | 14 | public static void main(String args[]) throws InterruptedException, ExecutionException { 15 | 16 | System.out.println("****With Sempahores*********"); 17 | withSemaphores(); 18 | Thread.sleep(5000); 19 | System.out.println("****With Watch and Notify*********"); 20 | withWatchNotify(); 21 | 22 | } 23 | 24 | public static void withWatchNotify() { 25 | 26 | Object oddLock = new Object(); 27 | Object evenLock = new Object(); 28 | 29 | Runnable printOdd = () -> { 30 | for (int i = 1; i < 10; i = i + 2) { 31 | if (i != 1) { 32 | synchronized (oddLock) { 33 | try { 34 | oddLock.wait(); 35 | } catch (InterruptedException e) { 36 | // TODO Auto-generated catch block 37 | e.printStackTrace(); 38 | } 39 | } 40 | } 41 | try { 42 | Thread.sleep(500); 43 | } catch (InterruptedException e) { 44 | // TODO Auto-generated catch block 45 | e.printStackTrace(); 46 | } 47 | System.out.println(i); 48 | synchronized (evenLock) { 49 | evenLock.notify(); 50 | } 51 | } 52 | }; 53 | 54 | Runnable printEven = () -> { 55 | for (int i = 2; i < 10; i = i + 2) { 56 | synchronized (evenLock) { 57 | try { 58 | evenLock.wait(); 59 | } catch (InterruptedException e) { 60 | // TODO Auto-generated catch block 61 | e.printStackTrace(); 62 | } 63 | } 64 | System.out.println(i); 65 | synchronized (oddLock) { 66 | oddLock.notify(); 67 | } 68 | } 69 | }; 70 | 71 | new Thread(printOdd).start(); 72 | new Thread(printEven).start(); 73 | } 74 | 75 | public static void withSemaphores() throws InterruptedException, ExecutionException { 76 | 77 | Semaphore oddLock = new Semaphore(1); 78 | Semaphore evenLock = new Semaphore(0); 79 | 80 | Runnable printOdd = () -> { 81 | for (int i = 1; i < 10; i = i + 2) { 82 | try { 83 | oddLock.acquire(); 84 | } catch (Exception e) { 85 | // TODO Auto-generated catch block 86 | e.printStackTrace(); 87 | } 88 | ; 89 | System.out.println(i); 90 | evenLock.release(); 91 | } 92 | }; 93 | 94 | Runnable printEven = () -> { 95 | for (int i = 2; i < 10; i = i + 2) { 96 | try { 97 | evenLock.acquire(); 98 | } catch (Exception e) { 99 | // TODO Auto-generated catch block 100 | e.printStackTrace(); 101 | } 102 | System.out.println(i); 103 | oddLock.release(); 104 | 105 | } 106 | }; 107 | 108 | new Thread(printOdd).start(); 109 | new Thread(printEven).start(); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /src/com/osfg/questions/MPlusNArrayMerge.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.Random; 7 | 8 | import com.osfg.factory.DSFactory; 9 | 10 | /** 11 | * 12 | * @author athakur 13 | * 14 | * Question : There are two sorted arrays. First one is of size m+n containing only m elements. 15 | * Another one is of size n and contains n elements. 16 | * Merge these two arrays into the first array of size m+n such that the output is sorted. 17 | */ 18 | public class MPlusNArrayMerge { 19 | 20 | public static void mergeAndSortArrays(int[] mPlusNArray, int[] nArray ) { 21 | 22 | //for simplicity this code assumes all values are non negative and 23 | // array positions which have -1 are considered to be empty - n positions will be like this 24 | 25 | //1. Move m sorted elements to the end of m+n array 26 | 27 | int trackMelements = mPlusNArray.length-1; 28 | for (int i=mPlusNArray.length-1;i>=0;i--) { 29 | if(mPlusNArray[i] != -1) { 30 | mPlusNArray[trackMelements] = mPlusNArray[i]; 31 | trackMelements--; 32 | } 33 | } 34 | 35 | System.out.println("mPlusNArray : " + Arrays.toString(mPlusNArray)); 36 | System.out.println("nArray : " + Arrays.toString(nArray)); 37 | 38 | if(trackMelements != nArray.length-1) { 39 | throw new RuntimeException("mPlusNArray does not have m elements"); 40 | } 41 | 42 | System.out.println("trackMelements : " + trackMelements); 43 | 44 | //1. now sort for parallely 45 | 46 | int mPlusNIndex = trackMelements + 1; //tracks elements of m+n array 47 | int nIndex = 0; //tracks elements of n array 48 | 49 | //compare and start adding from beginning 50 | for(int i = 0;i mPlusNArray.length-1){ 53 | mPlusNArray[i] = nArray[nIndex]; 54 | nIndex++; 55 | } 56 | else if(nIndex > nArray.length-1) { 57 | mPlusNArray[i] = mPlusNArray[mPlusNIndex]; 58 | mPlusNIndex++; 59 | } 60 | else { 61 | if(mPlusNArray[mPlusNIndex] < nArray[nIndex]) { 62 | mPlusNArray[i] = mPlusNArray[mPlusNIndex]; 63 | mPlusNIndex++; 64 | } 65 | else { 66 | mPlusNArray[i] = nArray[nIndex]; 67 | nIndex++; 68 | } 69 | } 70 | 71 | 72 | } 73 | 74 | } 75 | 76 | public static void main(String args[]) { 77 | 78 | int[] mPlusNArray = DSFactory.getArray(true, 10, 100); 79 | int[] nArray = DSFactory.getArray(true, 4, 100); 80 | Random random = new Random(); 81 | 82 | //mPlusNArray should have just 6 elements. So randomly set 4 elements to -1 83 | 84 | int elementsRemaining = 4; 85 | List elementsResetPosition = new ArrayList(); 86 | while(elementsRemaining != 0) { 87 | int indexToReset = random.nextInt(10); 88 | if(!elementsResetPosition.contains(indexToReset)) { 89 | mPlusNArray[indexToReset] = -1; 90 | elementsResetPosition.add(indexToReset); 91 | elementsRemaining--; 92 | } 93 | } 94 | 95 | //primnt arrays 96 | System.out.println("mPlusNArray : " + Arrays.toString(mPlusNArray)); 97 | System.out.println("nArray : " + Arrays.toString(nArray)); 98 | 99 | mergeAndSortArrays(mPlusNArray, nArray); 100 | 101 | System.out.println("Sorted and Megrged Array :" + Arrays.toString(mPlusNArray)); 102 | 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/com/osfg/questions/FormLargestNumber.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.Arrays; 4 | import java.util.Collections; 5 | import java.util.Comparator; 6 | import java.util.List; 7 | import java.util.Objects; 8 | 9 | /* Problem Name is &&& Largest Number &&& PLEASE DO NOT REMOVE THIS LINE. */ 10 | 11 | /* 12 | Instructions to candidate. 13 | 1) Given an array of positive integers, arrange the numbers to form the largest possible number 14 | Input: [1, 8, 7, 9, 4] 15 | Output: 98741 16 | 17 | Input: [604, 6, 112, 10, 5, 61, 9 ] 18 | Output: 9661604511210 19 | 20 | 2) Run this code in the REPL to observe its behavior. The 21 | execution entry point is main(). 22 | 3) Consider adding some additional tests in doTestsPass(). 23 | 4) If time permits, some possible follow-ups. 24 | */ 25 | 26 | public class FormLargestNumber 27 | { 28 | /** 29 | * String findLargestNum() 30 | * Returns the String value of the largest integer that can be formed by arranging the numbers in the input array of positive integers 31 | * Input: [1, 8, 7, 9, 4] 32 | * Output: 98741 33 | */ 34 | public static String findLargestNum(List numArr) { 35 | Collections.sort(numArr, new FormLargestNumber.IntComparator()); 36 | StringBuffer sb = new StringBuffer(); 37 | for(Integer intg : numArr) 38 | { 39 | sb.append(intg.toString()); 40 | } 41 | System.out.println(sb.toString()); 42 | return sb.toString(); 43 | } 44 | 45 | static class IntComparator implements Comparator { 46 | 47 | @Override 48 | public int compare(Integer o1, Integer o2) { 49 | if(o1.equals(o2)) { 50 | return 0; 51 | } 52 | else { 53 | char[] no1String = o1.toString().toCharArray(); 54 | char[] no2String = o2.toString().toCharArray(); 55 | int lowestLength = no1String.length > no2String.length? no2String.length : no1String.length; 56 | //sort in desc order 57 | for(int i = 0; i< lowestLength; i++) { 58 | if(no1String[i] > no2String[i]) { 59 | return -1; 60 | } 61 | else if (no1String[i] < no2String[i]) { 62 | return 1; 63 | } 64 | } 65 | /** 66 | * if it is same so far check check next character of largest array 67 | * At this point you can be sure both chars are not same and 68 | * also they are not equal 69 | * So check next char of largest array and compare it with 1st char of lowest no 70 | * remember we are sorting ind desc 71 | */ 72 | if(no1String.length == lowestLength) 73 | { 74 | //no1String is smaller 75 | if(no2String[no1String.length] > no1String[0]) 76 | { 77 | return 1; 78 | } 79 | else 80 | { 81 | return -1; 82 | } 83 | } 84 | else { 85 | //no2String is smaller 86 | if(no1String[no2String.length] > no2String[0]) 87 | { 88 | return -1; 89 | } 90 | else 91 | { 92 | return 1; 93 | } 94 | } 95 | } 96 | } 97 | 98 | } 99 | 100 | /** 101 | * int doTestsPass() 102 | * Returns 1 if all tests pass. Otherwise returns 0. 103 | */ 104 | public static void doTestsPass() 105 | { 106 | // todo: implement more tests, please 107 | // feel free to make testing more elegant 108 | boolean result = true; 109 | 110 | result = result && (Objects.equals(findLargestNum(Arrays.asList(7, 1, 9, 4, 2)), "97421")); 111 | result = result && (Objects.equals(findLargestNum(Arrays.asList(604, 6, 112, 10, 5, 61, 9)), "9661604511210")); 112 | 113 | if( result ) 114 | { 115 | System.out.println("All tests pass\n"); 116 | } 117 | else 118 | { 119 | System.out.println("There are test failures\n"); 120 | } 121 | } 122 | 123 | /** 124 | * Execution entry point. 125 | */ 126 | public static void main(String[] args) 127 | { 128 | doTestsPass(); 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/com/osfg/questions/LinkedListPalindromeFinder.java: -------------------------------------------------------------------------------- 1 | package com.osfg.questions; 2 | 3 | import java.util.Stack; 4 | 5 | import com.osfg.factory.DSFactory; 6 | import com.osfg.models.ListNode; 7 | 8 | /** 9 | * 10 | * @author athakur Find if a given Singly Linked list is a palindrome or not 11 | */ 12 | public class LinkedListPalindromeFinder { 13 | 14 | /** 15 | * Method 1 : Put all Linked List values in a Stack. Iterate again and 16 | * compare values from head of lost to the top of stack. Time Complexity : 17 | * O(N) Space Complexity : O(N) 18 | * 19 | * @param head 20 | * @return 21 | */ 22 | public static boolean isPalindrome(ListNode head) { 23 | boolean isPanindrome = true; 24 | 25 | Stack stack = new Stack<>(); 26 | ListNode currentNode = head; 27 | 28 | while (currentNode != null) { 29 | stack.push(currentNode.getValue()); 30 | currentNode = currentNode.getNext(); 31 | } 32 | 33 | currentNode = head; 34 | while (currentNode != null) { 35 | if (!currentNode.getValue().equals(stack.pop())) { 36 | isPanindrome = false; 37 | break; 38 | } 39 | currentNode = currentNode.getNext(); 40 | } 41 | return isPanindrome; 42 | } 43 | 44 | /** 45 | * Method 1 : Find the mid. Reverse second half and check. Time Complexity : 46 | * O(N) Space Complexity : O(1) 47 | * 48 | * @param head 49 | * @return 50 | */ 51 | public static boolean isPalindrome2(ListNode head) { 52 | boolean isPanindrome = true; 53 | 54 | ListNode fastPoiner = head; 55 | ListNode slowPointer = head; 56 | ListNode slowPointerPrev = head; 57 | 58 | while (fastPoiner != null && fastPoiner.getNext() != null) { 59 | slowPointerPrev = slowPointer; 60 | slowPointer = slowPointer.getNext(); 61 | fastPoiner = fastPoiner.getNext().getNext(); 62 | } 63 | 64 | if (fastPoiner != null) { 65 | // linked list had odd elements we can loose the middle element 66 | slowPointer = slowPointer.getNext(); 67 | } 68 | 69 | // split two halved 70 | // for odd elements case scenario we completely loose the middle element 71 | slowPointerPrev.setNext(null); 72 | 73 | // store mid. We will use this to detach 1st half once we have revered 74 | // 2nd half 75 | ListNode midPointer = slowPointer; 76 | 77 | // reverse 2nd half 78 | // You can also revisit LinkedListReversal.java 79 | ListNode prevPointer = slowPointer; 80 | ListNode currPointer = slowPointer.getNext(); 81 | 82 | while (currPointer != null) { 83 | ListNode nextPointer = currPointer.getNext(); 84 | currPointer.setNext(prevPointer); 85 | prevPointer = currPointer; 86 | currPointer = nextPointer; 87 | } 88 | 89 | // detach first half 90 | midPointer.setNext(null); 91 | 92 | // currPointer is now null 93 | // prevPointer will point to the last element of original list 94 | // which is now 1st element of 2nd half reversed list 95 | 96 | ListNode reveredSecondHalf = prevPointer; 97 | ListNode firstHalf = head; 98 | while (reveredSecondHalf != null) { 99 | if (!reveredSecondHalf.getValue().equals(firstHalf.getValue())) { 100 | isPanindrome = false; 101 | break; 102 | } 103 | reveredSecondHalf = reveredSecondHalf.getNext(); 104 | firstHalf = firstHalf.getNext(); 105 | } 106 | 107 | return isPanindrome; 108 | } 109 | 110 | public static void main(String args[]) { 111 | 112 | System.out.println(LinkedListPalindromeFinder.isPalindrome(DSFactory.getPalindromicStringLinkedList())); 113 | System.out.println(LinkedListPalindromeFinder.isPalindrome(DSFactory.getStringLinkedList())); 114 | 115 | System.out.println(LinkedListPalindromeFinder.isPalindrome2(DSFactory.getPalindromicStringLinkedList())); 116 | System.out.println(LinkedListPalindromeFinder.isPalindrome2(DSFactory.getStringLinkedList())); 117 | 118 | } 119 | 120 | } 121 | -------------------------------------------------------------------------------- /src/com/osfg/factory/DSFactory.java: -------------------------------------------------------------------------------- 1 | package com.osfg.factory; 2 | 3 | import com.osfg.models.ListNode; 4 | 5 | import java.util.Arrays; 6 | import java.util.Random; 7 | 8 | import com.osfg.models.BTreeNode; 9 | 10 | /** 11 | * 12 | * @author athakur 13 | * Factory class to get pre populated objects 14 | */ 15 | public class DSFactory { 16 | 17 | /** 18 | * 19 | * @return Singly linked list with numeric data 20 | */ 21 | public static ListNode getNumericLinkedList() { 22 | 23 | ListNode head = new ListNode<>(0); 24 | ListNode first = new ListNode<>(1); 25 | ListNode second = new ListNode<>(2); 26 | ListNode third = new ListNode<>(3); 27 | ListNode fourth = new ListNode<>(4); 28 | head.setNext(first); 29 | first.setNext(second); 30 | second.setNext(third); 31 | third.setNext(fourth); 32 | return head; 33 | } 34 | 35 | /** 36 | * 37 | * @param sorted if array needs to be sorted 38 | * @return array data 39 | */ 40 | public static int[] getArray(boolean sorted) { 41 | 42 | if(sorted) { 43 | return new int[]{2, 3, 3, 7, 9, 12, 15, 15, 15}; 44 | } 45 | else { 46 | return new int[]{15, 9, 20, 16 ,4, 1, 7}; 47 | } 48 | 49 | } 50 | 51 | 52 | /** 53 | * 54 | * @return binary search tree 55 | */ 56 | public static BTreeNode getBST() { 57 | 58 | BTreeNode rootNode = new BTreeNode(10); 59 | BTreeNode rootLeftNode = new BTreeNode(7); 60 | BTreeNode rootRightNode = new BTreeNode(12); 61 | 62 | rootNode.setLeft(rootLeftNode); 63 | rootNode.setRight(rootRightNode); 64 | 65 | rootLeftNode.setLeft(new BTreeNode(5)); 66 | rootLeftNode.setRight(new BTreeNode(9)); 67 | 68 | rootRightNode.setLeft(new BTreeNode(11)); 69 | rootRightNode.setRight(new BTreeNode(13)); 70 | 71 | return rootNode; 72 | 73 | } 74 | 75 | /** 76 | * 77 | * @return a binary tree 78 | */ 79 | public static BTreeNode getBTree() { 80 | 81 | BTreeNode rootNode = new BTreeNode(1); 82 | BTreeNode rootLeftNode = new BTreeNode(2); 83 | BTreeNode rootRightNode = new BTreeNode(3); 84 | 85 | rootNode.setLeft(rootLeftNode); 86 | rootNode.setRight(rootRightNode); 87 | 88 | rootLeftNode.setLeft(new BTreeNode(4)); 89 | rootLeftNode.setRight(new BTreeNode(5)); 90 | 91 | rootRightNode.setLeft(new BTreeNode(6)); 92 | rootRightNode.setRight(new BTreeNode(7)); 93 | 94 | return rootNode; 95 | 96 | } 97 | 98 | /** 99 | * 100 | * @param sorted if array is sorted 101 | * @param arraySize size of the array to be created 102 | * @param maxBound elements will be in range 0 - maxBound 103 | * @return 104 | */ 105 | public static int[] getArray(boolean sorted, int arraySize, int maxBound) { 106 | 107 | Random random = new Random(); 108 | int[] array = new int[arraySize]; 109 | 110 | for(int i=0; i< arraySize;i++) { 111 | array[i] = random.nextInt(maxBound); 112 | } 113 | 114 | if(sorted) { 115 | Arrays.sort(array); 116 | } 117 | 118 | return array; 119 | 120 | } 121 | 122 | /** 123 | * 124 | * @return singly linked list with palindromic string 125 | */ 126 | public static ListNode getPalindromicStringLinkedList() { 127 | 128 | ListNode head = new ListNode<>("a"); 129 | ListNode first = new ListNode<>("b"); 130 | ListNode second = new ListNode<>("c"); 131 | ListNode third = new ListNode<>("b"); 132 | ListNode fourth = new ListNode<>("a"); 133 | head.setNext(first); 134 | first.setNext(second); 135 | second.setNext(third); 136 | third.setNext(fourth); 137 | return head; 138 | } 139 | 140 | /** 141 | * 142 | * @return singly linked list consisting of string elements 143 | */ 144 | public static ListNode getStringLinkedList() { 145 | 146 | ListNode head = new ListNode<>("a"); 147 | ListNode first = new ListNode<>("b"); 148 | ListNode second = new ListNode<>("c"); 149 | ListNode third = new ListNode<>("e"); 150 | ListNode fourth = new ListNode<>("f"); 151 | head.setNext(first); 152 | first.setNext(second); 153 | second.setNext(third); 154 | third.setNext(fourth); 155 | return head; 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /src/com/osfg/utils/BTreePrinter.java: -------------------------------------------------------------------------------- 1 | package com.osfg.utils; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | import java.util.Stack; 6 | 7 | import com.osfg.models.BTreeNode; 8 | 9 | /** 10 | * 11 | * @author athakur Class to print pre order, post order, in order and level 12 | * order btree traversals 13 | */ 14 | public class BTreePrinter { 15 | 16 | // Root - Left - Right 17 | public static void printPreOrderTraversal(BTreeNode root) { 18 | if (root != null) 19 | System.out.println(root.getData()); 20 | 21 | if (root.getLeft() != null) 22 | printPreOrderTraversal(root.getLeft()); 23 | if (root.getRight() != null) 24 | printPreOrderTraversal(root.getRight()); 25 | } 26 | 27 | // Left - Right - Root 28 | public static void printPostOrderTraversal(BTreeNode root) { 29 | if (root.getLeft() != null) 30 | printPostOrderTraversal(root.getLeft()); 31 | if (root.getRight() != null) 32 | printPostOrderTraversal(root.getRight()); 33 | if (root != null) 34 | System.out.println(root.getData()); 35 | } 36 | 37 | // Left - Root - Right 38 | public static void printInOrderTraversal(BTreeNode root) { 39 | if (root.getLeft() != null) 40 | printInOrderTraversal(root.getLeft()); 41 | 42 | if (root != null) 43 | System.out.println(root.getData()); 44 | 45 | if (root.getRight() != null) 46 | printInOrderTraversal(root.getRight()); 47 | 48 | } 49 | 50 | private static Queue levelOrderQueue = new LinkedList(); 51 | 52 | public static void printLevelOrderTraversal(BTreeNode root) { 53 | 54 | if (root == null) { 55 | return; 56 | } 57 | 58 | System.out.println(root.getData()); 59 | 60 | if (root.getLeft() != null) 61 | levelOrderQueue.offer(root.getLeft()); 62 | 63 | if (root.getRight() != null) 64 | levelOrderQueue.offer(root.getRight()); 65 | 66 | if (!levelOrderQueue.isEmpty()) { 67 | printLevelOrderTraversal(levelOrderQueue.poll()); 68 | } else { 69 | levelOrderQueue.clear(); 70 | } 71 | 72 | } 73 | 74 | public static int getHeight(BTreeNode root) { 75 | if (root == null) { 76 | return 0; 77 | } else { 78 | int leftHeight = getHeight(root.getLeft()); 79 | int rightHeight = getHeight(root.getRight()); 80 | return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1; 81 | } 82 | } 83 | 84 | /** 85 | * 86 | * @param root 87 | * if the btrr Worst case time complexity - O(N^2) for skewed 88 | * trees No extra space 89 | */ 90 | public static void printSpiralRecurssive(BTreeNode root) { 91 | boolean leftToRight = false; 92 | int height = getHeight(root); 93 | for (int i = 1; i <= height; i++) { 94 | printLevelRecurssive(root, i, leftToRight); 95 | leftToRight = !leftToRight; 96 | } 97 | 98 | } 99 | 100 | public static void printLevelRecurssive(BTreeNode root, int level, boolean leftToRight) { 101 | if (level == 1) { 102 | System.out.println(root.getData()); 103 | } else { 104 | if (leftToRight) { 105 | printLevelRecurssive(root.getLeft(), level - 1, leftToRight); 106 | printLevelRecurssive(root.getRight(), level - 1, leftToRight); 107 | } else { 108 | printLevelRecurssive(root.getRight(), level - 1, leftToRight); 109 | printLevelRecurssive(root.getLeft(), level - 1, leftToRight); 110 | } 111 | 112 | } 113 | } 114 | 115 | private static Stack leftToRight = new Stack<>(); 116 | private static Stack rightToLeft = new Stack<>(); 117 | 118 | /** 119 | * 120 | * @param root 121 | * if the btrr Time complexity - O(N) space complexity - O(N) 122 | */ 123 | public static void printSpiralIterative(BTreeNode root) { 124 | 125 | leftToRight.push(root); 126 | 127 | while (!rightToLeft.isEmpty() || !leftToRight.isEmpty()) { 128 | while (!rightToLeft.isEmpty()) { 129 | BTreeNode node = rightToLeft.pop(); 130 | System.out.println(node.getData()); 131 | if (node.getLeft() != null) { 132 | leftToRight.push(node.getLeft()); 133 | } 134 | if (node.getRight() != null) { 135 | leftToRight.push(node.getRight()); 136 | } 137 | } 138 | 139 | while (!leftToRight.isEmpty()) { 140 | BTreeNode node = leftToRight.pop(); 141 | System.out.println(node.getData()); 142 | if (node.getRight() != null) { 143 | rightToLeft.push(node.getRight()); 144 | } 145 | if (node.getLeft() != null) { 146 | rightToLeft.push(node.getLeft()); 147 | } 148 | } 149 | } 150 | 151 | } 152 | 153 | } 154 | --------------------------------------------------------------------------------