├── .DS_Store ├── Leetcode ├── .DS_Store ├── JavaBasics.java ├── LeetCode30days │ ├── BackSpaceStringCompare │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── BuySellStock │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── DiameterOfBinaryTree │ │ ├── Solution$Reader.class │ │ ├── Solution$TreeNode.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── GroupAnagrams │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── MiddleLinkedList │ │ ├── Solution$ListNode.class │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── MinStack │ │ └── Solution.java │ ├── PriorityQueueGreedy │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── Recursion │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── SumContiguousArray │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ ├── findNonRepeatingNumber │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java │ └── maximumsubarray │ │ ├── Solution$Reader.class │ │ ├── Solution.class │ │ └── Solution.java ├── Solution.java └── examples │ ├── Aru12$Reader.class │ ├── Aru12.class │ ├── Aru12.java │ ├── Leetcode │ ├── CommonSubstring.java │ ├── MergeTwoSortedLists.java │ ├── PrefixSubstring.java │ └── ValidParenthesis.java │ ├── Solution1$Reader.class │ ├── Solution1.class │ ├── Solution1.java │ ├── Solution2$Reader.class │ ├── Solution2.class │ ├── Solution2.java │ ├── Solution3$Reader.class │ ├── Solution3.class │ ├── Solution3.java │ ├── Vadiraj$Reader.class │ ├── Vadiraj.class │ └── Vadiraj.java ├── README.md ├── allPermutation.java ├── amazon ├── ques5.class └── ques5.java ├── ch1 Array and Strings ├── permute.class ├── permute.java ├── ques1.class ├── ques1.java ├── ques2.class ├── ques2.java ├── ques3.class ├── ques3.java ├── ques4.class ├── ques4.java ├── ques5.class ├── ques5.java ├── ques6.class ├── ques6.java ├── ques7.class └── ques7.java ├── ch10 Sorting and Searching ├── a.out ├── bucketsort.cpp ├── mergeSort.class ├── mergeSort.java ├── ques2.class ├── ques2.java ├── ques5.class ├── ques5.java ├── quicksort.class ├── quicksort.java ├── radix.class └── radix.java ├── ch2 Linked Lists ├── Linked.class ├── Node.class ├── circularlink.class ├── circularlinked.class ├── circularlinked.java ├── doublelinked.class ├── doublelinked.java ├── lindedrecur.class ├── linked.java ├── linkedrecur.class ├── linkedrecur.java ├── practise │ ├── Node.class │ ├── palindrome.class │ └── palindrome.java ├── ques1.class ├── ques1.java ├── ques3.class ├── ques3.java ├── ques4.class ├── ques4.java ├── ques6.class ├── ques6.java ├── ques7.class ├── ques7.java ├── ques8.class ├── ques8.java └── revrecur.java ├── ch3 Stacks and Queues ├── Animal.class ├── Myqueue.class ├── Stack$StackNode.class ├── Stack.class ├── a.out ├── c++ │ ├── a.out │ ├── circularqueue.cpp │ ├── queue.cpp │ └── stack.cpp ├── linkedlist.class ├── ques3.cpp ├── ques4.class ├── ques4.cpp ├── ques4.java ├── ques5.class ├── ques5.java ├── ques6.class ├── ques6.java ├── stackdemo.class └── stackdemo.java ├── ch4 Trees and Graphs ├── Graph.class ├── Linked.class ├── Tree.class ├── dfs.class ├── dfsbfs.class ├── dfsbfs.java ├── graphexample.class ├── graphexample.java ├── minimaltree.class ├── minimaltree.java ├── node.class ├── ques1.java ├── ques12.class ├── ques12.java ├── ques3.class ├── ques3.java ├── ques3alternate.java ├── ques4.class ├── ques4.java ├── ques5.class ├── ques5.java ├── ques6.class ├── ques6.java ├── ques7.java ├── ques8.class ├── ques8.java ├── ques9.class ├── ques9.java ├── treeexample.class └── treeexample.java ├── ch5 bitmanipulation ├── BitManipulation.class ├── BitManipulation.java ├── ques2.class ├── ques2.java ├── ques3.class ├── ques3.java ├── ques4.class └── ques4.java ├── ch6 Maths and logic ├── Gender.class ├── ch7 Object Oriented Programming │ ├── Employee.class │ ├── ques2.class │ └── ques2.java ├── ques10.class ├── ques10.java ├── ques7.class └── ques7.java ├── ch8 recursion and dynamic prog ├── Point.class ├── ShortestPath.class ├── ques1.class ├── ques1.java ├── ques11.class ├── ques11.java ├── ques12.class ├── ques12.java ├── ques2.class ├── ques2.java ├── ques4.class ├── ques4.java ├── ques5.class ├── ques5.java ├── ques6.class ├── ques6.java ├── ques7.class ├── ques7.java ├── ques8.class ├── ques8.java ├── ques9.class └── ques9.java ├── codechef ├── kconcat.class ├── kconcat.java ├── maxscore.class ├── maxscore.java ├── partition.class ├── partition.java ├── rect.class ├── rect.java ├── stringmerging.class ├── stringmerging.java ├── subset.class └── subset.java ├── hackerrank ├── Algorithm │ ├── diagonal.class │ ├── diagonal.java │ ├── implementation │ │ ├── climbingleaderboard.class │ │ ├── climbingleaderboard.java │ │ └── gradingstudents.java │ ├── search │ │ ├── Coordinate.class │ │ ├── Gridland.java │ │ ├── HackerlandRadio.java │ │ ├── allCombination.class │ │ ├── allCombination.java │ │ ├── icecream.java │ │ ├── knightmove.class │ │ ├── knightmove.java │ │ ├── leftandright.java │ │ ├── maxsubarraysum.class │ │ ├── maxsubarraysum.java │ │ └── pairs.java │ └── warmup │ │ ├── candle.java │ │ ├── changetime.java │ │ ├── minmax.java │ │ ├── pattern.java │ │ └── precision.java ├── Dynamic Programming │ ├── fibo.class │ ├── fibo.java │ ├── planepoint$Point.class │ ├── planepoint.class │ └── planepoint.java ├── Graph Theory │ ├── BFSshortest.class │ ├── BFSshortest.java │ ├── Graph.class │ ├── adjmatrix.java │ ├── demo.class │ ├── demo.java │ ├── intro.class │ ├── intro.java │ ├── journeytomoon.class │ ├── journeytomoon.java │ ├── printAllPaths.class │ ├── printAllPaths.java │ └── roads.java └── Strings │ ├── richierich.class │ ├── richierich.java │ ├── sherlock.class │ └── sherlock.java ├── la.java ├── mentorship └── clone.java ├── microsoft interview ├── Tree.class ├── la.class ├── la.java ├── node.class ├── ques1.class ├── ques1.java ├── ques10.class ├── ques10.java ├── ques11.class ├── ques11.java ├── ques12.java ├── ques13.class ├── ques13.java ├── ques14.class ├── ques14.java ├── ques15.class ├── ques15.java ├── ques16.class ├── ques16.java ├── ques17.class ├── ques17.java ├── ques18.class ├── ques18.java ├── ques19.class ├── ques19.java ├── ques2.java ├── ques20.java ├── ques21.class ├── ques21.java ├── ques3.class ├── ques3.java ├── ques4.class ├── ques4.java ├── ques5.class ├── ques5.java ├── ques6.class ├── ques6.java ├── ques7.class ├── ques7.java ├── ques8.class ├── ques8.java ├── ques9.class └── ques9.java ├── node.class ├── palindrome.java ├── permutestrings.class ├── permutestrings.java ├── pg46 ├── solution.class └── solution.java ├── ques3again.java └── samsung ├── Combinatorial ├── CombinationalSum.class ├── CombinationalSum.java ├── LongcommonSubsequence.class ├── LongestcommonSubsequence.java ├── allLexicographic.class ├── allLexicographic.java ├── allPermutation.class ├── allPermutation.java ├── allPermuteiterative.class ├── allPermuteiterative.java ├── allPossibelString.class ├── allPossibelString.java ├── countDistinctPermutation.class ├── countDistinctPermutation.java ├── countSumCombination.class ├── countSumCombination.java ├── countnoofString.class ├── countnoofString.java ├── cutnintok.class ├── cutnintok.java ├── findSumallPer.class ├── findSumallPer.java ├── kdifferencepermutation.class ├── kdifferencepermutation.java ├── kthsmallestnumber.class ├── kthsmallestnumber.java ├── largeFactorial.class ├── largeFactorial.java ├── mobilepattern.java ├── nextLexicographic.class ├── nextLexicographic.java ├── printLexafterkswaps.class └── printLexafterkswaps.java ├── Graph.class ├── Graph ├── Graph.class ├── Node.class ├── bfs.class ├── bfs.java ├── bidirectional.class ├── bidirectional.java ├── bipartite.java ├── clone.class ├── clone.java ├── detectcycle.class ├── detectcycle.java ├── dfs.class ├── dfs.java ├── dfsiterative.class ├── dfsiterative.java ├── dfsmatrix.class ├── dfsmatrix.java ├── interviewgoright.class ├── interviewgoright.java ├── longestCable.java ├── mcoloring.class ├── mcoloring.java ├── mcoloring2.class ├── mcoloring2.java ├── printAllPaths.class ├── printAllPaths.java ├── topologicalsort.class └── topologicalsort.java ├── NGE.class ├── NGE.java ├── Node.class ├── SearchMazeBFS$Point.class ├── SearchMazeBFS$QueueNode.class ├── SearchMazeBFS.class ├── SearchMazeBFS.java ├── ShortestPath.class ├── ShortestPath.java ├── Tree.class ├── bfsgraph.class ├── bfsgraph.java ├── catalan.class ├── catalan.java ├── countflipbits.class ├── countflipbits.java ├── detectcycle.class ├── detectcycle.java ├── eggDrop.class ├── eggDrop.java ├── graphdfs.class ├── graphdfs.java ├── heapSort.class ├── heapSort.java ├── kthlargestinBST.class ├── kthlargestinBST.java ├── leftview.class ├── leftview.java ├── maxProductsubArray.class ├── maxProductsubArray.java ├── merge2list.class ├── merge2list.java ├── missing.java ├── multiply2number.java ├── nodedel.class ├── nodedel.java ├── recursremovedup.java ├── simplegraph.class ├── simplegraphimplement.class ├── simplegraphimplement.java ├── sumatk.class ├── sumatk.java └── twostacksinaarray.java /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/.DS_Store -------------------------------------------------------------------------------- /Leetcode/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/.DS_Store -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/BackSpaceStringCompare/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/BackSpaceStringCompare/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/BackSpaceStringCompare/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/BackSpaceStringCompare/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/BuySellStock/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/BuySellStock/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/BuySellStock/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/BuySellStock/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution$TreeNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution$TreeNode.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/DiameterOfBinaryTree/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/GroupAnagrams/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/GroupAnagrams/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/GroupAnagrams/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/GroupAnagrams/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/MiddleLinkedList/Solution$ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/MiddleLinkedList/Solution$ListNode.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/MiddleLinkedList/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/MiddleLinkedList/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/MiddleLinkedList/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/MiddleLinkedList/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/PriorityQueueGreedy/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/PriorityQueueGreedy/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/PriorityQueueGreedy/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/PriorityQueueGreedy/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/Recursion/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/Recursion/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/Recursion/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/Recursion/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/SumContiguousArray/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/SumContiguousArray/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/SumContiguousArray/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/SumContiguousArray/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/findNonRepeatingNumber/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/findNonRepeatingNumber/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/findNonRepeatingNumber/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/findNonRepeatingNumber/Solution.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/maximumsubarray/Solution$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/maximumsubarray/Solution$Reader.class -------------------------------------------------------------------------------- /Leetcode/LeetCode30days/maximumsubarray/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/LeetCode30days/maximumsubarray/Solution.class -------------------------------------------------------------------------------- /Leetcode/examples/Aru12$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Aru12$Reader.class -------------------------------------------------------------------------------- /Leetcode/examples/Aru12.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Aru12.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution1$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution1$Reader.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution1.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution2$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution2$Reader.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution2.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution3$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution3$Reader.class -------------------------------------------------------------------------------- /Leetcode/examples/Solution3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Solution3.class -------------------------------------------------------------------------------- /Leetcode/examples/Vadiraj$Reader.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Vadiraj$Reader.class -------------------------------------------------------------------------------- /Leetcode/examples/Vadiraj.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/Leetcode/examples/Vadiraj.class -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cracking-The-Coding-Interview-Solutions 2 | 3 | Hello, This Repository has all the solutions of the book Cracking the Coding Interviews. It is a must see repository for those who are preparing from the book. 4 | 5 | The topics covered are : 6 | 7 | HashTables, HashMaps, Maps, ArrayList, String Builder, String Buffer, Linked List, Arrays, Generic Classes, Java Basics and Concepts, Special Emphasis on Big O, Stacks and Queues, Trees and Graphs, Bit Manipulation, Math and Logic Puzzles, OOP, Recursion and Dynamic Programming, System Design and Scalablity, Sorting and Searching, Testing, C and C++, Advance Java Concepts. 8 | 9 | # All Codes Are written with least Run Time Possible 10 | 11 | # Created By Aman Singh Thakur 12 | 13 | Read, Share and Contribute People ! 14 | -------------------------------------------------------------------------------- /allPermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class allPermutation{ 3 | 4 | static void permute(String str, int l, int r){ 5 | if(l==r) 6 | System.out.println(str); 7 | else{ 8 | for(int i=l; i<=r; i++){ 9 | str = swap(str,l,i); 10 | permute(str,l+1,r); 11 | str = swap(str,l,i); 12 | } 13 | } 14 | } 15 | 16 | static String swap(String str, int i, int j){ 17 | char temp; 18 | char[] charArray = str.toCharArray(); 19 | temp = charArray[i]; 20 | charArray[i] = charArray[j]; 21 | charArray[j] = temp; 22 | return String.valueOf(charArray); 23 | } 24 | 25 | public static void main(String[] args) { 26 | 27 | String str; 28 | Scanner sc = new Scanner(System.in); 29 | str = sc.nextLine(); 30 | permute(str,0,str.length-1); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /amazon/ques5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/amazon/ques5.class -------------------------------------------------------------------------------- /amazon/ques5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques5{ 3 | public static void main(String[] args) { 4 | 5 | Scanner sc = new Scanner(System.in); 6 | int n = sc.nextInt(); 7 | 8 | int nbar = -n; 9 | System.out.println(nbar&n); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ch1 Array and Strings/permute.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch1 Array and Strings/permute.class -------------------------------------------------------------------------------- /ch1 Array and Strings/permute.java: -------------------------------------------------------------------------------- 1 | class permute 2 | { 3 | public static void main(String[] args) { 4 | String str = "ABC"; 5 | int n = str.length(); 6 | permute(str, 0, n-1); 7 | } 8 | 9 | static void permute(String str, int l, int r) 10 | { 11 | if(l==r) 12 | System.out.println(str); 13 | else 14 | { 15 | for(int i=l; i<=r; i++) 16 | { 17 | str = swap(str, l, i); 18 | permute(str, l+1, r); 19 | str = swap(str, l, i); 20 | } 21 | } 22 | } 23 | 24 | static String swap(String str, int l, int r) 25 | { 26 | char temp; 27 | char[] charArray = str.toCharArray(); 28 | temp = charArray[l]; 29 | charArray[l]=charArray[r]; 30 | charArray[r]=temp; 31 | return String.valueOf(charArray); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /ch1 Array and Strings/ques1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch1 Array and Strings/ques1.class -------------------------------------------------------------------------------- /ch1 Array and Strings/ques1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.util.Scanner; 3 | 4 | class ques1{ 5 | public static void main(String[] args) { 6 | 7 | Scanner sc = new Scanner(System.in); 8 | //Ask for input 9 | String name = sc.next(); 10 | isCharacterUnique(name); 11 | } 12 | 13 | public static void isCharacterUnique(String name) 14 | { 15 | name = name.toLowerCase(); 16 | int[] charMatched = new int[26]; 17 | int counter=0; 18 | 19 | for(int i=0; i<26; i++) 20 | charMatched[i]=0; 21 | 22 | for(char a='a'; counter1) 38 | { 39 | flag=1; 40 | break; 41 | } 42 | } 43 | if(flag==0) 44 | System.out.println("Unique"); 45 | else 46 | System.out.println("Not Unique"); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /ch1 Array and Strings/ques2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch1 Array and Strings/ques2.class -------------------------------------------------------------------------------- /ch1 Array and Strings/ques2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ques2 4 | { 5 | public static void main(String[] args) { 6 | 7 | Scanner in = new Scanner(System.in); 8 | String name1 = in.next(); 9 | String name2 = in.next(); 10 | 11 | int[] match = new int[26]; 12 | 13 | for(int i=0; i<26; i++) 14 | match[i]=0; 15 | 16 | int counter=0; 17 | 18 | //check if they have the same stirng length 19 | 20 | for (char a='a'; counter hm = new HashMap(); 17 | for(char a='a'; a<'z'; a++) 18 | hm.put(a,0); 19 | 20 | for(int i=0; i1 || counter<-1) 49 | System.out.println("String is not edited once"); 50 | else 51 | System.out.println("String is edited once"); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /ch1 Array and Strings/ques6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch1 Array and Strings/ques6.class -------------------------------------------------------------------------------- /ch1 Array and Strings/ques6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques6{ 3 | public static void main(String[] args) { 4 | 5 | Scanner in = new Scanner(System.in); 6 | String str = in.next(); 7 | str = compress(str); 8 | System.out.println(str); 9 | } 10 | 11 | static String compress(String str) 12 | { 13 | int count=1; 14 | 15 | // Use String Builder (Rookie Mistake) 16 | 17 | StringBuilder newstr = new StringBuilder(); 18 | newstr.append(str.charAt(0)); 19 | for(int i=0; itop 37 | arr[n-1-offset-i][offset]=arr[n-1-offset][n-1-offset-i]; 38 | arr[n-1-offset][n-1-offset-i]=arr[n-1-offset][i]; 39 | arr[i][n-1-offset]=temp; 40 | } 41 | offset++; 42 | } 43 | 44 | System.out.println(); 45 | 46 | for(int i=0; i 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | // Function to sort arr[] of size n using bucket sort 10 | void bucketSort(float arr[], int n) 11 | { 12 | // 1) Create n empty buckets 13 | vector b[n]; 14 | 15 | // 2) Put array elements in different buckets 16 | for (int i=0; ii; j--) 36 | output[j+1]=output[j]; 37 | output[i+1]=temp; 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | public static void main(String[] args) { 44 | 45 | ques2 q = new ques2(); 46 | String[] temp = new String[]{"aman","singh","nama","thakur","ghnis","maan"}; 47 | temp = q.groupAnagrams(temp); 48 | for(String s : temp) 49 | System.out.println(s); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /ch10 Sorting and Searching/ques5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch10 Sorting and Searching/ques5.class -------------------------------------------------------------------------------- /ch10 Sorting and Searching/ques5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques5{ 3 | 4 | int SparseSearch(String[] arr, int first, int last, String str){ 5 | 6 | if(first>last) return -1; 7 | int mid = (last+first) /2; 8 | 9 | if(arr[mid].equals("")){ 10 | 11 | int left = mid-1; 12 | int right = mid+1; 13 | 14 | // Great Code !!!! :) 15 | 16 | while(true){ 17 | if(leftlast) 18 | return -1; 19 | else if(right<=last && !arr[right].equals("")){ 20 | mid = right; 21 | break; 22 | } 23 | else if(left>=first && !arr[left].equals("")){ 24 | mid = left; 25 | break; 26 | } 27 | right++; 28 | left--; 29 | } 30 | } 31 | 32 | if(str.equals(arr[mid])) 33 | return mid; 34 | else if(arr[mid].compareTo(str)<0) // Important function string.compareTo 35 | return SparseSearch(arr,mid+1,last,str); 36 | 37 | return SparseSearch(arr,first,mid-1,str); 38 | } 39 | 40 | public static void main(String[] args) { 41 | 42 | ques5 q = new ques5(); 43 | String[] arr = new String[]{"","","aman","","singh","","","","","","thakur","","","ab"}; 44 | int i=q.SparseSearch(arr,0,arr.length-1,"aman"); 45 | if(i==-1) 46 | System.out.println("Not found"); 47 | else 48 | System.out.println("Found at + "+i); 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /ch10 Sorting and Searching/quicksort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch10 Sorting and Searching/quicksort.class -------------------------------------------------------------------------------- /ch10 Sorting and Searching/quicksort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class quicksort{ 3 | 4 | void quickSort(int[] arr, int left, int right){ 5 | int index = partition(arr,left,right); 6 | if(leftpivot) right--; 17 | 18 | if(left<=right){ 19 | swap(arr,left,right); 20 | left++; right--; 21 | } 22 | } 23 | return left; 24 | } 25 | 26 | void swap(int[] arr, int left, int right){ 27 | int temp = arr[left]; 28 | arr[left] = arr[right]; 29 | arr[right] = temp; 30 | } 31 | 32 | public static void main(String[] args) { 33 | 34 | quicksort q = new quicksort(); 35 | int arr[]={3,7,1,9,2,0}; 36 | q.quickSort(arr,0,arr.length-1); 37 | for(int i : arr) 38 | System.out.println(i); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /ch10 Sorting and Searching/radix.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch10 Sorting and Searching/radix.class -------------------------------------------------------------------------------- /ch10 Sorting and Searching/radix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class radix{ 4 | 5 | int getMax(int arr[]){ 6 | int max=arr[0]; 7 | for(int i=1; i=0; i--){ 29 | output[count[(arr[i]/exp%10)]-1] = arr[i]; 30 | count[(arr[i]/exp%10)]--; 31 | } 32 | 33 | for(i=0; i0; exp*=10) 41 | countsort(arr,n,exp); 42 | } 43 | public static void main(String[] args) { 44 | radix r = new radix(); 45 | int[] arr = new int[]{1,4,5,0,2,2,5}; 46 | r.radixsort(arr,arr.length); 47 | for(int i: arr) 48 | System.out.println(i); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /ch2 Linked Lists/Linked.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/Linked.class -------------------------------------------------------------------------------- /ch2 Linked Lists/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/Node.class -------------------------------------------------------------------------------- /ch2 Linked Lists/circularlink.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/circularlink.class -------------------------------------------------------------------------------- /ch2 Linked Lists/circularlinked.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/circularlinked.class -------------------------------------------------------------------------------- /ch2 Linked Lists/circularlinked.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node{ 3 | int data; 4 | Node next = this; 5 | Node(int d) 6 | { 7 | data = d; 8 | next = this; 9 | } 10 | void append(int d) 11 | { 12 | Node n = this; 13 | //System.out.println("lala1"); 14 | while(n.next!=this) 15 | { 16 | //System.out.println(n.data); 17 | n = n.next; 18 | } 19 | Node newnode = new Node(d); 20 | n.next = newnode; 21 | newnode.next = this; 22 | //System.out.println("lala3"); 23 | //System.out.println(); 24 | } 25 | 26 | void output() 27 | { 28 | Node n = this; 29 | while(n.next!=this) 30 | { 31 | System.out.println(n.data+" "); 32 | n = n.next; 33 | } 34 | } 35 | } 36 | 37 | class circularlinked 38 | { 39 | public static void main(String[] args) { 40 | 41 | Scanner in = new Scanner(System.in); 42 | int val = in.nextInt(); 43 | Node head = new Node(val); 44 | //System.out.println(head.data); 45 | //Node head = new Node(); 46 | for(int i=0; i<3; i++) 47 | { 48 | int d = in.nextInt(); 49 | head.append(d); 50 | } 51 | //System.out.println(head.next.next.data); 52 | head.output(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /ch2 Linked Lists/doublelinked.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/doublelinked.class -------------------------------------------------------------------------------- /ch2 Linked Lists/lindedrecur.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/lindedrecur.class -------------------------------------------------------------------------------- /ch2 Linked Lists/linkedrecur.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/linkedrecur.class -------------------------------------------------------------------------------- /ch2 Linked Lists/linkedrecur.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | 12 | void insert(Node head, int d) 13 | { 14 | if(head.next==null) 15 | { 16 | Node temp = new Node(d); 17 | head.next = temp; 18 | } 19 | else 20 | insert(head.next,d); 21 | } 22 | 23 | void output() 24 | { 25 | Node n = this; 26 | while(n!=null) 27 | { 28 | System.out.println(n.data); 29 | n=n.next; 30 | } 31 | } 32 | } 33 | 34 | class linkedrecur 35 | { 36 | public static void main(String[] args) { 37 | 38 | Scanner in = new Scanner(System.in); 39 | int d = in.nextInt(); 40 | Node head = new Node(d); 41 | for(int i=0; i<4; i++) 42 | { 43 | d = in.nextInt(); 44 | head.insert(head,d); 45 | } 46 | head.output(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ch2 Linked Lists/practise/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/practise/Node.class -------------------------------------------------------------------------------- /ch2 Linked Lists/practise/palindrome.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/practise/palindrome.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques1.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | Node(){ 12 | //data =0; 13 | Node n =this; 14 | n =null; 15 | } 16 | 17 | void appendToTail(int d) 18 | { 19 | Node temp = new Node(d); 20 | Node n = this; 21 | while(n.next!=null) 22 | { 23 | n = n.next; 24 | } 25 | n.next= temp; 26 | } 27 | 28 | void output() 29 | { 30 | Node temp = this; 31 | while(temp!=null) 32 | { 33 | System.out.println(temp.data); 34 | temp = temp.next; 35 | } 36 | } 37 | } 38 | 39 | class ques1 40 | { 41 | public static void main(String[] args) { 42 | 43 | Scanner in = new Scanner(System.in); 44 | int d = in.nextInt(); 45 | Node head = new Node(d); 46 | for(int i=0; i<4; i++) 47 | { 48 | d = in.nextInt(); 49 | head.appendToTail(d); 50 | } 51 | if(!checkDuplicate(head)) 52 | System.out.println("Not duplicated"); 53 | else 54 | System.out.println("Duplicate"); 55 | } 56 | 57 | static boolean checkDuplicate(Node head) 58 | { 59 | HashMap hm = new HashMap(); 60 | int i=0; 61 | while(head!=null) 62 | { 63 | if(hm.get(head.data)!=null) 64 | return true; 65 | else{ 66 | hm.put(head.data, i); 67 | System.out.println(hm.get(head.data)+" "+head.data); 68 | i++; 69 | } 70 | head = head.next; 71 | } 72 | return false; 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /ch2 Linked Lists/ques3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques3.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | Node(){ 12 | //data =0; 13 | Node n =this; 14 | n =null; 15 | } 16 | 17 | void appendToTail(int d) 18 | { 19 | Node temp = new Node(d); 20 | Node n = this; 21 | while(n.next!=null) 22 | { 23 | n = n.next; 24 | } 25 | n.next= temp; 26 | temp.next=null; 27 | } 28 | 29 | void output() 30 | { 31 | Node temp = this; 32 | while(temp!=null) 33 | { 34 | System.out.println(temp.data); 35 | temp = temp.next; 36 | } 37 | } 38 | 39 | void del() 40 | { 41 | Node head=this; 42 | Node fast=head, slow=head; 43 | Node prev=null; 44 | while(fast.next!=null) 45 | { 46 | prev=slow; 47 | slow=slow.next; 48 | fast=fast.next; 49 | if(fast.next!=null) 50 | fast=fast.next; 51 | //System.out.println(slow.data+" "+fast.data); 52 | } 53 | prev.next = slow.next; 54 | } 55 | } 56 | 57 | class ques3 58 | { 59 | public static void main(String[] args) { 60 | 61 | Scanner in = new Scanner(System.in); 62 | int d = in.nextInt(); 63 | Node head = new Node(d); 64 | for(int i=0; i<3; i++) 65 | { 66 | d = in.nextInt(); 67 | head.appendToTail(d); 68 | } 69 | System.out.println(); 70 | //head.output(); 71 | head.del(); 72 | head.output(); 73 | //System.out.println(); 74 | //head.output(); 75 | //del(head); 76 | //System.out.println(); 77 | //head.output(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /ch2 Linked Lists/ques4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques4.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next=null; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | Node(){} 12 | 13 | Node appendToHead(int d) 14 | { 15 | Node temp = new Node(d); 16 | Node n = this; 17 | if(n==null) 18 | return temp; 19 | temp.next = n; 20 | return temp; 21 | } 22 | 23 | void output() 24 | { 25 | Node temp = this; 26 | while(temp!=null) 27 | { 28 | System.out.print(temp.data+" "); 29 | temp = temp.next; 30 | } 31 | System.out.println(); 32 | } 33 | 34 | } 35 | 36 | class ques4 37 | { 38 | public static void main(String[] args) { 39 | 40 | Scanner in = new Scanner(System.in); 41 | Node head1 = new Node(); 42 | Node head2 = new Node(); 43 | int d; 44 | for(int i=0; i<3; i++) 45 | { 46 | d = in.nextInt(); 47 | head1=head1.appendToHead(d); 48 | } 49 | for(int i=0; i<3; i++) 50 | { 51 | d = in.nextInt(); 52 | head2=head2.appendToHead(d); 53 | } 54 | //System.out.println(); 55 | head1.output(); 56 | //System.out.println(); 57 | head2.output(); 58 | Node newnode = Sum(head1, head2); 59 | System.out.println(); 60 | newnode.output(); 61 | } 62 | 63 | static Node Sum(Node head1, Node head2) 64 | { 65 | Node newnode = new Node(); 66 | while(head1.next!=null) 67 | { 68 | int sum = head1.data + head1.data; 69 | int carry = sum/10; 70 | newnode = newnode.appendToHead(sum-carry*10); 71 | head1 = head1.next; 72 | head2 = head2.next; 73 | //System.out.println(sum+" "+carry+" "+(sum-carry*10)); 74 | //newnode.output(); 75 | } 76 | return newnode; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /ch2 Linked Lists/ques6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques6.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | Node(){ 12 | //data =0; 13 | Node n =this; 14 | n =null; 15 | } 16 | 17 | void appendToTail(int d) 18 | { 19 | Node temp = new Node(d); 20 | Node n = this; 21 | while(n.next!=null) 22 | { 23 | n = n.next; 24 | } 25 | n.next= temp; 26 | } 27 | 28 | void output() 29 | { 30 | Node temp = this; 31 | while(temp!=null) 32 | { 33 | System.out.println(temp.data); 34 | temp = temp.next; 35 | } 36 | } 37 | } 38 | 39 | class ques6 40 | { 41 | public static void main(String[] args) { 42 | 43 | Scanner in = new Scanner(System.in); 44 | int d = in.nextInt(); 45 | Node head = new Node(d); 46 | for(int i=0; i<5; i++) 47 | { 48 | d = in.nextInt(); 49 | head.appendToTail(d); 50 | } 51 | //head.output(); 52 | if(isPalindrome(head)) 53 | System.out.println("Palindrome"); 54 | else 55 | System.out.println("Not a Palindrome"); 56 | } 57 | 58 | static boolean isPalindrome(Node head) 59 | { 60 | Node fast=head, slow=head; 61 | int[] stack = new int[100]; 62 | int i=0; 63 | while(fast!=null && fast.next!=null) 64 | { 65 | stack[i] = slow.data; 66 | i++; 67 | slow = slow.next; 68 | fast = fast.next; 69 | //if(fast.next!=null) 70 | fast=fast.next; 71 | } 72 | if(fast!=null) 73 | slow = slow.next; 74 | while(slow.next!=null) 75 | { 76 | i--; 77 | if(stack[i]!=slow.data) 78 | return false; 79 | slow = slow.next; 80 | } 81 | return true; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ch2 Linked Lists/ques7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques7.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch2 Linked Lists/ques8.class -------------------------------------------------------------------------------- /ch2 Linked Lists/ques8.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | Node(){ 12 | //data =0; 13 | Node n =this; 14 | n =null; 15 | } 16 | 17 | void appendToTail(int d) 18 | { 19 | Node temp = new Node(d); 20 | Node n = this; 21 | while(n.next!=null) 22 | { 23 | n = n.next; 24 | } 25 | n.next= temp; 26 | } 27 | 28 | void output() 29 | { 30 | Node temp = this; 31 | while(temp!=null) 32 | { 33 | System.out.println(temp.data); 34 | temp = temp.next; 35 | } 36 | } 37 | } 38 | 39 | class ques8 40 | { 41 | public static void main(String[] args) { 42 | 43 | Scanner in = new Scanner(System.in); 44 | Node head = new Node(1); 45 | head.appendToTail(2); 46 | head.appendToTail(3); 47 | head.appendToTail(3); 48 | head.appendToTail(4); 49 | head.appendToTail(5); 50 | //head.output(); 51 | head.next.next.next.next.next.next= head; 52 | if(isLoopDetection(head)) 53 | System.out.println("Loop Present!"); 54 | else 55 | System.out.println("Loop not present!"); 56 | } 57 | 58 | static boolean isLoopDetection(Node head) 59 | { 60 | // Give condition for head 61 | 62 | Node fast = head, slow = head; 63 | fast = fast.next.next; 64 | 65 | while(fast.next!=null & fast!=null) 66 | { 67 | //System.out.println(slow.data); 68 | if(fast==slow) 69 | return true; 70 | slow = slow.next; 71 | fast = fast.next.next; 72 | } 73 | return false; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /ch2 Linked Lists/revrecur.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | 12 | void insert(Node head, int d) 13 | { 14 | if(head.next==null) 15 | { 16 | Node temp = new Node(d); 17 | head.next = temp; 18 | } 19 | else 20 | insert(head.next,d); 21 | } 22 | 23 | Node recur(Node head, Node newnode){ 24 | if(head==null) 25 | return null; 26 | newnode = recur(head.next, newnode); 27 | if(newnode!=null){ 28 | newnode.next=head; 29 | } 30 | return newnode; 31 | } 32 | 33 | void output() 34 | { 35 | Node n = this; 36 | while(n!=null) 37 | { 38 | System.out.println(n.data); 39 | n=n.next; 40 | } 41 | } 42 | } 43 | 44 | class revrecur 45 | { 46 | public static void main(String[] args) { 47 | 48 | Scanner in = new Scanner(System.in); 49 | int d = in.nextInt(); 50 | Node head = new Node(d); 51 | for(int i=0; i<4; i++) 52 | { 53 | d = in.nextInt(); 54 | head.insert(head,d); 55 | } 56 | head.output(); 57 | } 58 | Node head2 = recur(head,head2); 59 | } 60 | -------------------------------------------------------------------------------- /ch3 Stacks and Queues/Animal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/Animal.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/Myqueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/Myqueue.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/Stack$StackNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/Stack$StackNode.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/Stack.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/a.out -------------------------------------------------------------------------------- /ch3 Stacks and Queues/c++/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/c++/a.out -------------------------------------------------------------------------------- /ch3 Stacks and Queues/c++/queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct{ 4 | int r, f; 5 | int ele[100]; 6 | }queue; 7 | 8 | const int max = 5; 9 | 10 | void push(queue *s, int n) 11 | { 12 | if(s->f==max) 13 | printf("Overflow\n"); 14 | else 15 | s->ele[++(s->f)]=n; 16 | } 17 | 18 | int pop(queue *s) 19 | { 20 | if(s->r==max){ 21 | printf("Underflow"); 22 | return 0; } 23 | else 24 | return s->ele[++(s->r)];; 25 | } 26 | 27 | void output(queue s) 28 | { 29 | if(s.r==s.f) 30 | printf("Empty Queue!"); 31 | else 32 | { 33 | for(int i=(s.r)+1; i 2 | 3 | typedef struct 4 | { 5 | int top; 6 | int ele[100]; 7 | }stack; 8 | 9 | const int max = 5; 10 | 11 | void push(stack *s, int n) 12 | { 13 | if(s->top==max) 14 | printf("Overflow/n"); 15 | else 16 | s->ele[++(s->top)]=n; 17 | } 18 | 19 | int pop(stack *s) 20 | { 21 | if(s->top==-1) 22 | { 23 | printf("Underflow\n"); 24 | return 0; 25 | } 26 | else 27 | return s->ele[(s->top)--]; 28 | } 29 | 30 | void output(stack s) 31 | { 32 | while(s.top!=-1) 33 | { 34 | printf("%d\n",s.ele[s.top]); 35 | s.top--; 36 | } 37 | } 38 | 39 | int main() 40 | { 41 | stack mystack; 42 | mystack.top=-1; 43 | for(int i=0; i<5; i++) 44 | push(&mystack,i); 45 | output(mystack); 46 | pop(&mystack); 47 | pop(&mystack); 48 | pop(&mystack); 49 | pop(&mystack); 50 | pop(&mystack); 51 | pop(&mystack); 52 | //printf("%d\n",mystack.top); 53 | //output(mystack); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /ch3 Stacks and Queues/linkedlist.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/linkedlist.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/ques4.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class stack{ 3 | int top; 4 | int ele[]; 5 | 6 | stack() 7 | { 8 | top=-1; 9 | ele = new int[10]; 10 | } 11 | 12 | void push(int n) 13 | { 14 | if(this.top==5) 15 | System.out.println("Overflow"); 16 | else 17 | this.ele[++(this.top)]=n; 18 | } 19 | 20 | int pop() 21 | { 22 | if(this.top==-1) 23 | { System.out.println("Underflow"); return 0; } 24 | else 25 | { 26 | return this.ele[(this.top)--]; 27 | } 28 | } 29 | void output() 30 | { 31 | int val = this.top; 32 | while(val!=-1) 33 | { 34 | System.out.println(this.ele[val]); 35 | val--; 36 | } 37 | } 38 | int peek() 39 | { 40 | return this.top; 41 | } 42 | } 43 | 44 | class Myqueue{ 45 | stack s1, s2; 46 | 47 | Myqueue() 48 | { 49 | s1 = new stack(); 50 | s2 = new stack(); 51 | } 52 | 53 | void push(int n) 54 | { 55 | s1.push(n); 56 | } 57 | 58 | int pop() 59 | { 60 | int val=s1.pop(); 61 | s2.push(val); 62 | return val; 63 | } 64 | void output() 65 | { 66 | int up=s1.peek(); 67 | int down=s2.peek()+1; 68 | for(int i=down; down<=up; down++) 69 | System.out.println(s1.ele[down]); 70 | } 71 | } 72 | 73 | class ques4 74 | { 75 | public static void main(String[] args) { 76 | Myqueue queue = new Myqueue(); 77 | for(int i=0; i<5; i++) 78 | { 79 | queue.push(i); 80 | } 81 | queue.output(); 82 | queue.pop(); 83 | queue.output(); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/ques5.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques5.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class stack{ 3 | int top; 4 | int ele[]; 5 | 6 | stack() 7 | { 8 | top=-1; 9 | ele = new int[20]; 10 | } 11 | 12 | void push(int n) 13 | { 14 | if(this.top==5) 15 | System.out.println("Overflow"); 16 | else 17 | { 18 | int min = n, temp; 19 | int top = 0; 20 | if(top==-1) 21 | this.ele[++(this.top)]=min; 22 | else 23 | { 24 | while(top!=5) 25 | { 26 | if(min>this.ele[top]) 27 | { 28 | for(int i=this.top+1; i>top; i--) 29 | { 30 | this.ele[i]=this.ele[i-1]; 31 | } 32 | this.ele[top]=min; 33 | this.top++; 34 | //System.out.println(this.ele[top]); 35 | this.output(); 36 | System.out.println(); 37 | break; 38 | } 39 | top++; 40 | } 41 | } 42 | } 43 | 44 | } 45 | 46 | int pop() 47 | { 48 | if(this.top==-1) 49 | { System.out.println("Underflow"); return 0; } 50 | else 51 | { 52 | return this.ele[(this.top)--]; 53 | } 54 | } 55 | void output() 56 | { 57 | int val = this.top; 58 | while(val!=-1) 59 | { 60 | System.out.println(this.ele[val]); 61 | val--; 62 | } 63 | } 64 | int peek() 65 | { 66 | return this.top; 67 | } 68 | } 69 | 70 | class ques5{ 71 | public static void main(String[] args) { 72 | 73 | stack s = new stack(); 74 | //for(int i=0; i<8; i++) 75 | //s.push(i); 76 | s.push(2); 77 | s.push(4); 78 | s.push(3); 79 | s.push(1); 80 | s.output(); 81 | 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/ques6.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/ques6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class linkedlist 4 | { 5 | int timestamp; 6 | linkedlist next; 7 | 8 | linkedlist(int data) 9 | { 10 | timestamp=data; 11 | next=null; 12 | } 13 | linkedlist() 14 | {} 15 | 16 | void push(int data) 17 | { 18 | linkedlist newnode = new linkedlist(data); 19 | linkedlist temp = this; 20 | while(temp.next!=null) 21 | temp=temp.next; 22 | temp.next=newnode; 23 | } 24 | 25 | linkedlist pop() 26 | { 27 | if(this==null) 28 | {System.out.println("Empty list"); return null;} 29 | else{ 30 | System.out.println(this.timestamp+" removed !"); 31 | return this.next; 32 | } 33 | } 34 | 35 | void output() 36 | { 37 | linkedlist temp = this; 38 | while(temp!=null) 39 | { 40 | System.out.println(temp.timestamp); 41 | temp=temp.next; 42 | } 43 | } 44 | int peek() 45 | { 46 | return this.timestamp; 47 | } 48 | } 49 | 50 | // 1 is dog and 2 is cat 51 | 52 | class Animal{ 53 | linkedlist dog, cat; 54 | 55 | Animal() 56 | { 57 | dog = new linkedlist(); 58 | cat = new linkedlist(); 59 | } 60 | 61 | void push(int ch, int time) 62 | { 63 | if(ch==1) 64 | dog.push(time); 65 | else 66 | cat.push(time); 67 | } 68 | 69 | // Complete the rest 70 | 71 | } 72 | 73 | class ques6 74 | { 75 | public static void main(String[] args) { 76 | 77 | linkedlist node = new linkedlist(5); 78 | node.push(2); 79 | node.output(); 80 | node = node.pop(); 81 | node.output(); 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /ch3 Stacks and Queues/stackdemo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch3 Stacks and Queues/stackdemo.class -------------------------------------------------------------------------------- /ch3 Stacks and Queues/stackdemo.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Stack 4 | { 5 | public class StackNode{ 6 | private T data; 7 | private StackNode next; 8 | 9 | public StackNode(T item){ 10 | data =item; 11 | next=null; 12 | } 13 | } 14 | 15 | private StackNode top; 16 | 17 | public T pop(){ 18 | if(top==null) 19 | System.out.println("The Stack is empty"); 20 | else 21 | { 22 | T item = top.data; 23 | top = top.next; 24 | return item; 25 | } 26 | return null; 27 | } 28 | 29 | public void push(T item){ 30 | StackNode temp = new StackNode(item); 31 | temp.next=top; 32 | top=temp; 33 | } 34 | 35 | public T peek(){ 36 | return top.data; 37 | } 38 | } 39 | 40 | 41 | class stackdemo{ 42 | public static void main(String[] args) { 43 | 44 | Stack stack = new Stack(); 45 | Scanner in = new Scanner(System.in); 46 | int d; 47 | for(int i=0; i<5; i++) 48 | { 49 | d = in.nextInt(); 50 | stack.push(d); 51 | } 52 | for(int i=0; i<5; i++) 53 | { 54 | d = stack.pop(); 55 | System.out.println(d); 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /ch4 Trees and Graphs/Graph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/Graph.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/Linked.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/Linked.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/Tree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/Tree.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/dfs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/dfs.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/dfsbfs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/dfsbfs.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/graphexample.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch4 Trees and Graphs/graphexample.class -------------------------------------------------------------------------------- /ch4 Trees and Graphs/graphexample.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Node{ 3 | int data; 4 | int[] nodeChildren; 5 | int count; 6 | 7 | Node(int d){ 8 | data=d; 9 | count=0; 10 | nodeChildren = new int[10]; 11 | } 12 | 13 | void insertChildren(int[] children) 14 | { 15 | count=5; 16 | for(int i=0; i<5; i++) 17 | nodeChildren[i] = children[i]; 18 | } 19 | 20 | void outputChildren() 21 | { 22 | for(int i=0; i list; 21 | static int sum, noOfNodes; 22 | 23 | Tree(int d) 24 | { root = new node(d); 25 | list = new ArrayList(); 26 | sum=0; noOfNodes=0;} 27 | 28 | void push(int d) 29 | { 30 | node temp = root, parent = root; 31 | while(temp!=null) 32 | { 33 | parent = temp; 34 | if(temp.data > d) 35 | temp=temp.left; 36 | else 37 | temp=temp.right; 38 | } 39 | node newnode = new node(d); 40 | if(parent.data > d) 41 | parent.left = newnode; 42 | else 43 | parent.right = newnode; 44 | } 45 | 46 | void outputinorder(node n) 47 | { 48 | if(n!=null) 49 | { 50 | System.out.println(n.data); 51 | outputinorder(n.left); 52 | outputinorder(n.right); 53 | } 54 | } 55 | 56 | void countSum(node n, int s) 57 | { 58 | if(n!=null && sum 1) 25 | { 26 | mantisa-=1; 27 | temp+="1"; 28 | } 29 | else 30 | temp+="0"; 31 | } 32 | System.out.println(temp); 33 | 34 | //System.out.println(Integer.toString(n,2)); // prints 1100100 --binary representation 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | ques2 q = new ques2(); 40 | Scanner in = new Scanner(System.in); 41 | long num = Double.doubleToLongBits(15.20); 42 | q.output(num); 43 | } 44 | } 45 | */ 46 | -------------------------------------------------------------------------------- /ch5 bitmanipulation/ques3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch5 bitmanipulation/ques3.class -------------------------------------------------------------------------------- /ch5 bitmanipulation/ques4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch5 bitmanipulation/ques4.class -------------------------------------------------------------------------------- /ch5 bitmanipulation/ques4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class BitManipulation 3 | { 4 | int num; 5 | ArrayList seq; 6 | ArrayList seqarr; 7 | 8 | BitManipulation(int n) 9 | { num=n; 10 | seq = new ArrayList(); 11 | seqarr = new ArrayList();} 12 | 13 | void output() 14 | { 15 | int n = num; 16 | while(n!=0){ 17 | System.out.print(n%2+" "); 18 | n=n/2; 19 | } 20 | System.out.println(); 21 | } 22 | 23 | int getBit(int i){ 24 | if(((num & (1< seq = new ArrayList(); 48 | while(n>0) 49 | { 50 | seq.add(n%2); 51 | n>>=1; 52 | } 53 | 54 | for(int i=seq.size()-1; i>0; i--) 55 | { 56 | if(seq.get(i)==1 && seq.get(i-1)==0) 57 | { 58 | setBit(i-1); 59 | clearBit(i); 60 | break; 61 | } 62 | } 63 | //System.out.println(num); 64 | } 65 | } 66 | 67 | class ques4{ 68 | public static void main(String[] args) { 69 | BitManipulation bit = new BitManipulation(1755); 70 | bit.output(); 71 | bit.nextNumber(); 72 | bit.output(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /ch6 Maths and logic/Gender.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch6 Maths and logic/Gender.class -------------------------------------------------------------------------------- /ch6 Maths and logic/ch7 Object Oriented Programming/Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch6 Maths and logic/ch7 Object Oriented Programming/Employee.class -------------------------------------------------------------------------------- /ch6 Maths and logic/ch7 Object Oriented Programming/ques2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch6 Maths and logic/ch7 Object Oriented Programming/ques2.class -------------------------------------------------------------------------------- /ch6 Maths and logic/ch7 Object Oriented Programming/ques2.java: -------------------------------------------------------------------------------- 1 | class Employee{ 2 | String Employees[]; 3 | boolean isFree[]; 4 | int noOfEmployee; 5 | 6 | Employee(int n, boolean timeTable[], String temp[]) 7 | { 8 | noOfEmployee = n; 9 | Employees = temp; 10 | isFree = timeTable; 11 | } 12 | 13 | boolean dispatchCall(){ 14 | for(int i=0; i=0; i--) 22 | // System.out.print(bin[i]); 23 | // System.out.println(); 24 | for(i=9; i>lim; i--) 25 | bin[i]=0; 26 | // for(i=9; i>=0; i--) 27 | // System.out.print(bin[i]); 28 | // System.out.println(); 29 | int j=0; 30 | while(n>0) 31 | { bin[j]=n%2; 32 | n>>=1; 33 | j++;} 34 | //System.out.println(i); 35 | // for(i=9; i>=0; i--) 36 | // System.out.print(bin[i]); 37 | // System.out.println(); 38 | while(i>j) 39 | { 40 | bin[i]=0; 41 | i--; 42 | } 43 | for(i=9; i>=0; i--) 44 | System.out.print(bin[i]); 45 | } 46 | 47 | public static void main(String[] args) { 48 | 49 | ques10 q = new ques10(); 50 | int bin[]= new int[10]; 51 | q.getBinary(57,bin); 52 | 53 | // Now assign strip values and check 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ch6 Maths and logic/ques7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch6 Maths and logic/ques7.class -------------------------------------------------------------------------------- /ch6 Maths and logic/ques7.java: -------------------------------------------------------------------------------- 1 | class Gender{ 2 | int boys, girls; 3 | 4 | Gender() 5 | { boys=0; girls=0; } 6 | 7 | void setGender(int val, int ch) 8 | { 9 | if(ch==0) 10 | boys+=val; 11 | else 12 | girls+=val; 13 | } 14 | int getBoys() 15 | { return boys; } 16 | int getGirls() 17 | { return girls; } 18 | } 19 | 20 | class ques7{ 21 | 22 | static void checkgenderratio(int n){ 23 | Gender g = new Gender(); 24 | //System.out.println("Hello"); 25 | for(int i=0; i0.5) 39 | { 40 | //System.out.println(val); 41 | boys+=1; 42 | val = Math.random(); 43 | } 44 | //System.out.println(boys); 45 | g.setGender(boys,0); 46 | g.setGender(1,1); 47 | } 48 | 49 | public static void main(String[] args) { 50 | 51 | checkgenderratio(20); 52 | 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/Point.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/Point.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ShortestPath.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ShortestPath.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques1.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques1{ 3 | 4 | int findAllPossiblePaths(int n) 5 | { 6 | if(n==0) 7 | return 1; 8 | else if(n<0) 9 | return 0; 10 | 11 | return findAllPossiblePaths(n-1) + findAllPossiblePaths(n-2) + findAllPossiblePaths(n-3); 12 | } 13 | 14 | public static void main(String[] args) { 15 | 16 | ques1 q = new ques1(); 17 | System.out.println(q.findAllPossiblePaths(36)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques11.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques11.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques11.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | 4 | class ques11{ 5 | 6 | int pennies(int n, int sum){ 7 | if(n<1) 8 | return 0; 9 | 10 | sum+=quarters(n-1,sum); 11 | sum+=1; 12 | return sum; 13 | } 14 | 15 | int nickels(int n, int sum){ 16 | if(n<5) 17 | { 18 | System.out.println("No of Pennies "+pennies(n,0)); 19 | return 0; 20 | } 21 | 22 | sum+=quarters(n-5,sum); 23 | sum+=1; 24 | return sum; 25 | } 26 | 27 | int dimes(int n, int sum){ 28 | if(n<10) 29 | { 30 | System.out.println("No of Nickels "+nickels(n,0)); 31 | return 0; 32 | } 33 | 34 | sum+=quarters(n-10,sum); 35 | sum+=1; 36 | return sum; 37 | } 38 | 39 | int quarters(int n, int sum) 40 | { 41 | if(n<25) 42 | { 43 | System.out.println("No of Dimes "+dimes(n,0)); 44 | return 0; 45 | } 46 | 47 | sum+=quarters(n-25,sum); 48 | sum+=1; 49 | return sum; 50 | } 51 | 52 | public static void main(String[] args) { 53 | 54 | ques11 q = new ques11(); 55 | System.out.println("No of Quarters "+q.quarters(59,0)); 56 | 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques12.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques12.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques12.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques12{ 3 | 4 | int Grid_Size = 8; 5 | 6 | void placeQueens(int row, Integer[] columns, ArrayList results){ 7 | if(row==Grid_Size) 8 | results.add(columns.clone()); 9 | else{ 10 | for(int col=0; col < Grid_Size; col++){ 11 | if(checkValid(columns,row,col)){ 12 | columns[row] = col; 13 | placeQueens(row+1, columns, results); 14 | } 15 | } 16 | } 17 | } 18 | 19 | boolean checkValid(Integer[] columns, int row1, int column1){ 20 | for(int row2=0; row2 results = new ArrayList(); 40 | q.placeQueens(0,col,results); 41 | for(int i=0; i> getAllSubsets(ArrayList set, int index) 6 | { 7 | //Base Condition 8 | ArrayList> allSets; 9 | if(set.size()==index){ 10 | allSets = new ArrayList>(); 11 | allSets.add(new ArrayList()); 12 | }else{ 13 | allSets = getAllSubsets(set,index+1); 14 | int item = set.get(index); 15 | ArrayList> moreSubsets = new ArrayList>(); 16 | for(ArrayList subset : allSets) 17 | { 18 | ArrayList newSubset = new ArrayList(); 19 | newSubset.addAll(subset); 20 | newSubset.add(item); 21 | moreSubsets.add(newSubset); 22 | } 23 | allSets.addAll(moreSubsets); 24 | } 25 | return allSets; 26 | } 27 | 28 | public static void main(String[] args) { 29 | 30 | ques4 q = new ques4(); 31 | ArrayList ar = new ArrayList(); 32 | ar.add(1); 33 | ar.add(2); 34 | ar.add(3); 35 | ar.add(4); 36 | 37 | ArrayList> lol = q.getAllSubsets(ar,0); 38 | System.out.println(lol); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques5.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques5.java: -------------------------------------------------------------------------------- 1 | // This is the question you are searching for !!! 2 | 3 | import java.util.*; 4 | 5 | class ques5{ 6 | 7 | int getMinProducts(int a, int b){ 8 | int smaller = a < b ? a : b; 9 | int larger = a < b ? b : a; 10 | return minProducts(smaller, larger); 11 | } 12 | 13 | int minProducts(int smaller, int larger){ 14 | if(smaller==0) 15 | return 0; 16 | else if(smaller==1) 17 | return larger; 18 | 19 | int val = minProducts(smaller/2,larger); 20 | 21 | if(smaller%2==0) 22 | return val+val; 23 | else 24 | return val+val+larger; 25 | } 26 | 27 | public static void main(String[] args) { 28 | 29 | ques5 q = new ques5(); 30 | System.out.println(q.getMinProducts(5,6)); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques6.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques6.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class ques6{ 3 | void towerOfHanoi(int n, char source, char intermediate, char destination){ 4 | if(n==1){ 5 | System.out.println("Move disk "+n+" from "+source+" to "+destination); 6 | return; 7 | } 8 | towerOfHanoi(n-1,source, destination,intermediate); 9 | System.out.println("Move disk "+n+" from "+intermediate+" to "+destination); 10 | towerOfHanoi(n-1,intermediate,source,destination); 11 | } 12 | public static void main(String[] args) { 13 | 14 | ques6 q = new ques6(); 15 | int n=10; 16 | char[] a = new char[]{'A','B','C'}; 17 | q.towerOfHanoi(n,a[0],a[1],a[2]); 18 | } 19 | } 20 | 18001035466 21 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques7.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques7.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ques7{ 4 | 5 | String swap(String temp, int i, int j) 6 | { 7 | char[] arr = temp.toCharArray(); 8 | char t = arr[i]; 9 | arr[i] = arr[j]; 10 | arr[j] = t; 11 | return String.valueOf(arr); 12 | } 13 | 14 | void getpermutation(String temp, int l, int r) 15 | { 16 | if(l==r) 17 | System.out.println(temp); 18 | for(int i=l; i<=r; i++) 19 | { 20 | temp = swap(temp,l,i); 21 | getpermutation(temp,l+1,r); 22 | temp = swap(temp,l,i); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | ques7 q = new ques7(); 28 | String temp ="aac"; 29 | q.getpermutation(temp,0,temp.length()-1); // tofind length use .length() 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques8.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques8.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ques8{ 4 | 5 | String swap(String temp, int i, int j) 6 | { 7 | char[] arr = temp.toCharArray(); 8 | char t = arr[i]; 9 | arr[i] = arr[j]; 10 | arr[j] = t; 11 | return String.valueOf(arr); 12 | } 13 | 14 | boolean check(String temp, ArrayList memo){ 15 | for(int i=0; i memo) 22 | { 23 | if(l==r) 24 | { 25 | if(memo.size()==0) 26 | { 27 | System.out.println(temp); 28 | memo.add(temp); 29 | } 30 | else{ 31 | if(check(temp, memo)) 32 | { 33 | System.out.println(temp); 34 | memo.add(temp); 35 | } 36 | } 37 | } 38 | for(int i=l; i<=r; i++) 39 | { 40 | temp = swap(temp,l,i); 41 | getpermutation(temp,l+1,r,memo); 42 | temp = swap(temp,l,i); 43 | } 44 | } 45 | 46 | public static void main(String[] args) { 47 | ques8 q = new ques8(); 48 | String temp ="aac"; 49 | ArrayList memo = new ArrayList(); 50 | q.getpermutation(temp,0,temp.length()-1,memo); // tofind length use .length() 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/ch8 recursion and dynamic prog/ques9.class -------------------------------------------------------------------------------- /ch8 recursion and dynamic prog/ques9.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class ques9{ 4 | 5 | /*String checkString(String temp) 6 | { 7 | char[] arr = temp.toCharArray(); 8 | String newTemp = ""; 9 | for(int i=0; i printParen(int n, ArrayList temp){ 24 | 25 | if(n==0) 26 | temp.add(""); 27 | else{ 28 | ArrayList prev = printParen(n-1, temp); 29 | for(int j=0; j temp = new ArrayList(); 51 | temp=q.printParen(3,temp); 52 | System.out.println(temp); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /codechef/kconcat.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/codechef/kconcat.class -------------------------------------------------------------------------------- /codechef/kconcat.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | import java.io.*; 3 | 4 | class kconcat{ 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner (System.in); 7 | int T = sc.nextInt(); 8 | for(int i=0; i=0; i--) 24 | { 25 | long max=0; 26 | for(int j=0; j0) 40 | { 41 | if(scores[j]>=alice[i]) 42 | break; 43 | j--; 44 | } 45 | if(scores[j]==alice[i]) 46 | System.out.println(ranking[j]); 47 | else if (j==0) 48 | System.out.println("1"); 49 | else 50 | System.out.println(ranking[j]+1); 51 | } 52 | in.close(); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /hackerrank/Algorithm/implementation/gradingstudents.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | 9 | static int[] solve(int[] grades){ 10 | int len = grades.length; 11 | for(int i=0;i0){ 9 | for(int k=0; k0){ 46 | System.out.print(" "); 47 | j--; 48 | } 49 | j=i; 50 | while(j>=0){ 51 | System.out.print("#"); 52 | j--; 53 | } 54 | System.out.println(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /hackerrank/Dynamic Programming/fibo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/hackerrank/Dynamic Programming/fibo.class -------------------------------------------------------------------------------- /hackerrank/Dynamic Programming/fibo.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class fibo { 8 | public static void main(String args[] ) throws Exception { 9 | Scanner scn = new Scanner(System.in); 10 | BigInteger t1 = new BigInteger(scn.nextInt()+""); 11 | BigInteger t2 = new BigInteger(scn.nextInt()+""); 12 | int n = scn.nextInt(); 13 | 14 | BigInteger temp = new BigInteger("0"); 15 | for(int i=3;i<=n;i++){ 16 | temp = t2; 17 | t2 = t2.multiply(t2); 18 | t2 = t2.add(t1); 19 | t1 = temp; 20 | System.out.println(t2); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /hackerrank/Dynamic Programming/planepoint$Point.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/hackerrank/Dynamic Programming/planepoint$Point.class -------------------------------------------------------------------------------- /hackerrank/Dynamic Programming/planepoint.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/hackerrank/Dynamic Programming/planepoint.class -------------------------------------------------------------------------------- /hackerrank/Dynamic Programming/planepoint.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class planepoint{ 3 | static int n=5; 4 | class Point{ 5 | public int x,y; 6 | } 7 | static Point[][] point = new Point[n][n]; 8 | public static void main(String[] args) { 9 | for(int i=0; ib ? a: b; } 8 | 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | int n = sc.nextInt(); 12 | int k = sc.nextInt(); 13 | String S = sc.next(); 14 | char[] arr = S.toCharArray(); 15 | int i=0, j=0, diff=0; 16 | for(i=0, j=n-1; i hm = new HashMap(); 15 | int counter=0; 16 | for(int i=0; i<26; i++) 17 | { 18 | if(index[i]!=0 && hm.get(index[i])==null) 19 | hm.put(index[i],1); 20 | else if(hm.get(index[i])!=null) 21 | hm.put(index[i],hm.get(index[i])+1); 22 | if(hm.get(index[i]-1)!=null || hm.get(index[i]+1)!=null && index[i]!=0 || index[i]==1) 23 | counter++; 24 | } 25 | if(hm.size()==1 || (hm.size()==2 && counter==1)) 26 | System.out.println("YES"); 27 | else 28 | System.out.println("NO"); 29 | //System.out.println(counter); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /la.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class la{ 3 | public static void main(String[] args) { 4 | ArrayList a; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /mentorship/clone.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Node{ 4 | Node next; 5 | int data; 6 | 7 | Node(int d){ 8 | data = d; 9 | next = null; 10 | } 11 | 12 | void insert(Node head, int d) 13 | { 14 | if(head.next==null) 15 | { 16 | Node temp = new Node(d); 17 | head.next = temp; 18 | } 19 | else 20 | insert(head.next,d); 21 | } 22 | void output() 23 | { 24 | Node n = this; 25 | while(n!=null) 26 | { 27 | System.out.println(n.data); 28 | n=n.next; 29 | } 30 | } 31 | 32 | Node clonelist(Node head){ 33 | HashMap hm = new HashMap(); 34 | Node temp=head; 35 | while(temp!=null){ 36 | int i=1; 37 | hm.put(temp.data,i); 38 | i++; 39 | } 40 | while() 41 | int randomnumber=Math.Random(0,i-1); 42 | 43 | } 44 | 45 | } 46 | 47 | class clone 48 | { 49 | public static void main(String[] args) { 50 | 51 | Scanner in = new Scanner(System.in); 52 | int d = in.nextInt(); 53 | Node head = new Node(d); 54 | for(int i=0; i<4; i++) 55 | { 56 | d = in.nextInt(); 57 | head.insert(head,d); 58 | } 59 | head.output(); 60 | } 61 | Node head2 = recur(head,head2); 62 | } 63 | -------------------------------------------------------------------------------- /microsoft interview/Tree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/Tree.class -------------------------------------------------------------------------------- /microsoft interview/la.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/la.class -------------------------------------------------------------------------------- /microsoft interview/la.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class la{ 3 | public static void main(String[] args) { 4 | ArrayList> a = new ArrayList>(); 5 | ArrayList b = new ArrayList(); 6 | b.add(0); 7 | a.add(b); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /microsoft interview/node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/node.class -------------------------------------------------------------------------------- /microsoft interview/ques1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques1.class -------------------------------------------------------------------------------- /microsoft interview/ques10.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques10.class -------------------------------------------------------------------------------- /microsoft interview/ques10.java: -------------------------------------------------------------------------------- 1 | /* 2 | Print nodes at k distance from root 3 | Given a root of a tree, and an integer k. Print all the nodes which are at k distance from root. 4 | 5 | For example, in the below tree, 4, 5 & 8 are at distance 2 from root. 6 | */ 7 | import java.util.*; 8 | 9 | class node{ 10 | int data; 11 | node left; 12 | node right; 13 | node(int d){ 14 | data=d; 15 | left=null; 16 | right=null; 17 | } 18 | } 19 | 20 | class Tree{ 21 | 22 | node head; 23 | 24 | Tree(int d){ 25 | head = new node(d); 26 | } 27 | 28 | void push(int d) 29 | { 30 | node temp = head, parent=head; 31 | while(temp!=null){ 32 | parent=temp; 33 | if(temp.data>d) 34 | temp=temp.left; 35 | else 36 | temp=temp.right; 37 | } 38 | node newnode = new node(d); 39 | if(parent.data>d) 40 | parent.left=newnode; 41 | else 42 | parent.right=newnode; 43 | } 44 | 45 | void output(node temp){ 46 | if(temp!=null){ 47 | System.out.println(temp.data); 48 | output(temp.left); 49 | output(temp.right); 50 | } 51 | } 52 | 53 | void printkth(node temp, int k){ 54 | 55 | if(temp==null) return; 56 | if(k==0){ 57 | System.out.println(temp.data); 58 | return; 59 | } 60 | printkth(temp.left, k-1); 61 | printkth(temp.right, k-1); 62 | } 63 | } 64 | 65 | class ques10{ 66 | public static void main(String[] args) { 67 | 68 | Tree t = new Tree(5); 69 | t.push(2); 70 | t.push(3); 71 | t.push(4); 72 | t.push(6); 73 | 74 | t.printkth(t.head, 2); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /microsoft interview/ques11.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques11.class -------------------------------------------------------------------------------- /microsoft interview/ques11.java: -------------------------------------------------------------------------------- 1 | /* 2 | Square root of an integer 3 | Given an integer x, find square root of it. If x is not a perfect square, then return floor(√x). 4 | */ 5 | import java.util.*; 6 | class ques11{ 7 | 8 | static int findsqareroot(int n){ 9 | 10 | int start = 1, end = n/2, ans = 0; 11 | while(start<=end){ 12 | int mid = (start+end)/2; 13 | if(mid*mid==n) 14 | return mid; 15 | if(mid*midd) 35 | temp=temp.left; 36 | else 37 | temp=temp.right; 38 | } 39 | 40 | node newnode = new node(d); 41 | if(parent.data>d) 42 | parent.left=newnode; 43 | else 44 | parent.right=newnode; 45 | } 46 | 47 | void output(node temp){ 48 | if(temp!=null){ 49 | System.out.println(temp.data); 50 | output(temp.left); 51 | output(temp.right); 52 | } 53 | } 54 | 55 | boolean identicalTrees(node temp1, node temp2){ 56 | 57 | if(temp1!=null && temp2!=null) 58 | return true; 59 | 60 | if(temp1!=null && temp2!=null) 61 | return (temp1.data == temp2.data 62 | && identicalTrees(temp1.left,temp2.left) 63 | && identicalTrees(temp1.right,temp2.right)); 64 | 65 | return false; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /microsoft interview/ques13.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques13.class -------------------------------------------------------------------------------- /microsoft interview/ques13.java: -------------------------------------------------------------------------------- 1 | /* 2 | Reverse a linked list 3 | Given pointer to the head node of a linked list, the task is to reverse the linked list. 4 | */ 5 | import java.util.*; 6 | 7 | class node{ 8 | int data; 9 | node next; 10 | 11 | node(int d){ 12 | data=d; 13 | next=null; 14 | } 15 | 16 | void push(int d){ 17 | node temp = this; 18 | while(temp.next!=null) 19 | temp = temp.next; 20 | node newnode = new node(d); 21 | temp.next = newnode; 22 | } 23 | 24 | node reverse(){ 25 | node current=this, prev=null, next=null; 26 | while(current!=null){ 27 | next = current.next; 28 | current.next = prev; 29 | prev = current; 30 | current = next; 31 | } 32 | return prev; 33 | } 34 | void output() 35 | { 36 | node temp = this; 37 | while(temp!=null){ 38 | System.out.println(temp.data); 39 | temp = temp.next; 40 | } 41 | } 42 | } 43 | class ques13{ 44 | public static void main(String[] args) { 45 | 46 | node head = new node(0); 47 | for(int i=1; i<4; i++) 48 | head.push(i); 49 | head.output(); 50 | head = head.reverse(); 51 | head.output(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /microsoft interview/ques14.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques14.class -------------------------------------------------------------------------------- /microsoft interview/ques15.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques15.class -------------------------------------------------------------------------------- /microsoft interview/ques15.java: -------------------------------------------------------------------------------- 1 | /* 2 | Converting Decimal Number lying between 1 to 3999 to Roman Numerals 3 | Given a number, find its corresponding Roman numeral. 4 | */ 5 | import java.util.*; 6 | class ques15{ 7 | 8 | public static void main(String[] args) { 9 | 10 | Scanner in = new Scanner(System.in); 11 | int n = in.nextInt(); 12 | String temp=""; 13 | String[] roman = new String[]{"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; 14 | //System.out.println("debug1"); 15 | while(n!=0){ 16 | //System.out.println("debug1"); 17 | if(n>=1000){ 18 | temp+=roman[12]; 19 | n-=1000; 20 | } 21 | else if(n>=900){ 22 | temp+=roman[11]; 23 | n-=900; 24 | } 25 | else if(n>=500){ 26 | temp+=roman[10]; 27 | n-=500; 28 | } 29 | else if(n>=400){ 30 | temp+=roman[9]; 31 | n-=400; 32 | } 33 | else if(n>=100){ 34 | temp+=roman[8]; 35 | n-=100; 36 | } 37 | else if(n>=90){ 38 | temp+=roman[7]; 39 | n-=90; 40 | } 41 | else if(n>=50){ 42 | temp+=roman[6]; 43 | n-=50; 44 | } 45 | else if(n>=40){ 46 | temp+=roman[5]; 47 | n-=40; 48 | } 49 | else if(n>=10){ 50 | temp+=roman[4]; 51 | n-=10; 52 | } 53 | else if(n>=9){ 54 | temp+=roman[3]; 55 | n-=9; 56 | } 57 | else if(n>=5){ 58 | temp+=roman[2]; 59 | n-=5; 60 | } 61 | else if(n>=4){ 62 | temp+=roman[1]; 63 | n-=4; 64 | } 65 | else if(n>=1){ 66 | temp+=roman[0]; 67 | n-=1; 68 | } 69 | } 70 | System.out.println(temp); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /microsoft interview/ques16.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques16.class -------------------------------------------------------------------------------- /microsoft interview/ques17.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques17.class -------------------------------------------------------------------------------- /microsoft interview/ques17.java: -------------------------------------------------------------------------------- 1 | /* 2 | Inplace rotate square matrix by 90 degrees | Set 1 3 | Given an square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space. 4 | */ 5 | import java.util.*; 6 | 7 | class ques17{ 8 | 9 | static void rotate(int[][] arr){ 10 | 11 | // Rotation only possible in square matrix so check for non-square matrix 12 | int n = arr.length; 13 | for(int x=0; xd) 29 | temp=temp.left; 30 | else 31 | temp=temp.right; 32 | } 33 | 34 | node newnode = new node(d); 35 | if(parent.data>d) 36 | parent.left=newnode; 37 | else 38 | parent.right=newnode; 39 | } 40 | 41 | void output(node temp){ 42 | if(temp!=null){ 43 | System.out.println(temp.data); 44 | output(temp.left); 45 | output(temp.right); 46 | } 47 | } 48 | 49 | int toSumTree(node n){ 50 | if(n==null) 51 | return 0; 52 | 53 | int old_val = n.data; 54 | 55 | n.data = toSumTree(n.left) + toSumTree(n.right); 56 | 57 | return n.data + old_val; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /microsoft interview/ques21.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques21.class -------------------------------------------------------------------------------- /microsoft interview/ques3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques3.class -------------------------------------------------------------------------------- /microsoft interview/ques3.java: -------------------------------------------------------------------------------- 1 | /* 2 | k largest(or smallest) elements in an array | added Min Heap method 3 | Question: Write an efficient program for printing k largest elements in an array. Elements in array can be in any order. 4 | 5 | For example, if given array is [1, 23, 12, 9, 30, 2, 50] and you are asked for the largest 3 elements i.e., k = 3 then your program should print 50, 30 and 23. 6 | */ 7 | import java.util.*; 8 | 9 | class ques3 10 | { 11 | 12 | public static void main(String[] args) { 13 | 14 | int[] arr = new int[]{1,23,12,9,30,2,50}; 15 | int maxthele=0, i=0; 16 | HashMap hm = new HashMap(); 17 | hm.put(maxthele,arr[i]); 18 | for(i=1; ihm.get(maxthele)) 20 | { 21 | maxthele++; 22 | hm.put(maxthele,arr[i]); 23 | } 24 | System.out.println(hm.get(maxthele-2)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /microsoft interview/ques4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques4.class -------------------------------------------------------------------------------- /microsoft interview/ques4.java: -------------------------------------------------------------------------------- 1 | /* 2 | Longest Even Length Substring such that Sum of First and Second Half is same 3 | Given a string ‘str’ of digits, find length of the longest substring of ‘str’, 4 | such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits. 5 | */ 6 | import java.util.*; 7 | 8 | class ques4{ 9 | 10 | static void findmax(String str){ 11 | 12 | int n = str.length(), max=0, len=0; 13 | char[] arr = str.toCharArray(); 14 | for(int i=0; i=0 && r<=n-1){ 18 | lsum+=arr[l]-'0'; 19 | rsum+=arr[r]-'0'; 20 | if(lsum==rsum) 21 | if(max0; i=i/2){ 12 | if((n&i)==0) 13 | System.out.print(0); 14 | else 15 | System.out.print(1); 16 | } 17 | } 18 | 19 | public static void main(String[] args) { 20 | bin(7); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /microsoft interview/ques7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques7.class -------------------------------------------------------------------------------- /microsoft interview/ques7.java: -------------------------------------------------------------------------------- 1 | /* 2 | Largest Rectangular Area in a Histogram | Set 2 3 | Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have same width and the width is 1 unit. 4 | 5 | For example, consider the following histogram with 7 bars of heights {6, 2, 5, 4, 5, 1, 6}. The largest possible rectangle possible is 12 (see the below figure, the max area rectangle is highlighted in red) 6 | */ 7 | import java.util.*; 8 | class ques7{ 9 | 10 | static void largestRectangularArea(int[] arr){ 11 | Stack stack = new Stack(); 12 | int i=0; 13 | int max_area=0, tp=0, area=0; 14 | while(i= low){ 11 | int mid = low + (high-low)/2; 12 | 13 | if((mid==0 || arr[mid-1]==0) && arr[mid]==1) 14 | return mid; 15 | else if(arr[mid]==0) 16 | return find(arr,(mid+1),high); 17 | else 18 | return find(arr,low,(mid-1)); 19 | } 20 | return -1; 21 | } 22 | 23 | static void maxones(int arr[][]){ 24 | 25 | int max_row_index=0, max=-1; 26 | int i, index; 27 | int r = arr.length, c = arr[0].length; 28 | for(i=0; i max){ 32 | max = c - index; 33 | max_row_index = i; 34 | } 35 | } 36 | System.out.println(max_row_index); 37 | } 38 | 39 | public static void main(String[] args) { 40 | 41 | int[][] arr = new int[][]{ 42 | {0,0,0,1}, 43 | {0,0,1,1}, 44 | {1,1,1,1}, 45 | {0,0,0,0} 46 | }; 47 | 48 | maxones(arr); 49 | 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /microsoft interview/ques9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/microsoft interview/ques9.class -------------------------------------------------------------------------------- /microsoft interview/ques9.java: -------------------------------------------------------------------------------- 1 | /* 2 | Write an Efficient Function to Convert a Binary Tree into its Mirror Tree 3 | Mirror of a Tree: Mirror of a Binary Tree T is another Binary Tree M(T) with left and right children of all non-leaf nodes interchanged. 4 | */ 5 | import java.util.*; 6 | 7 | class node{ 8 | int data; 9 | node left; 10 | node right; 11 | node(int d){ 12 | data=d; 13 | left=null; 14 | right=null; 15 | } 16 | } 17 | 18 | class Tree{ 19 | 20 | node head; 21 | 22 | Tree(int d){ 23 | head = new node(d); 24 | } 25 | 26 | void push(int d) 27 | { 28 | node temp = head, parent=head; 29 | while(temp!=null){ 30 | parent=temp; 31 | if(temp.data>d) 32 | temp=temp.left; 33 | else 34 | temp=temp.right; 35 | } 36 | node newnode = new node(d); 37 | if(parent.data>d) 38 | parent.left=newnode; 39 | else 40 | parent.right=newnode; 41 | } 42 | 43 | void output(node temp){ 44 | if(temp!=null){ 45 | System.out.println(temp.data); 46 | output(temp.left); 47 | output(temp.right); 48 | } 49 | } 50 | 51 | void mirror(node temp){ 52 | if(temp == null) 53 | return; 54 | else{ 55 | node temp2; 56 | mirror(temp.left); 57 | mirror(temp.right); 58 | temp2 = temp.left; 59 | temp.left = temp.right; 60 | temp.right = temp2; 61 | } 62 | } 63 | } 64 | 65 | class ques9{ 66 | public static void main(String[] args) { 67 | 68 | Tree t = new Tree(5); 69 | t.push(2); 70 | t.push(3); 71 | t.push(4); 72 | t.push(6); 73 | //System.out.println(t.head.left.left.data); 74 | t.output(t.head); 75 | t.mirror(t.head); 76 | t.output(t.head); 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/node.class -------------------------------------------------------------------------------- /palindrome.java: -------------------------------------------------------------------------------- 1 | import java.util.io.*; 2 | 3 | class Node{ 4 | int data; 5 | Node next; 6 | Node(int d){ 7 | data = d; 8 | next = null; 9 | } 10 | void appendToTail(int d){ 11 | Node newnode = new Node(d); 12 | 13 | } 14 | } 15 | 16 | class palindrome{ 17 | public static void main(String[] args) { 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /permutestrings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/permutestrings.class -------------------------------------------------------------------------------- /permutestrings.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class permutestrings 4 | { 5 | 6 | static void permutation(String str) 7 | { 8 | permutation(str,""); 9 | } 10 | 11 | static void permutation(String str, String prefix) 12 | { 13 | if(str.length()==0) 14 | System.out.println(prefix); 15 | else{ 16 | for(int i=0; i map = new HashMap(); 10 | 11 | for(int c=0; c 0) 14 | { 15 | for (int k = 0; k =1; i--) 8 | if(arr[i-1]arr[j+1]) 14 | { break;} 15 | 16 | int temp = arr[i]; 17 | arr[i] = arr[j]; 18 | arr[j] = temp; 19 | 20 | for(int l=j, k=n-1; l=arr[j]) 11 | count[i]+=count[i-arr[j]]; 12 | return count[n]; 13 | } 14 | 15 | public static void main(String[] args) { 16 | 17 | int n=14; 18 | int[] arr = new int[]{5,6,7,8}; 19 | System.out.println(countSum(arr,n)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /samsung/Combinatorial/countnoofString.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/countnoofString.class -------------------------------------------------------------------------------- /samsung/Combinatorial/countnoofString.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class countnoofString{ 3 | 4 | static int possibleString(int n, int r, int b, int g){ 5 | int fact[] = new int[n+1]; 6 | fact[0]=1; 7 | for(int i=1; i<=n; i++) 8 | fact[i] = fact[i-1]*i; 9 | int left = n -(r+g+b), sum=0; 10 | 11 | for(int i=0; i<=left; i++){ 12 | for(int j=0; j<=left-i; j++){ 13 | int k = left - (i+j); 14 | sum = sum +fact[n]/(fact[i+r]*fact[j+b]*fact[k+g]); 15 | } 16 | } 17 | return sum; 18 | } 19 | public static void main(String[] args) { 20 | int n=5, b=2, r=1, g=1; 21 | System.out.println(possibleString(n,r,b,g)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /samsung/Combinatorial/cutnintok.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/cutnintok.class -------------------------------------------------------------------------------- /samsung/Combinatorial/findSumallPer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/findSumallPer.class -------------------------------------------------------------------------------- /samsung/Combinatorial/findSumallPer.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class findSumallPer{ 4 | 5 | static int sum=0; 6 | 7 | static void getPermutation(String arr, int l, int r){ 8 | 9 | if(l==r) 10 | { sum+=Integer.parseInt(arr); 11 | return;} 12 | 13 | for(int i=l; i<=r; i++) 14 | { 15 | arr=swap(arr,l,i); 16 | getPermutation(arr,l+1,r); 17 | arr=swap(arr,l,i); 18 | } 19 | } 20 | 21 | static String swap(String temp, int l, int i){ 22 | char[] arr = temp.toCharArray(); 23 | char t = arr[l]; 24 | arr[l]=arr[i]; 25 | arr[i]=t; 26 | return String.valueOf(arr); 27 | } 28 | 29 | public static void main(String[] args) { 30 | String temp = "123"; 31 | findSumallPer f = new findSumallPer(); 32 | f.getPermutation(temp,0, temp.length()-1); 33 | System.out.println(f.sum); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /samsung/Combinatorial/kdifferencepermutation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/kdifferencepermutation.class -------------------------------------------------------------------------------- /samsung/Combinatorial/kthsmallestnumber.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/kthsmallestnumber.class -------------------------------------------------------------------------------- /samsung/Combinatorial/kthsmallestnumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class kthsmallestnumber{ 3 | 4 | static void permute(String str, int l, int r, int k){ 5 | if(l==r && k==0) 6 | System.out.println(str); 7 | else{ 8 | for(int i=l; i<=r; i++){ 9 | str = swap(str,l,i); 10 | permute(str,l+1,r,k); 11 | str = swap(str,l,i); 12 | k--; 13 | } 14 | } 15 | } 16 | 17 | static String swap(String str, int i, int j){ 18 | char temp; 19 | char[] charArray = str.toCharArray(); 20 | temp = charArray[i]; 21 | charArray[i] = charArray[j]; 22 | charArray[j] = temp; 23 | return String.valueOf(charArray); 24 | } 25 | 26 | public static void main(String[] args) { 27 | String num="3412"; 28 | int k=2; 29 | char[] arr = num.toCharArray(); 30 | Arrays.sort(arr); 31 | num=String.valueOf(arr); 32 | permute(num,0,num.length()-1,k); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /samsung/Combinatorial/largeFactorial.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/largeFactorial.class -------------------------------------------------------------------------------- /samsung/Combinatorial/largeFactorial.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class largeFactorial{ 3 | 4 | static int multiply(int n, int[] arr, int res_size){ 5 | int carry=0, i; 6 | for(i=0; i=0; i--) 27 | System.out.print(arr[i]); 28 | return arr; 29 | } 30 | public static void main(String[] args) { 31 | int[] arr = largeFactorial(100); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /samsung/Combinatorial/nextLexicographic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Combinatorial/nextLexicographic.class -------------------------------------------------------------------------------- /samsung/Combinatorial/nextLexicographic.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | // 1) scan from right to left, find the first element that is less than its previous one. 4 | // 2) scan from right to left, find the first element that is greater than p. 5 | // 3) swap p and q 6 | // 4) reverse elements [p+1, nums.length] 7 | 8 | class nextLexicographic{ 9 | 10 | static void nextLexicographic(int[] arr){ 11 | 12 | int n = arr.length, i, j; 13 | for(i=n-1; i>=1; i--) 14 | if(arr[i-1]arr[j+1]) 20 | { break;} 21 | 22 | int temp = arr[i]; 23 | arr[i] = arr[j]; 24 | arr[j] = temp; 25 | 26 | for(int l=j, k=n-1; l=1; i--) 8 | if(arr[i-1]arr[j+1]) 14 | { break;} 15 | 16 | int temp = arr[i]; 17 | arr[i] = arr[j]; 18 | arr[j] = temp; 19 | 20 | for(int l=j, k=n-1; l[] adj; 5 | Graph(int v){ 6 | V=v; 7 | adj = new LinkedList[V]; 8 | for(int i=0; i(); 10 | } 11 | void addEdge(int v, int w){ 12 | adj[v].add(w); 13 | } 14 | void bfs(int v){ 15 | LinkedList Queue = new LinkedList(); 16 | boolean[] visited = new boolean[V]; 17 | visited[v]=true; 18 | Queue.add(v); 19 | 20 | while(Queue.size()!=0){ 21 | int i=Queue.poll(); 22 | System.out.print(i+" "); 23 | Iterator iterator = adj[i].listIterator(); 24 | while(iterator.hasNext()){ 25 | int n = iterator.next(); 26 | if(!visited[n]){ 27 | Queue.add(n); 28 | visited[n]=true; 29 | } 30 | } 31 | } 32 | } 33 | } 34 | 35 | class bfs{ 36 | public static void main(String[] args) { 37 | Graph g = new Graph(6); 38 | g.addEdge(0,1); 39 | g.addEdge(0,3); 40 | g.addEdge(1,4); 41 | g.addEdge(1,2); 42 | g.addEdge(2,5); 43 | g.addEdge(5,0); 44 | g.bfs(0); 45 | } 46 | } 47 | 48 | /* 49 | Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method 50 | */ 51 | -------------------------------------------------------------------------------- /samsung/Graph/bidirectional.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/bidirectional.class -------------------------------------------------------------------------------- /samsung/Graph/bipartite.java: -------------------------------------------------------------------------------- 1 | /* 2 | Check whether a given graph is Bipartite or not 3 | A Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U. 4 | In other words, for every edge (u, v), either u belongs to U and v to V, or u belongs to V and v to U. We can also say that there is no edge that connects vertices of same set. 5 | 6 | A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color. 7 | Note that it is possible to color a cycle graph with even cycle using two colors 8 | */ 9 | -------------------------------------------------------------------------------- /samsung/Graph/clone.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/clone.class -------------------------------------------------------------------------------- /samsung/Graph/detectcycle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/detectcycle.class -------------------------------------------------------------------------------- /samsung/Graph/detectcycle.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Graph{ 3 | int V; 4 | LinkedList[] adj; 5 | Graph(int v){ 6 | V=v; 7 | adj = new LinkedList[v]; 8 | for(int i=0; i(); 10 | } 11 | void addEdge(int v, int w){ 12 | adj[v].add(w); 13 | adj[w].add(v); 14 | } 15 | boolean isCyclic() 16 | { 17 | boolean[] visited = new boolean[V]; 18 | for (int i = 0; i < V; i++) 19 | visited[i] = false; 20 | for(int i=0; i iterator = adj[v].iterator(); 28 | visited[v]=true; 29 | while(iterator.hasNext()) 30 | { 31 | int n = iterator.next(); 32 | if(!visited[n]) 33 | if(isCyclicUtil(n,visited,v)) 34 | return true; 35 | 36 | if(n!=parent) 37 | return true; 38 | } 39 | return false; 40 | } 41 | } 42 | 43 | class detectcycle{ 44 | public static void main(String[] args) { 45 | /*Graph g1 = new Graph(5); 46 | g1.addEdge(1, 0); 47 | g1.addEdge(0, 2); 48 | g1.addEdge(2, 0); 49 | g1.addEdge(0, 3); 50 | g1.addEdge(3, 4); 51 | if (g1.isCyclic()) 52 | System.out.println("Graph contains cycle"); 53 | else 54 | System.out.println("Graph doesn't contains cycle");*/ 55 | 56 | Graph g2 = new Graph(3); 57 | g2.addEdge(0, 1); 58 | g2.addEdge(1, 2); 59 | if (g2.isCyclic()) 60 | System.out.println("Graph contains cycle"); 61 | else 62 | System.out.println("Graph doesn't contains cycle"); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /samsung/Graph/dfs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/dfs.class -------------------------------------------------------------------------------- /samsung/Graph/dfs.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Graph{ 4 | int V; 5 | LinkedList[] adj; 6 | 7 | Graph(int V){ 8 | this.V=V; 9 | adj = new LinkedList[V]; 10 | for(int i=0; i(); } 12 | 13 | void addEdge(int v, int w){ 14 | adj[v].add(w); 15 | } 16 | 17 | void Dfs(int src){ 18 | boolean[] visited = new boolean[V]; 19 | for(int i=0; i iterator = adj[i].iterator(); 28 | while(iterator.hasNext()){ 29 | int v = iterator.next(); 30 | if(!visited[v]) 31 | DfsUtil(visited,v); 32 | } 33 | } 34 | } 35 | 36 | class dfs{ 37 | public static void main(String[] args) { 38 | Graph g = new Graph(6); 39 | g.addEdge(0,1); 40 | g.addEdge(0,3); 41 | g.addEdge(1,4); 42 | g.addEdge(1,2); 43 | g.addEdge(2,5); 44 | g.addEdge(5,0); 45 | g.Dfs(0); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /samsung/Graph/dfsiterative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/dfsiterative.class -------------------------------------------------------------------------------- /samsung/Graph/dfsiterative.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Graph{ 3 | int V; 4 | LinkedList[] adj; 5 | 6 | Graph(int V){ 7 | this.V = V; 8 | adj = new LinkedList[V]; 9 | for(int i=0; i(); 11 | } 12 | 13 | void addEdge(int v, int w){ 14 | adj[v].add(w); 15 | } 16 | 17 | void dfsiterative(int v){ 18 | boolean[] visited = new boolean[V]; 19 | Stack stack = new Stack(); 20 | stack.push(v); 21 | while(!stack.empty()){ 22 | v=stack.peek(); 23 | stack.pop(); 24 | if(!visited[v]) 25 | { 26 | System.out.print(v+" "); 27 | visited[v]=true; 28 | } 29 | Iterator itr = adj[v].iterator(); 30 | while(itr.hasNext()){ 31 | int s = itr.next(); 32 | if(!visited[s]) 33 | stack.push(s); 34 | } 35 | } 36 | } 37 | } 38 | 39 | class dfsiterative{ 40 | public static void main(String[] args) { 41 | Graph g = new Graph(5); 42 | 43 | g.addEdge(1, 0); 44 | g.addEdge(0, 2); 45 | g.addEdge(2, 1); 46 | g.addEdge(0, 3); 47 | g.addEdge(1, 4); 48 | 49 | g.dfsiterative(0); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /samsung/Graph/dfsmatrix.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/dfsmatrix.class -------------------------------------------------------------------------------- /samsung/Graph/dfsmatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class dfsmatrix{ 3 | 4 | int V; 5 | 6 | dfsmatrix(int V){ 7 | V=this.V; 8 | } 9 | 10 | 11 | void dfs(int[][] edges, int v, boolean[] visited){ 12 | System.out.print(v+" "); 13 | visited[v]=true; 14 | for(int i=0; i[] adj; 5 | Graph(int v){ 6 | V=v; 7 | adj = new LinkedList[v]; 8 | for(int i=0; i(); 10 | } 11 | void addEdge(int v, int w){ 12 | adj[v].add(w); 13 | adj[w].add(v); 14 | } 15 | int longestCable(){ 16 | int max_len = Integer.MIN_VAL; 17 | for(int i=0; i[] adj; 5 | 6 | Graph(int v){ 7 | V=v; 8 | adj = new LinkedList[v]; 9 | for(int i=0; i(); 11 | } 12 | 13 | void addEdge(int v, int w){ 14 | adj[v].add(w); 15 | } 16 | 17 | void printAll(int src, int dest){ 18 | boolean[] visited = new boolean[V]; 19 | ArrayList path = new ArrayList(); 20 | printAllUtil(src,dest,visited,path); 21 | } 22 | 23 | void printAllUtil(int src, int dest, boolean[] visited, ArrayList path){ 24 | 25 | visited[src]=true; 26 | path.add(src); 27 | 28 | if(src==dest) 29 | System.out.println(path); 30 | 31 | Iterator iterator = adj[src].iterator(); 32 | while(iterator.hasNext()){ 33 | int n = iterator.next(); 34 | if(!visited[n]) 35 | printAllUtil(n,dest,visited,path); 36 | } 37 | visited[src]=false; 38 | } 39 | } 40 | 41 | class printAllPaths{ 42 | public static void main(String[] args) { 43 | Graph g = new Graph(6); 44 | g.addEdge(0,1); 45 | g.addEdge(0,3); 46 | g.addEdge(1,4); 47 | g.addEdge(1,2); 48 | g.addEdge(2,5); 49 | g.addEdge(5,0); 50 | g.printAll(0,5); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /samsung/Graph/topologicalsort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Graph/topologicalsort.class -------------------------------------------------------------------------------- /samsung/Graph/topologicalsort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Graph{ 3 | int V; 4 | LinkedList[] adj; 5 | Graph(int V){ 6 | this.V=V; 7 | adj = new LinkedList[V]; 8 | for(int i=0; i(); 10 | } 11 | void addEdge(int v, int w){ 12 | adj[v].add(w); 13 | } 14 | void topologicalSort(){ 15 | Stack stack = new Stack(); 16 | boolean[] visited = new boolean[V]; 17 | for(int i=0; i iterator = adj[v].iterator(); 31 | while(iterator.hasNext()){ 32 | int n = iterator.next(); 33 | if(!visited[n]){ 34 | topologicalSortUtil(n,visited,stack); 35 | } 36 | } 37 | stack.push(new Integer(v)); 38 | } 39 | } 40 | 41 | class topologicalsort{ 42 | public static void main(String[] args) { 43 | Graph g = new Graph(6); 44 | g.addEdge(5, 2); 45 | g.addEdge(5, 0); 46 | g.addEdge(4, 0); 47 | g.addEdge(4, 1); 48 | g.addEdge(2, 3); 49 | g.addEdge(3, 1); 50 | g.topologicalSort(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /samsung/NGE.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/NGE.class -------------------------------------------------------------------------------- /samsung/NGE.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class NGE{ 4 | 5 | static void findNGE(int[] arr){ 6 | Stack s = new Stack(); 7 | s.push(arr[0]); 8 | int element, next=0; 9 | for(int i=1; inext) 20 | s.push(element); 21 | } 22 | s.push(next); 23 | } 24 | while(!s.isEmpty()){ 25 | element = s.pop(); 26 | next =-1; 27 | System.out.println(element+" "+next); 28 | } 29 | } 30 | 31 | public static void main(String[] args) { 32 | 33 | int[] arr = new int[]{12,-8,-3,2,56,2,21}; 34 | findNGE(arr); 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /samsung/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Node.class -------------------------------------------------------------------------------- /samsung/SearchMazeBFS$Point.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/SearchMazeBFS$Point.class -------------------------------------------------------------------------------- /samsung/SearchMazeBFS$QueueNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/SearchMazeBFS$QueueNode.class -------------------------------------------------------------------------------- /samsung/SearchMazeBFS.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/SearchMazeBFS.class -------------------------------------------------------------------------------- /samsung/ShortestPath.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/ShortestPath.class -------------------------------------------------------------------------------- /samsung/Tree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/Tree.class -------------------------------------------------------------------------------- /samsung/bfsgraph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/bfsgraph.class -------------------------------------------------------------------------------- /samsung/bfsgraph.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Graph{ 4 | int V; 5 | LinkedList[] adj; 6 | 7 | Graph(int V){ 8 | this.V = V; 9 | adj = new LinkedList[V]; 10 | for(int i=0; i(); 12 | } 13 | 14 | void addEdge(int v, int w){ 15 | adj[v].add(w); 16 | } 17 | 18 | void bfs(int s){ 19 | boolean[] visited = new boolean[V]; 20 | LinkedList queue = new LinkedList(); 21 | visited[s] = true; 22 | queue.add(s); 23 | 24 | while(queue.size()!=0){ 25 | s=queue.poll(); 26 | System.out.println(s+" "); 27 | Iterator i = adj[s].listIterator(); 28 | while(i.hasNext()){ 29 | int n = i.next(); 30 | if(!visited[n]) 31 | { 32 | visited[n] = true; 33 | queue.add(n); 34 | } 35 | } 36 | } 37 | } 38 | } 39 | 40 | class bfsgraph{ 41 | public static void main(String[] args) { 42 | Graph g = new Graph(4); 43 | g.addEdge(0,1); 44 | g.addEdge(0,2); 45 | g.addEdge(2,0); 46 | g.addEdge(2,3); 47 | //g.addEdge(3,3); 48 | g.bfs(0); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /samsung/catalan.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/catalan.class -------------------------------------------------------------------------------- /samsung/catalan.java: -------------------------------------------------------------------------------- 1 | /* 2 | Total number of possible Binary Search Trees with n keys 3 | Total number of possible Binary Search Trees with n different keys = Catalan number Cn = (2n)!/(n+1)!*n! 4 | 5 | For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, …. 6 | So are numbers of Binary Search Trees. 7 | */ 8 | 9 | import java.util.*; 10 | class catalan{ 11 | 12 | static long findBinCoeff(long n, long k){ 13 | 14 | long res=1; 15 | 16 | if(k>n-k) 17 | k=n-k; 18 | 19 | for(int i=0; i>=1; 24 | } 25 | System.out.println(count); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /samsung/detectcycle.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/detectcycle.class -------------------------------------------------------------------------------- /samsung/eggDrop.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/eggDrop.class -------------------------------------------------------------------------------- /samsung/eggDrop.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class eggDrop{ 4 | 5 | static void findOptimalSolution(int n, int k){ 6 | 7 | int[][] eggfloors = new int[n+1][k+1]; // holds min no of trials 8 | 9 | int res=0, i, j, x; 10 | 11 | for(i=1; i<=n; i++){ 12 | eggfloors[i][1] = 1; 13 | eggfloors[i][0] = 0; 14 | } 15 | 16 | for(j=1; j<=k; j++) 17 | eggfloors[1][j] = j; 18 | 19 | for(i=2; i<=n; i++) 20 | { 21 | for(j=2; j<=k; j++) 22 | { 23 | eggfloors[i][j] = Integer.MAX_VALUE; 24 | for(x=1; x<=j; x++){ 25 | res = 1 + Math.max(eggfloors[i-1][x-1],eggfloors[i][j-x]); 26 | if(res < eggfloors[i][j]) 27 | eggfloors[i][j]=res; 28 | } 29 | } 30 | } 31 | System.out.println(eggfloors[n][k]); 32 | } 33 | 34 | 35 | public static void main(String[] args) { 36 | 37 | int n =2, k=100; 38 | findOptimalSolution(n,k); 39 | 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /samsung/graphdfs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/graphdfs.class -------------------------------------------------------------------------------- /samsung/graphdfs.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Graph{ 4 | int V; 5 | LinkedList[] adj; 6 | 7 | Graph(int V){ 8 | this.V = V; 9 | adj = new LinkedList[V]; 10 | for(int i=0; i(); 12 | } 13 | 14 | void addEdge(int v, int w){ 15 | adj[v].add(w); 16 | } 17 | 18 | void dfs(int v){ 19 | boolean[] visited = new boolean[V]; 20 | for(int i=0; i iterator = adj[v].iterator(); 30 | while(iterator.hasNext()){ 31 | int i = iterator.next(); 32 | if(!visited[i]) 33 | dfsUtil(i,visited); 34 | } 35 | } 36 | } 37 | 38 | class graphdfs{ 39 | public static void main(String[] args) { 40 | Graph g = new Graph(4); 41 | 42 | g.addEdge(0,2); 43 | g.addEdge(1,2); 44 | g.addEdge(2,3); 45 | 46 | g.dfs(0); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /samsung/heapSort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/heapSort.class -------------------------------------------------------------------------------- /samsung/heapSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class heapSort{ 3 | 4 | static void sort(int[] arr){ 5 | int n = arr.length, i; 6 | 7 | for(i=n/2-1; i>=0; i--) 8 | heapify(arr,n,i); 9 | 10 | for(i=n-1; i>=0; i--){ 11 | int temp = arr[0]; 12 | arr[0] = arr[i]; 13 | arr[i] = temp; 14 | heapify(arr,i,0); 15 | } 16 | } 17 | 18 | static void heapify(int[] arr, int n, int i){ 19 | int largest = i; 20 | int l = 2*i + 1; 21 | int r = 2*i + 2; 22 | 23 | if(larr[largest]) 24 | largest=l; 25 | if(rarr[largest]) 26 | largest=r; 27 | 28 | if(largest!=i){ 29 | int swap = arr[i]; 30 | arr[i] = arr[largest]; 31 | arr[largest] = swap; 32 | heapify(arr,n,largest); 33 | } 34 | } 35 | 36 | public static void main(String[] args) { 37 | 38 | int[] arr = new int[]{3,5,1,2,7,4}; 39 | sort(arr); 40 | for(int i: arr) 41 | System.out.print(i+" "); 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /samsung/kthlargestinBST.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/kthlargestinBST.class -------------------------------------------------------------------------------- /samsung/kthlargestinBST.java: -------------------------------------------------------------------------------- 1 | /* 2 | K’th Largest Element in BST when modification to BST is not allowed 3 | */ 4 | import java.util.*; 5 | 6 | class Tree{ 7 | int data; 8 | Tree left, right; 9 | 10 | Tree(int d){ 11 | data =d; 12 | left =null; 13 | right =null; 14 | } 15 | 16 | void insert(int d){ 17 | Tree temp = this, prev = this; 18 | while(temp!=null){ 19 | prev = temp; 20 | if(temp.data>d) 21 | temp = temp.left; 22 | else 23 | temp = temp.right; 24 | } 25 | Tree newnode = new Tree(d); 26 | if(prev.data > d) 27 | prev.left=newnode; 28 | else 29 | prev.right=newnode; 30 | } 31 | } 32 | 33 | 34 | class kthlargestinBST{ 35 | 36 | static void kthlargest(Tree head, int k){ 37 | int c=0; 38 | kthlargestUtil(head,k,c); 39 | } 40 | 41 | static void kthlargestUtil(Tree head, int k, int c){ 42 | 43 | if(head==null || c>=k) 44 | return; 45 | 46 | kthlargestUtil(head.right,k,c); 47 | c++; 48 | 49 | if(c==k){ 50 | System.out.println(head.data); 51 | return; 52 | } 53 | 54 | kthlargestUtil(head.left,k,c); 55 | } 56 | 57 | public static void main(String[] args) { 58 | 59 | Tree root = new Tree(50); 60 | root.insert(30); 61 | root.insert(20); 62 | root.insert(40); 63 | root.insert(70); 64 | root.insert(60); 65 | root.insert(80); 66 | 67 | for(int k=1; k<=7; k++) 68 | kthlargest(root,k); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /samsung/leftview.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/leftview.class -------------------------------------------------------------------------------- /samsung/leftview.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Tree{ 4 | int data; 5 | Tree left, right; 6 | 7 | Tree(int d){ 8 | data =d; 9 | left =null; 10 | right =null; 11 | } 12 | 13 | void insert(int d){ 14 | Tree temp = this, prev = this; 15 | while(temp!=null){ 16 | prev = temp; 17 | if(temp.data>d) 18 | temp = temp.left; 19 | else 20 | temp = temp.right; 21 | } 22 | 23 | Tree newnode = new Tree(d); 24 | if(prev.data > d) 25 | prev.left=newnode; 26 | else 27 | prev.right=newnode; 28 | } 29 | } 30 | 31 | class leftview{ 32 | 33 | static int maxlevel=0; 34 | 35 | static void leftViewUtil(Tree root, int level){ 36 | 37 | if(root==null) 38 | return; 39 | 40 | if(maxlevel0){ 11 | max_ending_here = max_ending_here*arr[i]; 12 | min_ending_here = Math.min(min_ending_here*arr[i],1); 13 | }else if(arr[i]==0){ 14 | max_ending_here=1; 15 | min_ending_here=1; 16 | } 17 | else{ 18 | int temp = max_ending_here; 19 | max_ending_here = Math.max(min_ending_here*arr[i],1); 20 | min_ending_here = temp*arr[i]; 21 | } 22 | 23 | 24 | if(max_so_far < max_ending_here) 25 | max_so_far = max_ending_here; 26 | } 27 | return max_so_far; 28 | } 29 | static int maxSubArraySum(int a[]) 30 | { 31 | int size = a.length; 32 | int max_so_far = Integer.MIN_VALUE, max_ending_here = 0; 33 | 34 | for (int i = 0; i < size; i++) 35 | { 36 | max_ending_here = max_ending_here + a[i]; 37 | if (max_so_far < max_ending_here) 38 | max_so_far = max_ending_here; 39 | if (max_ending_here < 0) 40 | max_ending_here = 0; 41 | } 42 | return max_so_far; 43 | } 44 | 45 | public static void main(String[] args) { 46 | 47 | int[] arr = new int[]{-6,0,-1,5,-2}; 48 | System.out.println(maxProduct(arr)); 49 | System.out.println(maxSubArraySum(arr)); 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /samsung/merge2list.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/merge2list.class -------------------------------------------------------------------------------- /samsung/missing.java: -------------------------------------------------------------------------------- 1 | /* 2 | Find the smallest positive number missing from an unsorted array | Set 1 3 | You are given an unsorted array with both positive and negative elements. You have to find the smallest positive number missing from the array in O(n) time using constant extra space. You can modify the original array. 4 | */ 5 | -------------------------------------------------------------------------------- /samsung/multiply2number.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class multiply2number{ 4 | public static void main(String[] args) { 5 | int[] n1 = new int[]{2,3,5}; 6 | int[] n2 = new int[]{1,1,2}; 7 | int n = n1.length; 8 | 9 | int[] res = new int[n]; 10 | int l=0, r=n-1; 11 | while(l<=r){ 12 | int mid = (l+r)/2; 13 | res[mid] = 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /samsung/nodedel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/nodedel.class -------------------------------------------------------------------------------- /samsung/recursremovedup.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class recursremovedup{ 3 | 4 | public static void main(String[] args) { 5 | 6 | String input = args[0]; 7 | System.out.println(input + "-" + input.length()); 8 | 9 | StringBuffer buffer = new StringBuffer(input); 10 | removeChar(buffer, 0, input.length()); 11 | System.out.println(buffer.toString() + "-" + buffer.length()); 12 | } 13 | 14 | static void removeChar(StringBuffer str, int start, int length) { 15 | if (start == length) 16 | return; 17 | removeChar(str, start + 1, length); 18 | if (start > 0 && start != str.length() && str.charAt(start - 1) == str.charAt(start)) 19 | str.delete(start - 1, start + 1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /samsung/simplegraph.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/simplegraph.class -------------------------------------------------------------------------------- /samsung/simplegraphimplement.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/simplegraphimplement.class -------------------------------------------------------------------------------- /samsung/simplegraphimplement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Graph{ 4 | int V; 5 | LinkedList[] adj; 6 | 7 | Graph(int v){ 8 | V=v; 9 | adj = new LinkedList[10]; 10 | for(int i=0; i(); 12 | } 13 | 14 | void addEdge(int v, int w){ 15 | adj[v].add(w); 16 | } 17 | } 18 | 19 | class simplegraphimplement{ 20 | public static void main(String[] args) { 21 | 22 | Graph g = new Graph(2); 23 | //g.addEdge(0,2); 24 | //g.addEdge(1,2); 25 | //g.addEdge(2,1); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /samsung/sumatk.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/singh96aman/Cracking-The-Coding-Interview-Solutions/44055f50868d9a6972360eda6a5556f8aee562f7/samsung/sumatk.class -------------------------------------------------------------------------------- /samsung/sumatk.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Tree{ 4 | int data; 5 | Tree left, right; 6 | Tree(int d){ 7 | data=d; 8 | left=null; 9 | right=null; 10 | } 11 | void push(int d){ 12 | Tree temp=this, parent=this; 13 | while(temp!=null){ 14 | parent=temp; 15 | if(temp.data>d) 16 | temp = temp.left; 17 | else 18 | temp = temp.right; 19 | } 20 | Tree newnode = new Tree(d); 21 | if(parent.data>d) 22 | parent.left=newnode; 23 | else 24 | parent.right=newnode; 25 | } 26 | int sum(Tree temp, int k){ 27 | if(temp==null) 28 | return 0; 29 | if(k==0) 30 | return temp.data; 31 | 32 | return sum(temp.left,k-1)+sum(temp.right,k-1); 33 | } 34 | void inorder(Tree temp){ 35 | if(temp!=null){ 36 | inorder(temp.left); 37 | System.out.println(temp.data); 38 | inorder(temp.right); 39 | } 40 | } 41 | } 42 | 43 | class sumatk{ 44 | public static void main(String[] args) { 45 | Tree t = new Tree(5); 46 | t.push(3); 47 | t.push(7); 48 | t.push(1); 49 | t.push(2); 50 | t.push(6); 51 | t.push(8); 52 | t.inorder(t); 53 | System.out.println(t.sum(t,2)); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /samsung/twostacksinaarray.java: -------------------------------------------------------------------------------- 1 | /* 2 | mplement two stacks in an array 3 | Create a data structure twoStacks that represents two stacks. Implementation of twoStacks should use only one array, i.e., both stacks should use the same array for storing elements. Following functions must be supported by twoStacks. 4 | 5 | push1(int x) –> pushes x to first stack 6 | push2(int x) –> pushes x to second stack 7 | 8 | pop1() –> pops an element from first stack and return the popped element 9 | pop2() –> pops an element from second stack and return the popped element 10 | 11 | Implementation of twoStack should be space efficient. 12 | */ 13 | 14 | // Use top1=0 and top2=size-1 15 | --------------------------------------------------------------------------------