├── .gitignore ├── Aggregate Zeros ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── microsoft │ │ └── Aggregation.java │ └── test │ └── java │ └── in │ └── interview │ └── microsoft │ └── AggregationTest.java ├── Alphabets Order ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── amazon │ │ └── interview │ │ └── Order.java └── test │ └── in │ └── kuldeepyadav │ └── amazon │ └── interview │ └── OrderTest.java ├── Anagram Sorting ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── AnagramComparator.java │ │ └── Sort.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ ├── AnagramComparatorTest.java │ ├── AnagramSorting.java │ └── SortTest.java ├── BST to DLL ├── .ALGORITH.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITH.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── ConvertBST.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── ConvertTest.java ├── Balanced Parentheses ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ ├── App.java │ │ └── ParenthesesGenerator.java │ └── test │ └── java │ └── com │ └── tarkshala │ ├── AppTest.java │ └── ParenthesesGeneratorTest.java ├── Ball in Bins ├── .DS_Store ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── Questions.pdf └── src │ └── in │ └── interview │ └── catamaranventures │ ├── BallBins.java │ └── MaxCycle.java ├── Best Grouping of Digits of a Phone Number ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── GroupUtils.java │ │ └── Grouping.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── GroupUtilsTest.java ├── Binary Search Tree ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── BinarySearchTree.java │ │ ├── BinaryTree.java │ │ └── Node.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── BinarySearchTreeTest.java ├── Binary Search in Array ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ ├── BinarySearch.java │ └── BinarySearchTest.java ├── Bits ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ └── housing │ └── interview │ └── Solution.java ├── Ceiling and Floor in BST ├── .ALGORITHM.md.html ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── amazon │ │ └── interview │ │ └── FloorCeilingBST.java └── test │ └── in │ └── kuldeepyadav │ └── amazon │ └── interview │ └── FloorCeilingBSTTest.java ├── Choose Non Consecutive Objects ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── NonConsecutiveSelection.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── NonConsecutiveSelectionTest.java ├── Color Blocks ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── google │ │ └── Painter.java │ └── test │ └── java │ └── in │ └── interview │ └── google │ └── PainterTest.java ├── Consistent Hashing ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ ├── App.java │ │ ├── LoadBalancer.java │ │ ├── MD5.java │ │ ├── Request.java │ │ └── Server.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── AppTest.java ├── Cycle Shooter ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── App.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── AppTest.java ├── DLL to BST ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ └── in │ └── interview │ └── google │ ├── DLLUtilities.java │ ├── DoublyLinkedList.java │ ├── DoublyLinkedListTest.java │ └── Node.java ├── Death Probability of Bot on a Grid ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── ola │ │ └── BotOnGrid.java └── test │ └── in │ └── interview │ └── ola │ └── BotOnGridTest.java ├── Dictionary ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── Element.java │ │ ├── Node.java │ │ └── Trie.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ ├── ElementTest.java │ └── TrieTest.java ├── DistinctSubsequences ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── SubsequenceCounter.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── SubsequenceCounterTest.java ├── Fibonacci Repersentation of a Number ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── FibonacciRepersentation.java │ │ ├── FibonacciRepersentationTest.java │ │ └── FibonacciSeries.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── FibonacciTest.java ├── Fibonacci ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── google │ │ └── Fibonacci.java │ └── test │ └── java │ └── in │ └── interview │ └── google │ └── FibonacciTest.java ├── GasStations ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── IndexComputer.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── AppTest.java ├── Graph ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md └── src │ └── in │ └── interview │ └── amazon │ ├── AdjacencyList.java │ ├── DirectedGraph.java │ └── UndirectedGraph.java ├── Heap ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── interview │ │ ├── HeapIterator.java │ │ └── HeapList.java └── test │ └── in │ └── kuldeepyadav │ └── interview │ └── HeapTest.java ├── Increment Array Number ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ └── main │ └── java │ └── in │ └── interview │ └── google │ ├── NumberUtils.java │ └── NumberUtilsTest.java ├── Infix to Postfix ├── .gitignore ├── .idea │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── encodings.xml │ ├── misc.xml │ └── modules.xml ├── ALGORITHM.md ├── Infix to Postfix.iml ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── inbhiwadi │ │ └── postfix │ │ └── Algebra.java │ └── test │ └── java │ └── com │ └── inbhiwadi │ └── postfix │ └── AlgebraTest.java ├── Inorder to All Possible Binary Tree ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── Intersect Zeros in Matrix ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── images │ └── Intersect Zeros.jpg └── src │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── IntersectZeros.java ├── Inversions Count in Array ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── amazon │ │ └── interview │ │ └── Inversions.java └── test │ └── in │ └── kuldeepyadav │ └── amazon │ └── interview │ └── InversionsTest.java ├── Islands in Matrix ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── ola │ │ └── Islands.java └── test │ └── in │ └── interview │ └── ola │ └── IslandsTest.java ├── Kth smallest element in Matrix sorted along Rows and Columns ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ └── in │ └── interview │ └── limeroad │ └── Matrix.java ├── LICENSE ├── LRU Cache ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ └── main │ └── java │ └── in │ └── interview │ └── google │ ├── Cache.java │ └── LRUCache.java ├── Large Balanced Paranthesis File ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── Line Passing through Maximum Points ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── LinkedList ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── pom.xml └── src │ └── in │ └── kuldeepyadav │ └── amazon │ └── interview │ └── LookAheadIterator.java ├── Longest Contiguous Increasing Sequence in Matrix ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── microsoft │ │ └── MatrixUtils.java │ └── test │ └── java │ └── in │ └── interview │ └── microsoft │ └── MatrixUtilsTest.java ├── Majority Element ├── .gitignore └── pom.xml ├── MajorityElement ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── MajorityFinder.java │ └── test │ └── java │ └── com │ └── tarkshala │ ├── AppTest.java │ └── MajorityFinderTest.java ├── Make Palindrom ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── kuldeep │ │ └── PalindromeUtils.java │ └── test │ └── java │ └── com │ └── kuldeep │ └── PalindromeUtilsTest.java ├── Maths Algebra ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── Multiply.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── MultiplyTest.java ├── Maths ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── in │ └── interview │ └── google │ ├── MathUtils.java │ └── MathUtilsTest.java ├── Max Tasks Scheduler for N Machines ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md └── images │ └── Max tasks using N machines.jpg ├── Max Tasks Scheduler ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── microsoft │ │ └── Scheduler.java └── test │ └── in │ └── interview │ └── microsoft │ └── SchedulerTest.java ├── Median in 2 Sorted array ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeep │ │ └── interview │ │ └── google │ │ └── Median.java └── test │ └── in │ └── kuldeep │ └── interview │ └── google │ └── MedianTest.java ├── MinJumpsArray ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── MinimumJumpComputer.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── MinimumJumpComputerTest.java ├── Minimum Distance between Two Words ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── Minimum Sum of Distances ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── Minimum Sum of Triplets ├── .gitignore ├── .idea │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── encodings.xml │ ├── misc.xml │ └── modules.xml ├── ALGORITHM.md ├── Minimum Sum of Triplets.iml ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── inbhiwadi │ │ └── MinimumSumArray.java │ └── test │ └── java │ └── com │ └── inbhiwadi │ └── MinimumSumArrayTest.java ├── Minimum Swaps for Pairing ├── .gitignore ├── .idea │ ├── compiler.xml │ ├── copyright │ │ └── profiles_settings.xml │ ├── encodings.xml │ ├── misc.xml │ └── modules.xml ├── ALGORITHM.md ├── Minimum Swaps for Pairing.iml ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── inbhiwadi │ └── ArraySwap.java ├── Missing Positive Number ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md └── src │ └── in │ └── interview │ └── limeroad │ ├── ArrayUtils.java │ └── ArrayUtilsTest.java ├── Most Frequent Words in a File ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── dump ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── FrequentWords.java ├── test │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── FrequentWordsTest.java └── wordsEn.txt ├── No Two Consecutive Zeros ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── NonConsecutiveZeroStrings.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── NonConsecutiveZeroStringsTest.java ├── Non Intersecting Chords ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── NonIntersectingChordsCounter.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── NonIntersectingChordsCounterTest.java ├── Optimal Path in Matrix ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── Pairs having Sum Greater than Threshold ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── google │ │ └── ArrayUtils.java │ └── test │ └── java │ └── in │ └── interview │ └── google │ └── ArrayUtilsTest.java ├── Palindrome With Whitechar ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── microsoft │ │ └── PalindromeUtils.java │ └── test │ └── java │ └── in │ └── interview │ └── microsoft │ └── PalindromeUtilsTest.java ├── PalindromePartitioning ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── tarkshala │ └── PalindromePartitioner.java ├── Predecessor and Successor of a key in BST ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── ola │ │ └── BinarySearchTree.java └── test │ └── in │ └── interview │ └── ola │ └── BinarySearchTreeTest.java ├── Preorder to BST ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── algorithm.md ├── images │ └── PreorderToBST.jpg ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── PreorderToBST.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── PreorderToBSTTest.java ├── Print Partial Merged Arrays ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── in │ │ └── interview │ │ └── microsoft │ │ └── Permutation.java │ └── test │ └── java │ └── in │ └── interview │ └── microsoft │ └── PermutationTest.java ├── Queue using Array ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── ALGORITHM.md ├── README.md ├── RepeatedSubstring.md ├── Repetation In Array ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── google │ │ └── Repetition.java └── test │ └── in │ └── interview │ └── google │ └── RepetitionTest.java ├── Reservoir Sampling ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ ├── App.java │ │ └── RandomNumberCalculator.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── AppTest.java ├── Reverse Every Two Nodes in Linkedlist ├── .classpath ├── .gitignore ├── .project ├── .settings │ ├── org.eclipse.jdt.core.prefs │ └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src │ └── main │ └── java │ └── in │ └── interview │ └── google │ ├── LinkedlistUtils.java │ └── LinkedlistUtilsTest.java ├── Robot reachability ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── kuldeep │ │ └── RobotMovement.java │ └── test │ └── java │ └── com │ └── kuldeep │ └── RobotMovementTest.java ├── Rotate Matrix Inplace ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── interview │ │ └── google │ │ ├── MatrixUtils.java │ │ └── Rotate.java └── test │ └── in │ └── kuldeepyadav │ └── interview │ └── google │ ├── MatrixUtilsTest.java │ └── RotateTest.java ├── Search in Rotated Array ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ ├── NewRotatedBinarySearch.java │ │ ├── PivotUtil.java │ │ └── RotatedBinarySearch.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ ├── PivotUtilTest.java │ └── RotatedBinarySearchTest.java ├── Segregate Positive and Negative ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── interview │ │ └── google │ │ └── Segregate.java └── test │ └── in │ └── kuldeepyadav │ └── interview │ └── google │ └── SegregateTest.java ├── Sort N Element array with K Distinct keys ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── google │ │ └── ArrayUtils.java └── test │ └── in │ └── interview │ └── google │ └── ArrayUtilsTest.java ├── String Interleavings ├── .ALGORITHM.md.html ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md └── src │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ ├── Interleaving.java │ └── InterleavingTest.java ├── Subarray with given Sum ├── .gitignore ├── ALGORITHM.md └── src │ └── com │ └── kuldeep │ ├── SubarrayFinder.java │ └── SubarrayFinderTest.java ├── Suffix Tree ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── kuldeep │ │ └── algorithm │ │ └── SuffixTree.java │ └── test │ └── java │ └── com │ └── kuldeep │ └── algorithm │ └── SuffixTreeTest.java ├── Sum Along Diaginals in Binary Tree ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── kuldeepyadav │ │ └── google │ │ └── interview │ │ └── IntegerBinaryTree.java └── test │ └── in │ └── kuldeepyadav │ └── google │ └── interview │ └── IntegerBinaryTreeTest.java ├── Sum of Bit Difference ├── .gitignore ├── ALGORITHM.md ├── Sum of Bit Difference.iml ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── inbhiwadi │ │ └── BitsCounter.java │ └── test │ └── java │ └── com │ └── inbhiwadi │ └── BitsCounterTest.java ├── Sum of Cube of Two equal to Cube of Third ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── src │ └── in │ │ └── kuldeepyadav │ │ └── amazon │ │ └── interview │ │ └── SumOfCubes.java └── test │ └── in │ └── kuldeepyadav │ └── amazon │ └── interview │ └── SumOfCubesTest.java ├── Sum of Pair divisible by 'K' ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── ixigo │ │ └── Pairs.java └── test │ └── in │ └── interview │ └── ixigo │ └── PairsTest.java ├── Trapping Rain Water ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── kuldeep │ ├── Trap.java │ └── TrapTest.java ├── Two Jugs ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── ALGORITHM.md ├── src │ └── in │ │ └── interview │ │ └── housing │ │ └── Jugs.java └── test │ └── in │ └── interview │ └── housing │ └── JugsTest.java ├── Walk Array ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── pseudo code.md ├── src │ └── com │ │ └── kuldeep │ │ └── google │ │ └── interview │ │ └── Jump.java └── test │ └── com │ └── kuldeep │ └── google │ └── interview │ └── JumpTest.java ├── Zero Sum Triplets ├── .gitignore ├── ALGORITHM.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── tarkshala │ │ └── App.java │ └── test │ └── java │ └── com │ └── tarkshala │ └── AppTest.java └── Zig Zag Sorting ├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── ALGORITHM.md ├── pom.xml └── src └── main └── java └── in └── interview └── microsoft ├── ArrayUtils.java └── ArrayUtilsTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | /bin/ 14 | -------------------------------------------------------------------------------- /Aggregate Zeros/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Aggregate Zeros/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Aggregate Zeros 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Aggregate Zeros/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Aggregate Zeros/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Aggregate Zeros/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Aggregate Zeros after all Non-zero elements maintaining Order of Non-zero elements 2 | =============================================================================== 3 | 4 |

5 | Statement : 6 |

7 | Aggregate all zeros in last of array, such that all non-zero elements are in beginning one after. Also order between non-zero elements is to be maintained. 8 | 9 |

10 | Algorithm : 11 |

12 | Take two index pointers. First pointing to leftmost index which have zero. 13 | Initially second pointer also points where first points. 14 | Then go on incrementing second pointer, if element under it is non-zero then put it where first 15 | pointer points and increment first pointer by one. 16 | 17 |

18 | Complexity : 19 |

20 | Time : O(n)
21 | Space : O(1) 22 | -------------------------------------------------------------------------------- /Aggregate Zeros/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.microsoft 4 | aggregate-elements 5 | 0.1 6 | Aggregate Elements 7 | -------------------------------------------------------------------------------- /Aggregate Zeros/src/test/java/in/interview/microsoft/AggregationTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.microsoft; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Aggregation}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class AggregationTest { 13 | 14 | @Test 15 | public void testAggregate() { 16 | 17 | int[] array = new int[]{1,0,2,0,3,0,0,4,5,6,7,0,0,0}; 18 | 19 | int[] expected = new int[]{1,2,3,4,5,6,7,0,0,0,0,0,0,0}; 20 | Aggregation.aggregate(array); 21 | assertArrayEquals(expected, array); 22 | 23 | array = Aggregation.aggregate(new int[]{0,0,0,0,1,2,3,7,5,3,0,0,0}); 24 | assertArrayEquals(new int[]{1,2,3,7,5,3,0,0,0,0,0,0,0}, array); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Alphabets Order/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Alphabets Order/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Alphabets Order/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Alphabets Order 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Alphabets Order/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Alphabets Order/test/in/kuldeepyadav/amazon/interview/OrderTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.amazon.interview; 2 | 3 | import java.util.LinkedList; 4 | import java.util.List; 5 | 6 | import org.junit.Test; 7 | 8 | public class OrderTest { 9 | 10 | @Test 11 | public void testGetAlphabetInOrder() { 12 | 13 | List dictionary = new LinkedList(); 14 | dictionary.add("aaaaa"); 15 | dictionary.add("aaaab"); 16 | dictionary.add("aaac"); 17 | dictionary.add("aadz"); 18 | dictionary.add("abdaa"); 19 | dictionary.add("ad"); 20 | 21 | Order order = new Order(); 22 | List list = order.getAlphabetInOrder(dictionary); 23 | System.out.println(list); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Anagram Sorting/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Anagram Sorting/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Anagram Sorting/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Anagram Sorting 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Anagram Sorting/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Anagram Sorting/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Anagrams Sorting 2 | ================== 3 | 4 |

5 | Statement : 6 |

7 | Write a method to sort an array of strings so that all the anagrams are next to each other. 8 | 9 |

10 | Algorithm : 11 |

12 | To compare two strings, sort them character by character and compare the sorted characters array lexicographically. If both of them are equal then consider strings to be equal. 13 | 14 |

15 | Complexity : 16 |

17 | Time: O(nlogn)*O(klogk) where k is upperbound on length of strings.
18 | Space : O(n) in sorting. It can be reduced to O(1) if we do inplace merging. 19 | -------------------------------------------------------------------------------- /Anagram Sorting/test/in/kuldeepyadav/google/interview/AnagramComparatorTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link AnagramComparator}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class AnagramComparatorTest { 13 | 14 | @Test 15 | public void testCompare() { 16 | 17 | AnagramComparator anagramComparator = new AnagramComparator(); 18 | assertTrue(anagramComparator.compare("hello", "hello") == 0); 19 | assertTrue(anagramComparator.compare("hello", "olleh") == 0); 20 | assertTrue(anagramComparator.compare("abc", "abcd") < 0); 21 | assertTrue(anagramComparator.compare("abcd", "abce") < 0); 22 | assertTrue(anagramComparator.compare("abcd", "abced") < 0); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Anagram Sorting/test/in/kuldeepyadav/google/interview/AnagramSorting.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import org.junit.Test; 4 | 5 | public class AnagramSorting { 6 | 7 | @Test 8 | public void test() { 9 | 10 | String[] array = new String[]{"abc", "bca", "a", "b"}; 11 | Sort sort = new Sort(new AnagramComparator()); 12 | sort.sort(array); 13 | 14 | for (int i = 0; i < array.length; i++) { 15 | System.out.println(array[i]); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /BST to DLL/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BST to DLL/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /BST to DLL/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | BST to DLL 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /BST to DLL/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /BST to DLL/ALGORITH.md: -------------------------------------------------------------------------------- 1 | Binary Search Tree to Doubly Linked List 2 | ========================================= 3 | 4 |

5 | Problem : 6 |

7 | Convert a given Binary Search Tree into sorted doubly linked list. 8 | 9 |

10 | Algorithm : 11 |

12 | Go on traversing tree into inorder fashion. 13 | Convert left subtree into list, append current node to the list. Convert right subtree into list, 14 | append right list to previous list. 15 | 16 |

17 | Complexity : 18 |

19 | Time: O(n)
20 | Space: O(n*log(n)) -------------------------------------------------------------------------------- /Balanced Parentheses/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | -------------------------------------------------------------------------------- /Balanced Parentheses/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Statement 2 | ====== 3 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses of length 2*n. 4 | 5 | For example, given n = 3, a solution set is: 6 | 7 | "((()))", "(()())", "(())()", "()(())", "()()()" 8 | 9 | Solution 10 | ===== 11 | Go recursive 12 | 13 | Time: O(n^2) 14 | Space: O(n) 15 | -------------------------------------------------------------------------------- /Balanced Parentheses/src/main/java/com/tarkshala/App.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Balanced Parentheses/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Balanced Parentheses/src/test/java/com/tarkshala/ParenthesesGeneratorTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import org.junit.Test; 4 | 5 | public class ParenthesesGeneratorTest { 6 | 7 | @Test 8 | public void printAllParentheses() { 9 | 10 | ParenthesesGenerator parenthesesGenerator = new ParenthesesGenerator(); 11 | parenthesesGenerator.generate(4); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Ball in Bins/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kuldeepiitg/Algorithms-Puzzles/70261cbfea3cd0c61c82558500ae80253b7178d8/Ball in Bins/.DS_Store -------------------------------------------------------------------------------- /Ball in Bins/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Ball in Bins/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Ball in Bins/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Ball in Bins 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Ball in Bins/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Ball in Bins/Questions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kuldeepiitg/Algorithms-Puzzles/70261cbfea3cd0c61c82558500ae80253b7178d8/Ball in Bins/Questions.pdf -------------------------------------------------------------------------------- /Best Grouping of Digits of a Phone Number/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Best Grouping of Digits of a Phone Number/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Best Grouping of Digits of a Phone Number/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Best Grouping of Digits of a Phone Number 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Best Grouping of Digits of a Phone Number/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Best Grouping of Digits of a Phone Number/test/in/kuldeepyadav/google/interview/GroupUtilsTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link GroupUtils}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class GroupUtilsTest { 13 | 14 | @Test 15 | public void testGetBestGroup() { 16 | 17 | String phoneNumber = "11010"; 18 | String bestGrouping = GroupUtils.getBestGroup(phoneNumber); 19 | 20 | assertEquals("11" + GroupUtils.delimiter + "010", bestGrouping); 21 | assertEquals(3, GroupUtils.getScore(bestGrouping)); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Binary Search Tree/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Binary Search Tree/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Binary Search Tree/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Binary Search Tree 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Binary Search Tree/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Binary Search Tree/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Binary Search Tree 2 | ================== 3 | 4 | Binary Tree and Binary Search Tree with insert and search utilities. 5 | 6 |

7 | Algorithm : 8 |

9 | 10 |
11 | Check if given binary tree is binary search tree. 12 |
13 | Check if left and right subtrees are BST. Also check minimum value of right subtree 14 | is larger than value stored at node and maximum value of left subtree is smaller 15 | than value stored at node. 16 | 17 |
18 | Complexity : 19 |
20 | Time : O(n), inorder traversal
21 | Space : O(1) -------------------------------------------------------------------------------- /Binary Search in Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Binary Search in Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Binary Search in Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Binary Search in Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Binary Search in Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Binary Search in Array/src/in/kuldeepyadav/google/interview/BinarySearchTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link BinarySearch} 9 | * 10 | * @author kuldeep 11 | */ 12 | public class BinarySearchTest { 13 | 14 | @Test 15 | public void testSearch() { 16 | 17 | assertEquals(18, BinarySearch.search(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10}, 6)); 18 | assertTrue(0 <= BinarySearch.search(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10}, 1)); 19 | assertTrue(14 > BinarySearch.search(new int[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10}, 1)); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Bits/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Bits/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Bits/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Bits 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Bits/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Ceiling and Floor in BST/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Ceiling and Floor in BST/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Ceiling and Floor in BST 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Ceiling and Floor in BST/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Ceiling and Floor in BST/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Ceiling And Floor in Binary Search Tree 2 | ======================================= 3 | 4 |

5 | Statement : 6 |

7 | Find the ceiling and floor of a value in a given BST without extra space. 8 | if a BST contains 1 3 6 7 9 12 9 |
if the given value is 8 floor is 7 and ceiling is 9. 10 |
if the given value is 9 both floor and ceiling is 9. 11 | 12 |

Algorithm

13 | Go for search in BST and keep track of last smaller and larger element. -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Choose Non Consecutive Objects 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Choose Non Consecutive Objects 2 | ############################### 3 | 4 |

5 | Statement : 6 |

7 | Given n identical books(objects), find number of ways of choosing r non-consecutive books. 8 | 9 |

10 | Algorithm : 11 |

12 | select first object and find r-1 objects in last n-2 objects. We can't consider next object 13 | in domain of remaining selection otherwise it will give us consecutive selection. 14 | Or reject first element and find r objects in last n-1 objects. 15 | 16 | Recurrence Relation : 17 | ``` 18 | F(n,r) = n if r = 1, 19 | = 0 if r > n, 20 | = F(n-2,r-1) + F(n-1,r) otherwise. 21 | ``` 22 | 23 |

24 | Complexity : 25 |

26 | Time : O(n×r), quite loose bound. 27 | Space: O(n×r) 28 | -------------------------------------------------------------------------------- /Choose Non Consecutive Objects/test/in/kuldeepyadav/google/interview/NonConsecutiveSelectionTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link NonConsecutiveSelection}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class NonConsecutiveSelectionTest { 13 | 14 | @Test 15 | public void testSelect() { 16 | 17 | NonConsecutiveSelection nonConsecutiveSelection = new NonConsecutiveSelection(); 18 | assertEquals(4368, nonConsecutiveSelection.select(20, 5)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Color Blocks/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Color Blocks/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Color Blocks 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Color Blocks/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Color Blocks/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Color Blocks/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | in.interview.google 5 | color-blocks 6 | 0.1 7 | 8 | 9 | in.interview.google 10 | fibonacci-number 11 | 0.1 12 | 13 | 14 | -------------------------------------------------------------------------------- /Consistent Hashing/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | ConsistentHashing.iml 3 | -------------------------------------------------------------------------------- /Consistent Hashing/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Loadbalancer using Consistent Hashing 2 | ===== 3 | 4 | https://www.geeksforgeeks.org/hashing-in-distributed-systems/ 5 | 6 | https://medium.com/system-design-blog/consistent-hashing-b9134c8a9062/ 7 | -------------------------------------------------------------------------------- /Consistent Hashing/src/main/java/com/tarkshala/Request.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | import lombok.Setter; 7 | 8 | @NoArgsConstructor 9 | @Getter 10 | @Setter 11 | @AllArgsConstructor 12 | public class Request { 13 | 14 | private String requestId; 15 | } 16 | -------------------------------------------------------------------------------- /Consistent Hashing/src/main/java/com/tarkshala/Server.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Getter; 5 | import lombok.NoArgsConstructor; 6 | 7 | @AllArgsConstructor 8 | @NoArgsConstructor 9 | @Getter 10 | public class Server { 11 | 12 | private String uid; 13 | 14 | public void serve(Request request) { 15 | System.out.println("Server " + uid + " serving the request " + request.getRequestId()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Consistent Hashing/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Cycle Shooter/.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | *.iml 4 | -------------------------------------------------------------------------------- /Cycle Shooter/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | public void testGetLastManAlive() { 14 | assertEquals(73, App.getLastManAlive(100)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /DLL to BST/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /DLL to BST/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | DLL to BST 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /DLL to BST/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /DLL to BST/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /DLL to BST/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Doubly Linked-list to Binary Search Tree 2 | ======================================== 3 | 4 |

5 | Statement : 6 |

7 | Given a doubly sorted linked list, make a binary search tree. -------------------------------------------------------------------------------- /DLL to BST/src/in/interview/google/DLLUtilities.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | 4 | /** 5 | * Utilities concerned about doubly linked list. 6 | * 7 | * @author kuldeep 8 | */ 9 | public class DLLUtilities { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Death Probability of Bot on a Grid/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Death Probability of Bot on a Grid/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Death Probability of Bot on a Grid/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Death Probability of Bot on a Grid 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Death Probability of Bot on a Grid/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Dictionary/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Dictionary/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Dictionary/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Dictionary 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Dictionary/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Dictionary/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Tire Data Structure 2 | =================== 3 | 4 |

5 | Statement : 6 |

7 | Efficient way of implementing dictionary. 8 | Tire data structure http://en.wikipedia.org/wiki/Trie 9 | 10 |

11 | Algorithm : 12 |

13 | Make k-ary tree, where n is size of alphabet. If dictionary contains only English 14 | words then k is 26. 15 | 16 |

17 | Complexity : 18 |

19 | Insertion Time : O(n), n is length of word to be inserted.
20 | Search Time : O(n).
21 | Space : O(n) -------------------------------------------------------------------------------- /Dictionary/test/in/kuldeepyadav/google/interview/ElementTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Element}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class ElementTest { 13 | 14 | @Test 15 | public void testElement() { 16 | Element element1 = new Element(); 17 | Element element2 = new Element(); 18 | 19 | assertNotEquals(element1.hashCode(), element2.hashCode()); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Dictionary/test/in/kuldeepyadav/google/interview/TrieTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Trie}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class TrieTest { 13 | 14 | @Test 15 | public void testSearch() { 16 | 17 | char[] alphabet = new char[]{'b','i','o','x', 'l'}; 18 | Trie tire = new Trie(alphabet); 19 | tire.insert("box"); 20 | tire.insert("ox"); 21 | assertTrue(tire.isPresent("box")); 22 | assertFalse(tire.isPresent("lol")); 23 | tire.delete("box"); 24 | assertFalse(tire.isPresent("box")); 25 | assertTrue(tire.isPresent("ox")); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /DistinctSubsequences/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | *.iml 4 | target -------------------------------------------------------------------------------- /DistinctSubsequences/src/test/java/com/tarkshala/SubsequenceCounterTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class SubsequenceCounterTest { 9 | 10 | @Before 11 | public void setUp() throws Exception { 12 | 13 | } 14 | 15 | @Test 16 | public void count() { 17 | String sequence = "rabbbbiiiitit"; 18 | String subsequence = "rabbit"; 19 | SubsequenceCounter subsequenceCounter = new SubsequenceCounter(sequence, subsequence); 20 | System.out.println(subsequenceCounter.count()); 21 | } 22 | } -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Fibonacci Repersentation of a Number 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Fibonacci Representation of a Number 2 | =================================== 3 | 4 |

5 | Statement : 6 |

7 | Write a program to show Fibonacci representation of a given number. 8 | Ex- 15 can be represented as 100010 (1*13+0*8+0*5+0*3+1*2+0*1) 9 | 10 |

11 | Algorithm : 12 |

13 | Go on finding largest fibonacci number which is smaller than the given number. 14 | Subtract it from the number. And repeat the process for next smaller fibonacci number 15 | on remainder. -------------------------------------------------------------------------------- /Fibonacci Repersentation of a Number/src/in/kuldeepyadav/google/interview/FibonacciRepersentationTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link FibonacciRepersentation}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class FibonacciRepersentationTest { 13 | 14 | @Test 15 | public void testRepresent() { 16 | 17 | assertEquals("100010", FibonacciRepersentation.represent(15)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Fibonacci/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Fibonacci/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Fibonacci 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Fibonacci/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Fibonacci/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Fibonacci/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.google 4 | fibonacci-number 5 | 0.1 6 | -------------------------------------------------------------------------------- /Fibonacci/src/main/java/in/interview/google/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | /** 4 | * Fibonacci numbers and series. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class Fibonacci { 9 | 10 | /** 11 | * Nth fibonacci number.
12 | * 13 | * Note : it will work only for n less than 50, for larger values of n number overflows. 14 | * 15 | * @param n 16 | * index of number in fibonacci series. 17 | * @return nth {@link Fibonacci} number. 18 | */ 19 | public static int number(int n) { 20 | 21 | double phi = (1 + Math.sqrt(5))/2; 22 | double sai = (1 - Math.sqrt(5))/2; 23 | 24 | double nthFibonacci = Math.pow(phi, n) - Math.pow(sai, n); 25 | nthFibonacci /= (phi - sai); 26 | return (int) nthFibonacci; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Fibonacci/src/test/java/in/interview/google/FibonacciTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Fibonacci}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class FibonacciTest { 13 | 14 | @Test 15 | public void testNumber() { 16 | 17 | int last = 1, secondLast = 1; 18 | for (int i = 3; i < 100; i++) { 19 | int expected = secondLast + last; 20 | try { 21 | int formulaValue = Fibonacci.number(i); 22 | assertEquals(expected, formulaValue); 23 | } catch (AssertionError error) { 24 | System.out.println("failed for i : " + i); 25 | } 26 | 27 | secondLast = last; 28 | last = expected; 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /GasStations/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | 3 | .idea/ 4 | *.iml 5 | -------------------------------------------------------------------------------- /GasStations/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Gas Station on Circular Road 2 | ========== 3 | 4 | ####Statement 5 | There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. 6 | 7 | You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations. 8 | 9 | Return the minimum starting gas station’s index if you can travel around the circuit once, otherwise return -1. 10 | 11 | You can only travel in one direction. i to i+1, i+2, ... n-1, 0, 1, 2.. 12 | Completing the circuit means starting at i and ending up at i again. 13 | 14 | ##### Algorithm 15 | Start with first gas station, keep on filling and burning fuel. If empties in between, start with next 16 | station and keep on filling. 17 | 18 | ##### Complexity 19 | Time: O(n)
20 | Space: O(1) 21 | -------------------------------------------------------------------------------- /GasStations/src/main/java/com/tarkshala/IndexComputer.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | public class IndexComputer { 4 | 5 | private int[] gas; 6 | 7 | private int[] costs; 8 | 9 | public IndexComputer(int[] gas, int[] costs) { 10 | this.gas = gas; 11 | this.costs = costs; 12 | } 13 | 14 | public int getMinimumIndex() { 15 | int index = 0; 16 | int gasTank = gas[index]; 17 | while(index < gas.length) { 18 | int i = index; 19 | while (gasTank >= costs[i]) { 20 | gasTank -= costs[i]; 21 | i = (i+1)%gas.length; 22 | gasTank += gas[i]; 23 | 24 | if (i == index) return index; 25 | } 26 | index = i+1; 27 | } 28 | 29 | return -1; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /GasStations/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Graph/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Graph/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Graph/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Graph 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Graph/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Graph/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Directed and Undirected Graphs 2 | ============================== 3 | 4 | -------------------------------------------------------------------------------- /Graph/src/in/interview/amazon/DirectedGraph.java: -------------------------------------------------------------------------------- 1 | package in.interview.amazon; 2 | 3 | /** 4 | * Directed Graph. 5 | * 6 | * @author kuldeep 7 | * 8 | * @param 9 | */ 10 | public class DirectedGraph extends AdjacencyList{ 11 | 12 | public DirectedGraph(Class c, int verticesCount) { 13 | super(c, verticesCount); 14 | } 15 | 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Graph/src/in/interview/amazon/UndirectedGraph.java: -------------------------------------------------------------------------------- 1 | package in.interview.amazon; 2 | 3 | /** 4 | * Undirected Graph 5 | * 6 | * @author kuldeep 7 | * 8 | * @param 9 | */ 10 | public class UndirectedGraph extends DirectedGraph{ 11 | 12 | public UndirectedGraph(Class c, int verticesCount) { 13 | super(c, verticesCount); 14 | } 15 | 16 | @Override 17 | public void addEdge(int start, int end) { 18 | super.addEdge(start, end); 19 | super.addEdge(end, start); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Heap/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Heap/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /Heap/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Heap 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Heap/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Heap/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Heap List 2 | ================ 3 | [Heap](http://en.wikipedia.org/wiki/Heap_%28data_structure%29) implementation of a list. 4 | -------------------------------------------------------------------------------- /Heap/src/in/kuldeepyadav/interview/HeapIterator.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.interview; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * Iterator for {@link Heap}. 7 | * 8 | * @author kuldeep 9 | */ 10 | public class HeapIterator implements Iterator { 11 | 12 | /** 13 | * Local copy of heap. 14 | */ 15 | private HeapList heapCopy; 16 | 17 | public HeapIterator(HeapList heapCopy) { 18 | super(); 19 | this.heapCopy = heapCopy; 20 | } 21 | 22 | @Override 23 | public boolean hasNext() { 24 | 25 | return heapCopy.size() > 1; 26 | } 27 | 28 | @Override 29 | public E next() { 30 | 31 | return heapCopy.pop(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Increment Array Number/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Increment Array Number/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Increment Array Number 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Increment Array Number/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Increment Array Number/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Increment Array Number/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Increment Array Number 2 | ====================== 3 | 4 |

5 | Statement : 6 |

7 | A number is represented in the form of an array of digits. 8 | Number 4385 will be represented as [4,3,8,5]. 9 | Make an increment function like incrementing the number [4,3,8,5] will give 10 | [4,3,8,6] and number [9,9,9] will give [1,0,0,0]. 11 | 12 |

13 | Source : 14 |

15 | http://www.careercup.com/question?id=4809209524781056 -------------------------------------------------------------------------------- /Increment Array Number/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.google 4 | increment-array-number 5 | 0.1 6 | -------------------------------------------------------------------------------- /Increment Array Number/src/main/java/in/interview/google/NumberUtilsTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link NumberUtils}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class NumberUtilsTest { 13 | 14 | @Test 15 | public void testIncrement() { 16 | 17 | int[] number = new int[]{5,4,3,6}; 18 | NumberUtils.increment(number); 19 | assertArrayEquals(new int[]{5,4,3,7}, number); 20 | 21 | number = new int[]{9,9,9}; 22 | number = NumberUtils.increment(number); 23 | assertArrayEquals(new int[]{1,0,0,0}, number); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Infix to Postfix/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Infix to Postfix/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Infix to Postfix/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Infix to Postfix/ALGORITHM.md: -------------------------------------------------------------------------------- 1 |

2 | Convert an Infix expression to Prefix 3 |

4 | 5 |

6 | Solution 7 |

8 | Use a stack and traverse. 9 | 10 |

11 | Complexity 12 |

13 | Space = O(n)
14 | Time = O(n)
-------------------------------------------------------------------------------- /Infix to Postfix/src/test/java/com/inbhiwadi/postfix/AlgebraTest.java: -------------------------------------------------------------------------------- 1 | package com.inbhiwadi.postfix; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * Junit test for {@link Algebra} 7 | *

8 | * Created by Kuldeep Yadav on 09/11/16. 9 | */ 10 | public class AlgebraTest { 11 | @Test 12 | public void infixToPostfix() throws Exception { 13 | 14 | String infixExpression = Algebra.infixToPostfix("( ( ( 1 + 2 ) * 3 ) - ( 4 + 5 ) )"); 15 | 16 | System.out.println(infixExpression); 17 | System.out.println(Algebra.evaluate(infixExpression)); 18 | 19 | infixExpression = Algebra.infixToPostfix("( ( ( 4 * 2 ) + ( 8 / 4 ) ) - ( 2 * ( 5 - 4 ) ) )"); 20 | 21 | System.out.println(infixExpression); 22 | System.out.println(Algebra.evaluate(infixExpression)); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /Inorder to All Possible Binary Tree/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Inorder to All Possible Binary Tree/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Inorder to All Possible Binary Tree/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Inorder to All Possible Binary Tree 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Inorder to All Possible Binary Tree/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Intersect Zeros in Matrix 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/images/Intersect Zeros.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kuldeepiitg/Algorithms-Puzzles/70261cbfea3cd0c61c82558500ae80253b7178d8/Intersect Zeros in Matrix/images/Intersect Zeros.jpg -------------------------------------------------------------------------------- /Intersect Zeros in Matrix/src/in/kuldeepyadav/google/interview/IntersectZeros.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | public class IntersectZeros { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Inversions Count in Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Inversions Count in Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Inversions Count in Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Inversions Count in Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Inversions Count in Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Inversions Count in Array/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Inversions in Array 2 | ===================== 3 | 4 |

5 | Statement : 6 |

7 | Pair of indices i,j is called an inversion if i < j and array[i] > array[j]. 8 | Given an array count total number of inversions. 9 | 10 |

11 | Algorithm : 12 |

13 | Divide and conquer. Use approach like merge sort. 14 | Sort first half and second half independently. 15 | While merging count how many elements of first half 16 | are skipped when an element of second half is put in place 17 | in sorted array. Take total from each and every step. 18 | 19 |

20 | Complexity : 21 |

22 | Time : O(nlogn)
23 | Space : O(nlogn), space can be optimized to O(n). -------------------------------------------------------------------------------- /Inversions Count in Array/test/in/kuldeepyadav/amazon/interview/InversionsTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.amazon.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Inversions}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class InversionsTest { 13 | 14 | @Test 15 | public void testSort() { 16 | 17 | assertEquals(0, Inversions.getInversionCount(new int[]{4,10,25})); 18 | assertEquals(6, Inversions.getInversionCount(new int[]{4,3,2,1})); 19 | assertEquals(1, Inversions.getInversionCount(new int[]{4,1})); 20 | assertEquals(41, Inversions.getInversionCount(new int[]{11,12,6,3,1,13,1,9,5,4,8,10,7})); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Islands in Matrix/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Islands in Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Islands in Matrix/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Islands in Matrix 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Islands in Matrix/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Kth smallest element in Matrix sorted along Rows and Columns 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Kth Smallest Element in Matrix Sorted along Rows and Columns 2 | ============================================================ 3 | 4 |

5 | Statement : 6 |

7 | Given a matrix (2-D) array which is sorted along both rows and columns. Find kth smallest element 8 | in matrix. 9 | 10 |

11 | Algorithm : 12 |

13 | Go on including smallest of neighbors incrementally. 14 | 15 |

16 | Complexity : 17 |

18 | Time : O(klog(k))
19 | Space : O(k + m.n) where m and n are dimensions of matrix. -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.limeroad 4 | matrix 5 | 0.0.1-SNAPSHOT 6 | 7 | src 8 | test 9 | 10 | 11 | maven-compiler-plugin 12 | 3.3 13 | 14 | 1.8 15 | 1.8 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Kth smallest element in Matrix sorted along Rows and Columns/src/in/interview/limeroad/Matrix.java: -------------------------------------------------------------------------------- 1 | package in.interview.limeroad; 2 | 3 | /** 4 | * 2-D matrix sorted along rows and columns. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class Matrix { 9 | 10 | /** 11 | * 2-D array representing matrix 12 | */ 13 | private int[][] matrix; 14 | 15 | /** 16 | * @param k 17 | * @return kth smallest element in matrix 18 | */ 19 | public int get(int k) { 20 | 21 | throw new RuntimeException(); 22 | } 23 | 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /LRU Cache/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /LRU Cache/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Cache 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /LRU Cache/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /LRU Cache/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /LRU Cache/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Least Recently Used Cache 2 | ========================= 3 | 4 | https://en.wikipedia.org/wiki/Cache_algorithms#LRU 5 | -------------------------------------------------------------------------------- /LRU Cache/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | in.interview.google 5 | lru-cache 6 | 0.1 7 | LRU Cache 8 | 9 | 10 | in.interview.google 11 | dll-to-bst 12 | 0.1 13 | 14 | 15 | -------------------------------------------------------------------------------- /LRU Cache/src/main/java/in/interview/google/Cache.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | /** 4 | * Cache storing objects. 5 | * 6 | * @author kuldeep 7 | * 8 | * @param 9 | */ 10 | public interface Cache { 11 | 12 | /** 13 | * Put key value in cache. 14 | * 15 | * @param key 16 | * key for value object 17 | * @param value 18 | * value to be stored in cache 19 | * @return stored value 20 | */ 21 | public V put(K key, V value); 22 | 23 | /** 24 | * @param key 25 | * key to get value from cache 26 | * @return stored value for key 27 | */ 28 | public V get(K key); 29 | } 30 | -------------------------------------------------------------------------------- /Large Balanced Paranthesis File/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Large Balanced Paranthesis File/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Large Balanced Paranthesis File/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Large Balanced Paranthesis File 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Large Balanced Paranthesis File/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Line Passing through Maximum Points/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Line Passing through Maximum Points/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Line Passing through Maximum Points/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Line Passing through Maximum Points 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Line Passing through Maximum Points/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Line Passing through Maximum Points/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Line Passing through Maximum Number of Points on a 2-D Graph 2 | ============================================================= 3 | 4 |

5 | Statement : 6 |

7 | Given a two dimensional graph with points on it, find a line which passes the most number of points. 8 | 9 |

10 | Algorithm : 11 |

12 | For every two points on graph, find slope and y-intercept. Keep count of pairs which gave a particular 13 | slope and y-intercept using hashmap. 14 | 15 |

16 | Complexity : 17 |

18 | Time : O(n2)
19 | Space : O(n2) -------------------------------------------------------------------------------- /LinkedList/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /target/ 3 | -------------------------------------------------------------------------------- /LinkedList/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | LinkedList 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /LinkedList/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /LinkedList/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /LinkedList/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.google 4 | linkedlist 5 | 0.1 6 | 7 | src 8 | test 9 | 10 | 11 | maven-compiler-plugin 12 | 3.3 13 | 14 | 1.8 15 | 1.8 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /LinkedList/src/in/kuldeepyadav/amazon/interview/LookAheadIterator.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.amazon.interview; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * Iterator to look ahead without going to next element of list. 7 | * 8 | * @author kuldeep 9 | */ 10 | public class LookAheadIterator implements Iterator{ 11 | 12 | /** 13 | * The look ahead element. 14 | */ 15 | private T lookAhead; 16 | 17 | /** 18 | * Enclosed Iterator. 19 | */ 20 | private Iterator iterator; 21 | 22 | @Override 23 | public boolean hasNext() { 24 | return lookAhead != null || iterator.hasNext(); 25 | } 26 | 27 | @Override 28 | public T next() { 29 | // TODO Auto-generated method stub 30 | T toReturn = lookAhead; 31 | if (iterator.hasNext()) { 32 | lookAhead = iterator.next(); 33 | } else { 34 | lookAhead = null; 35 | } 36 | return toReturn; 37 | } 38 | 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Longest Contiguous Increasing Sequence in Matrix 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Longest Contiguous Increasing Sequence in Matrix 2 | ================= 3 | 4 |

5 | Statement : 6 |

7 | Given a 2 dimensional array of unique values that could be either integers or floating point numbers, find the longest, contiguous, increasing sequence of values. When traversing the array, you can move up, down, left or right. 8 | 9 | For Example: 10 | ``` 11 | 10 15 2 5 12 | 4 23 6 19 13 | 1 8 9 21 14 | ``` 15 | 16 | The longest sequence is 1 -> 4 -> 10 -> 15 -> 23 17 | 18 |

19 | Algorithm : 20 |

21 | For every element find max sequence length of all its lesser neighbors and consider length for the sequence originating from this point to be one more than max. 22 | If there is no such neighbor then sequence length for that point is 1. 23 | 24 |

25 | Complexity : 26 |

27 | Time : O(n)
28 | Space : O(n)
29 | -------------------------------------------------------------------------------- /Longest Contiguous Increasing Sequence in Matrix/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | in.interview.microsoft 5 | longest-increasing-contiguous-sequence-in-matrix 6 | 0.1 7 | 8 | 9 | org.apache.commons 10 | commons-lang3 11 | 3.4 12 | 13 | 14 | -------------------------------------------------------------------------------- /Majority Element/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | *.iml 4 | -------------------------------------------------------------------------------- /Majority Element/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.tarkshala 8 | majority-element 9 | 1.0-SNAPSHOT 10 | 11 | 12 | -------------------------------------------------------------------------------- /MajorityElement/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | *.iml 4 | 5 | -------------------------------------------------------------------------------- /MajorityElement/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Majority Element 2 | ===== 3 | 4 | #### Statement 5 | 6 | Given an array of size n, find the majority element. The majority element is the element that appears more than floor(n/2) times. 7 | 8 | You may assume that the array is non-empty and the majority element always exist in the array. 9 | 10 | Example : 11 | 12 | Input : [2, 1, 2] 13 | Return : 2 which occurs 2 times which is greater than 3/2. 14 | 15 | ##### Algorithm 16 | [Moore's Majority Algorithm](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_majority_vote_algorithm) 17 | 18 | ##### Complexity 19 | Time: O(n)
20 | Space: O(1) 21 | -------------------------------------------------------------------------------- /MajorityElement/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import static org.junit.Assert.assertTrue; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | { 12 | /** 13 | * Rigorous Test :-) 14 | */ 15 | @Test 16 | public void shouldAnswerWithTrue() 17 | { 18 | assertTrue( true ); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MajorityElement/src/test/java/com/tarkshala/MajorityFinderTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import org.junit.Before; 4 | import org.junit.Test; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class MajorityFinderTest { 9 | 10 | @Before 11 | public void setUp() throws Exception { 12 | } 13 | 14 | @Test 15 | public void findMajor() { 16 | 17 | MajorityFinder finder = new MajorityFinder(new int[]{2,2,2,1,3,4,5,2,6,7,2,2,2}); 18 | System.out.println("Majority won by: " + finder.findMajor()); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /Make Palindrom/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Form a palindrome 2 | ================= 3 | 4 |

5 | Statement : 6 |

7 | Given a string, find the minimum number of characters to be inserted to convert it to palindrome.
8 | 9 | For Example:
10 | ab: Number of insertions required is 1. bab or aba
11 | aa: Number of insertions required is 0. aa
12 | abcd: Number of insertions required is 3. dcbabcd
13 | geeks: Number of insertions required is 3. skgeegks
14 | 15 |

16 | Algorithm : 17 |

18 | 19 | Count(string) = Math.min(a,b,c), where
20 | a = Count(substring starting from second character to last),
21 | b = Count(substring starting from first character to second last),
22 | c = Count(substring starting from second character to second last) 23 | if first and last character are same, otherwise infinite.
24 | -------------------------------------------------------------------------------- /Maths Algebra/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Maths Algebra/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Maths Algebra/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Maths Algebra 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Maths Algebra/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Maths Algebra/test/in/kuldeepyadav/google/interview/MultiplyTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import org.junit.Test; 4 | 5 | public class MultiplyTest { 6 | 7 | @Test 8 | public void testMultiply() { 9 | 10 | // assertEquals(49300, Multiply.multiply(1450, 34)); 11 | // 12 | // assertEquals(0, Multiply.multiply(0, 34)); 13 | // 14 | // assertEquals(0, Multiply.multiply(1450, 0)); 15 | // 16 | // System.out.println(Multiply.multiply(10, -23)); 17 | 18 | System.out.println(1 << 31); 19 | System.out.println(Integer.MIN_VALUE); 20 | System.out.println(Integer.MAX_VALUE); 21 | 22 | 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Maths/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Maths/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Maths/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Maths 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Maths/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Maths/src/in/interview/google/MathUtils.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | /** 4 | * Common maths functions. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class MathUtils { 9 | 10 | /** 11 | * @param firstNumber 12 | * @param secondNumber 13 | * @return Greatest Common Divisor of given two integers 14 | */ 15 | public static int gcd(int firstNumber, int secondNumber) { 16 | 17 | while(firstNumber%secondNumber != 0) { 18 | int temp = secondNumber; 19 | secondNumber = firstNumber%secondNumber; 20 | firstNumber = temp; 21 | } 22 | 23 | return secondNumber; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /Maths/src/in/interview/google/MathUtilsTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link MathUtils} 9 | * 10 | * @author kuldeep 11 | */ 12 | public class MathUtilsTest { 13 | 14 | @Test 15 | public void testGcd() { 16 | 17 | assertEquals(4, MathUtils.gcd(12, 8)); 18 | assertEquals(4, MathUtils.gcd(8, 12)); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Max Tasks Scheduler for N Machines/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Max Tasks Scheduler for N Machines/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Max Tasks Scheduler for N Machines/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Max Tasks Scheduler for N Machines 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Max Tasks Scheduler for N Machines/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Max Tasks Scheduler for N Machines/images/Max tasks using N machines.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kuldeepiitg/Algorithms-Puzzles/70261cbfea3cd0c61c82558500ae80253b7178d8/Max Tasks Scheduler for N Machines/images/Max tasks using N machines.jpg -------------------------------------------------------------------------------- /Max Tasks Scheduler/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Max Tasks Scheduler/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Max Tasks Scheduler/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Max Tasks Scheduler 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Max Tasks Scheduler/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Max Tasks Scheduler/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Schedule Maximum Number of Tasks using One Processor 2 | ==================================================== 3 | 4 |

5 | Statement : 6 |

7 | Given 'n' tasks, where each task is pair of start time and finish time. 8 | Schedule maximum number of tasks such that no two of them overlap. 9 | 10 |

11 | Algorithm : 12 |

13 | Sort all the tasks by finish time. Task finishing earlier, schedule first. 14 | If next earliest finish task is starting before finish time of already scheduled task, then drop it and look for next. Otherwise, schedule this task. 15 | 16 |

17 | Complexity : 18 |

19 | Time : Sorting O(nLog(n)) + look for all tasks one by one O(n) = O(nLog(n))
20 | Space : O(n) for n tasks = O(n) -------------------------------------------------------------------------------- /Median in 2 Sorted array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Median in 2 Sorted array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Median in 2 Sorted array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Median in 2 Sorted array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Median in 2 Sorted array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Median in 2 Sorted array/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Median of two Equal Size Sorted Arrays 2 | ====================================== 3 | Find median of two equal size sorted arrays. 4 | 5 |

6 | Statement : 7 |

8 | There are given two sorted arrays in ascending order. 9 | Find median of the sorted array found by merging them. 10 | 11 |

12 | Algorithm : 13 |

14 | Find mid element of both the arrays. If both are equal then value of median is equal to that. 15 | Otherwise, repeat the process by taking subarrays. Take first half subarray if the median is larger 16 | than median of second and take second half for other array. If size of both the subarrays are 1, 17 | then return anyone of them as median. 18 | 19 |

20 | Complexity : 21 |

22 | Time : O(log(n))
23 | Space : O(n) for storing arrays. -------------------------------------------------------------------------------- /Median in 2 Sorted array/test/in/kuldeep/interview/google/MedianTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeep.interview.google; 2 | 3 | import org.junit.Test; 4 | 5 | public class MedianTest { 6 | 7 | @Test 8 | public void testFindMedian() { 9 | 10 | int[] firstSortedArray = new int[]{7,8,9,10,11,12}; 11 | int[] secondSortedArray = new int[]{1,2,3,4,5,6}; 12 | int median = Median.findMedian(firstSortedArray, secondSortedArray); 13 | System.out.println(median); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /MinJumpsArray/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | *.iml 3 | .idea 4 | target -------------------------------------------------------------------------------- /MinJumpsArray/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Min Jumps Array 2 | ======= 3 | 4 | #### Statement 5 | Given an array of non-negative integers, you are initially positioned at the first index of the array. 6 | 7 | Each element in the array represents your maximum jump length at that position. 8 | 9 | Your goal is to reach the last index in the minimum number of jumps. 10 | 11 | Example : 12 | Given array A = [2,3,1,1,4] 13 | 14 | The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.) 15 | 16 | If it is not possible to reach the end index, return -1. 17 | 18 | 19 | ##### Algorithm: 20 | Use Dynamic programming 21 | 22 | ##### Complexity 23 | Time: O(n)
24 | Space: O(n) -------------------------------------------------------------------------------- /Minimum Distance between Two Words/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Minimum Distance between Two Words/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Minimum Distance between Two Words/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Minimum Distance between Two Words 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Minimum Distance between Two Words/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Minimum Sum of Distances/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Minimum Sum of Distances/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Minimum Sum of Distances/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Minimum Sum of Distances 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Minimum Sum of Distances/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Minimum Sum of Distances/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Meeting Point for N Men standing on a 2-D grid, such that Sum of Total Walk is Minimum 2 | ================ 3 | 4 |

5 | Statement : 6 |

7 | On a 2-D grid, the positions (x,y) of 3 persons are given. 8 | Find the meeting point such that sum of distances of each 9 | person from meeting point is minimized. -------------------------------------------------------------------------------- /Minimum Sum of Triplets/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Minimum Sum of Triplets/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Minimum Sum of Triplets/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Minimum Swaps for Pairing/.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /Minimum Swaps for Pairing/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Minimum Swaps for Pairing/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Minimum Swaps for Pairing/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.inbhiwadi 6 | swaps 7 | 1 8 | jar 9 | 10 | swaps 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 3.8.1 22 | test 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Missing Positive Number/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Missing Positive Number/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Missing Positive Number/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Missing Positive Number 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Missing Positive Number/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Missing Positive Number/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | First Missing Positive Number in Integer Array 2 | ============================================== 3 | 4 |

5 | Statement : 6 |

7 | Find first(smallest) positive integer which is absent in array. The array can have integer 8 | including negative values. 9 | 10 |

11 | Algorithm : 12 |

13 | Put 0 at indices where value is either greater than length of array itself or smaller than 0. 14 | Map values at indices such that if array have m occurrences of k then value at 15 | index k will be -m. It can be done in cyclic traversal over array just like 16 | counting sort. 17 | 18 |

19 | Complexity : 20 |

21 | Time : O(n)
22 | Space : O(1) -------------------------------------------------------------------------------- /Missing Positive Number/src/in/interview/limeroad/ArrayUtilsTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.limeroad; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link ArrayUtils} 9 | * 10 | * @author kuldeep 11 | * 12 | */ 13 | public class ArrayUtilsTest { 14 | 15 | @Test 16 | public void testMissingPositiveNumber() { 17 | 18 | int actual = ArrayUtils.missingPositiveNumber(new int[]{-1, -2, -3, -4, 1, 2, 3}); 19 | assertEquals(4, actual); 20 | 21 | actual = ArrayUtils.missingPositiveNumber(new int[]{-1, -2, -3, -4, 1, 2, 4}); 22 | assertEquals(3, actual); 23 | 24 | actual = ArrayUtils.missingPositiveNumber(new int[]{1,4,5,1,4,2,2,3,6}); 25 | assertEquals(7, actual); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Most Frequent Words in a File/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Most Frequent Words in a File/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Most Frequent Words in a File/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Most Frequent Words in a File 4 | 5 | 6 | Dictionary 7 | 8 | 9 | 10 | org.eclipse.jdt.core.javabuilder 11 | 12 | 13 | 14 | 15 | 16 | org.eclipse.jdt.core.javanature 17 | 18 | 19 | -------------------------------------------------------------------------------- /Most Frequent Words in a File/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 5 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 6 | org.eclipse.jdt.core.compiler.compliance=1.8 7 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 8 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 9 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 10 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 12 | org.eclipse.jdt.core.compiler.source=1.8 13 | -------------------------------------------------------------------------------- /Most Frequent Words in a File/dump: -------------------------------------------------------------------------------- 1 | Hello world 2 | how are you doing? -------------------------------------------------------------------------------- /No Two Consecutive Zeros/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /No Two Consecutive Zeros/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /No Two Consecutive Zeros/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | No Two Consecutive Zeros 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /No Two Consecutive Zeros/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | No Two Consecutive Zeros in a Binary String 2 | ============================================ 3 | 4 |

Statement :

5 | Find number of strings of length n which don't have any two consecutive zeros. 6 | 7 |

Algorithm :

8 | A '1' can be concatenated on right side with strings of length 9 | n-1 which don't have consecutive zeros, 10 | new formed strings of length 'n' will also hold the property. 11 | If '0' is concatenated then element left to it must be a 1 12 | otherwise we will end up in having two consecutive zeros. 13 | that is equivalent of concatenating '10' to strings of length n-2. 14 | 15 | 16 | Number of strings of length 'n' = C(n) 17 | C1) = 2
18 | C(2) = 3
19 | C(n) = C(n-1) + C(n-2)
20 | 21 | Use Dynamic Programming. 22 | 23 |

Pseudo Code :

24 | Recurrence equation is enough to make pseudo code. -------------------------------------------------------------------------------- /No Two Consecutive Zeros/src/in/kuldeepyadav/google/interview/NonConsecutiveZeroStrings.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | /** 4 | * Count of strings which don't have any consecutive zeros. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class NonConsecutiveZeroStrings { 9 | 10 | /** 11 | * @param n length of strings. 12 | * 13 | * @return count of strings which doesn't have consecutive zeros. 14 | */ 15 | public static int count(int n) { 16 | 17 | if (n == 1) { 18 | return 2; 19 | } else if (n == 2) { 20 | return 3; 21 | } else { 22 | return count(n - 2) + count(n -1); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /No Two Consecutive Zeros/test/in/kuldeepyadav/google/interview/NonConsecutiveZeroStringsTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link NonConsecutiveZeroStrings}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class NonConsecutiveZeroStringsTest { 13 | 14 | @Test 15 | public void testCount() { 16 | 17 | assertEquals(8,NonConsecutiveZeroStrings.count(4)); 18 | assertEquals(13,NonConsecutiveZeroStrings.count(5)); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Non Intersecting Chords/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Non Intersecting Chords/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Non Intersecting Chords/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Non Intersecting Chords 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Non Intersecting Chords/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Optimal Path in Matrix/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Optimal Path in Matrix/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /Optimal Path in Matrix/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Optimal Path in Matrix 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Optimal Path in Matrix/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Optimal Path in Matrix/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Optimal Path in Matrix 2 | ====================== -------------------------------------------------------------------------------- /Pairs having Sum Greater than Threshold/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Pairs having Sum Greater than Threshold/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Pairs having Sum Greater than Threshold 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Pairs having Sum Greater than Threshold/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Pairs having Sum Greater than Threshold/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Pairs having Sum Greater than Threshold/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.google 4 | pair-sum-greater-than-threshold 5 | 0.1 6 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Palindrome With Whitechar 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Palindrome with White Space Characters 2 | ================ 3 | 4 | 5 |

6 | Statement : 7 |

8 | whether a string with extra chars is a palindrome or not - BR telephonic 9 | "au u u a" is palindromic - yes. 10 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.microsoft 4 | palindrom-with-whitechar 5 | 0.1 6 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/src/main/java/in/interview/microsoft/PalindromeUtils.java: -------------------------------------------------------------------------------- 1 | package in.interview.microsoft; 2 | 3 | /** 4 | * Palindrome utilities. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class PalindromeUtils { 9 | 10 | /** 11 | * @param string 12 | * input string to checked if palindrome 13 | * @return true if string is palindrome ignoring spaces 14 | */ 15 | public static boolean isPalindromIgnoreWhitespace(String string) { 16 | 17 | int i = 0, j = string.length() - 1; 18 | while (i < j) { 19 | 20 | while ((string.charAt(i) == ' ' || string.charAt(i) == '\t') && i < j) i++; 21 | while ((string.charAt(j) == ' ' || string.charAt(j) == '\t') && i < j) j--; 22 | 23 | if (string.charAt(i) != string.charAt(j)) return false; 24 | i++; 25 | j--; 26 | } 27 | 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Palindrome With Whitechar/src/test/java/in/interview/microsoft/PalindromeUtilsTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.microsoft; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link PalindromeUtils}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class PalindromeUtilsTest { 13 | 14 | @Test 15 | public void testIsPalindromIgnoreWhitespace() { 16 | 17 | assertTrue(PalindromeUtils.isPalindromIgnoreWhitespace("")); 18 | assertTrue(PalindromeUtils.isPalindromIgnoreWhitespace("abcba")); 19 | assertTrue(PalindromeUtils.isPalindromIgnoreWhitespace("abccba")); 20 | assertTrue(PalindromeUtils.isPalindromIgnoreWhitespace("a bc b a")); 21 | assertTrue(PalindromeUtils.isPalindromIgnoreWhitespace(" ")); 22 | assertFalse(PalindromeUtils.isPalindromIgnoreWhitespace("abcb ca")); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /PalindromePartitioning/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | *.iml 3 | target/ 4 | .idea/ -------------------------------------------------------------------------------- /PalindromePartitioning/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Palindrome Partitioning II 2 | ======= 3 | 4 | #### Statement 5 | Given a string s, partition s such that every substring of the partition is a palindrome. 6 | 7 | Return the minimum cuts needed for a palindrome partitioning of s. 8 | 9 | Example : 10 | Given 11 | s = "aab", 12 | Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut. 13 | 14 | ##### Algorithm 15 | Find all palindromes taking every character(or pair of characters) in center and spreading out. 16 | Make a 2D array, keeping whose first index marks start and second index marks end index. 17 | 18 | Now do BFS to reach to last palindrome and count the least number of hops(palindromes). 19 | 20 | ##### Complexities 21 | Time: O(n^2)
22 | Space: O(n^2) -------------------------------------------------------------------------------- /Predecessor and Successor of a key in BST/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Predecessor and Successor of a key in BST/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Predecessor and Successor of a key in BST/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Predecessor and Successor of a key in BST 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Predecessor and Successor of a key in BST/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Predecessor and Successor of a key in BST/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Inorder Predecessor and Successor of a Key in BST 2 | ================================================= 3 | 4 |

5 | Statement : 6 |

7 | Find predecessor and successor of a key in inorder of BST. 8 | 9 |

10 | Algorithm : 11 |

12 | Search key in tree. For successor find leftmost node of it's(key's) right subtree. For predecessor look for rightmost node of it's left subtree. 13 | 14 |

15 | Complexity : 16 |

17 | Time : To search key O(log(n)) + To find predecessor O(log(n)) = O(log(n))
18 | Space : O(1) -------------------------------------------------------------------------------- /Preorder to BST/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Preorder to BST/.gitignore: -------------------------------------------------------------------------------- 1 | /bin 2 | -------------------------------------------------------------------------------- /Preorder to BST/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Preorder to BST 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Preorder to BST/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Preorder to BST/images/PreorderToBST.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kuldeepiitg/Algorithms-Puzzles/70261cbfea3cd0c61c82558500ae80253b7178d8/Preorder to BST/images/PreorderToBST.jpg -------------------------------------------------------------------------------- /Print Partial Merged Arrays/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Print Partial Merged Arrays/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Print Partial Merged Arrays 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Print Partial Merged Arrays/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Print Partial Merged Arrays/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Print Partial Merged Arrays/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Partially Merged Permutations of Two Sorted Arrays 2 | =========== 3 | 4 |

5 | Statement : 6 |

7 | Given two sorted arrays A and B, you have to generate all the possible arrays such that first element is taken from A then from B then from A and so on in increasing order till the arrays exhausted. The array should end with element from B. 8 | 9 |

10 | Algorithm : 11 | 12 | -------------------------------------------------------------------------------- /Print Partial Merged Arrays/src/test/java/in/interview/microsoft/PermutationTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.microsoft; 2 | 3 | import java.util.List; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Permutation}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class PermutationTest { 13 | 14 | @Test 15 | public void testPermute() { 16 | 17 | int[] firstArray = new int[]{4,7,10,15,17}; 18 | int[] secondArray = new int[]{5,9,11,16}; 19 | 20 | Permutation permutation = new Permutation(); 21 | List list = permutation.permute(firstArray, secondArray); 22 | System.out.println(list); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Queue using Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Queue using Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Queue using Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Queue using Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Queue using Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Queue using Array/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Queue Using an Array 2 | ==================== 3 | 4 |

5 | Statement : 6 |

7 | Implement a queue using an array. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Algorithms-Puzzles 2 | ================= 3 | 4 | Solution to usual problems they ask in interview. And yeah some of them are asked in Google too. 5 | -------------------------------------------------------------------------------- /Repetation In Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Repetation In Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Repetation In Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Repetation In Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Repetation In Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Repetation In Array/test/in/interview/google/RepetitionTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.google; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link Repetition} 9 | * 10 | * @author kuldeep 11 | */ 12 | public class RepetitionTest { 13 | 14 | @Test 15 | public void testSearch() { 16 | 17 | int repetition = Repetition.search(new int[]{5,2,1,4,1,3}); 18 | assertEquals(1, repetition); 19 | 20 | repetition = Repetition.search(new int[]{7,3,6,13,11,12,2,4,3,4,5,9,10,8}); 21 | assertEquals(3, repetition); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Reservoir Sampling/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Reservoir Sampling(size = 1) 2 | ======================================= 3 | 4 |

5 | Statement : 6 |

7 | An infinite stream of numbers is given. 8 | The stream is stopped at an arbitrary point. 9 | Return any number of the stream read till now with equal probability, using O(1) space. 10 | 11 |

Algorithm

12 | Select incoming number with probability of 1/n where n is count of number at that point of time including it. 13 | Means retain the old number with probability (n-1)/n. -------------------------------------------------------------------------------- /Reservoir Sampling/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.tarkshala 6 | Reservoir Sampling 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | Reservoir Sampling 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 3.8.1 22 | test 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /Reservoir Sampling/src/test/java/com/tarkshala/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.tarkshala; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Reverse Every Two Nodes in Linkedlist 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Reverse Every Two Consecutive Elements of LinkedList 2 | ==================================================== 3 | 4 |

5 | Statement : 6 |

7 | Reverse every 2 nodes in a singly linked list. 8 | 9 |

10 | Algorithm : 11 |

12 | Reverse every two nodes and that's all what is the algorithm. 13 | 14 | 15 |

16 | Complexity : 17 |

18 | Time : O(n)
19 | Space : O(1) -------------------------------------------------------------------------------- /Reverse Every Two Nodes in Linkedlist/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | in.interview.google 5 | reverse-2-nodes-linkedlist 6 | 0.1 7 | 8 | 9 | org.apache.commons 10 | commons-lang3 11 | 3.4 12 | 13 | 14 | -------------------------------------------------------------------------------- /Robot reachability/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Robot Reachability 2 | ================== 3 | 4 |

Statement

5 | A robot's movement is confined by following rule. 6 | If robot is currently standing on (x, y) then it can go to either (x+y, y) or (x, x+y). 7 | Given a starting point and ending point, check if robot can reach to later from first. 8 | 9 |

Algorithm

10 | Use BFS. Discard points which have x or y value more than coordinates of end point. 11 | 12 |

Complexity

13 | Time : might be O(n), not sure though 14 | Space : --- -------------------------------------------------------------------------------- /Robot reachability/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.kuldeep 6 | rivigo-test 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | rivigo-test 11 | http://maven.apache.org 12 | 13 | 14 | UTF-8 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 4.11 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /Robot reachability/src/test/java/com/kuldeep/RobotMovementTest.java: -------------------------------------------------------------------------------- 1 | package com.kuldeep; 2 | 3 | /** 4 | * Junit test for RobotMovement 5 | *

6 | * Created by kuldeep on 08/06/16. 7 | */ 8 | public class RobotMovementTest { 9 | 10 | 11 | } -------------------------------------------------------------------------------- /Rotate Matrix Inplace/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Rotate Matrix Inplace/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Rotate Matrix Inplace 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Rotate Matrix Inplace/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Rotate Matrix Inplace/src/in/kuldeepyadav/interview/google/Rotate.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.interview.google; 2 | 3 | public class Rotate { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Rotate Matrix Inplace/test/in/kuldeepyadav/interview/google/RotateTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.interview.google; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | public class RotateTest { 8 | 9 | @Test 10 | public void test() { 11 | fail("Not yet implemented"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Search in Rotated Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Search in Rotated Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Search in Rotated Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Search in Rotated Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Search in Rotated Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Search in Rotated Array/src/in/kuldeepyadav/google/interview/RotatedBinarySearch.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | /** 4 | * Binary Search in a rotated array. 5 | * 6 | * @author kuldeep 7 | */ 8 | public class RotatedBinarySearch { 9 | 10 | /** 11 | * Search key in rotated array. 12 | * 13 | * @param rotatedArray initial sorted array which is rotated by few index. 14 | * @param key key to be searched. 15 | * @return index of key in domain array. 16 | */ 17 | public static int search(int[] rotatedArray, int key) { 18 | 19 | int pivot = PivotUtil.getPivotIndex(rotatedArray); 20 | int index = BinarySearch.search(rotatedArray, key, 0, pivot - 1); 21 | if (index != -1) { 22 | return index; 23 | } 24 | index = BinarySearch.search(rotatedArray, key, pivot, rotatedArray.length - 1); 25 | return index; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Search in Rotated Array/test/in/kuldeepyadav/google/interview/PivotUtilTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | /** 8 | * Junit test for {@link PivotUtil}. 9 | * 10 | * @author kuldeep 11 | */ 12 | public class PivotUtilTest { 13 | 14 | @Test 15 | public void testGetPivotIndex() { 16 | 17 | assertEquals(0, PivotUtil.getPivotIndex(new int[]{1,2,3,4,5,6})); 18 | assertEquals(0, PivotUtil.getPivotIndex(new int[]{1,})); 19 | assertEquals(0, PivotUtil.getPivotIndex(new int[]{1,2})); 20 | assertEquals(1, PivotUtil.getPivotIndex(new int[]{2,1})); 21 | assertEquals(2, PivotUtil.getPivotIndex(new int[]{2,3,1})); 22 | assertEquals(1, PivotUtil.getPivotIndex(new int[]{3,1,2})); 23 | assertEquals(4, PivotUtil.getPivotIndex(new int[]{3,4,5,6,1,2})); 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Segregate Positive and Negative 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Segregate Positive and Negative elements in an Array 2 | ==================================================== 3 |

4 | Statement: Given an array of positive and negative integers segregate negative elements in front of array such that 5 | internal order of negative and positive elements don't change. 6 |

7 | e.g. -1 1 3 -2 2
8 | answer: -1 -2 1 3 2 9 | 10 |

11 | pseudo code: 12 |

13 | ``` 14 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/src/in/kuldeepyadav/interview/google/Segregate.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.interview.google; 2 | 3 | public class Segregate { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /Segregate Positive and Negative/test/in/kuldeepyadav/interview/google/SegregateTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.interview.google; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | public class SegregateTest { 8 | 9 | @Test 10 | public void test() { 11 | fail("Not yet implemented"); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Sort N Element array with K Distinct keys/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Sort N Element array with K Distinct keys/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Sort N Element array with K Distinct keys/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sort N Element array with K Distinct keys 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Sort N Element array with K Distinct keys/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Sort N Element array with K Distinct keys/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Sort an Array with Integer Keys having low Variance 2 | =================================================== 3 | 4 |

5 | Statement : 6 |

7 | Given an array with integer such that difference between minimum and maximum is less than size of the array. Sort it efficiently. Efficiently means O(n) time and O(1) space. 8 | 9 |

10 | Algorithm : 11 |

12 | Subtract minimum from each key, now the array must contain keys with in 0 to N-1. 13 | Map all the keys at corresponding index such that if there are k occurrences of x then 14 | index x will have value -k. 15 | 16 | Once Whole array is mapped, put keys in place. 17 | 18 | 19 |

20 | Complexity : 21 |

22 | Time : O(n)
23 | Space : O(1) -------------------------------------------------------------------------------- /String Interleavings/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /String Interleavings/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /String Interleavings/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | String Interleavings 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /String Interleavings/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /String Interleavings/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | String Interleavings 2 | ===================== 3 | 4 |

Statement :

5 | Print all interleavings of two strings.
6 | e.g. string1 : "AB" string2 : "CD"
7 | ABCD, ACBD, CABD, CADB etc. 8 | 9 |

10 | Algorithm : 11 |

12 | Use simple recursion. 13 | 14 |

15 | Pseudo Code : 16 |

17 | ``` 18 | Given string1 and string2 : 19 | 20 | if string1 is empty string : 21 | then print string2, 22 | else if string2 is empty string : 23 | then print string1 24 | 25 | print first character of string1 26 | append interleaving of string2 and remaining part of string1. 27 | print first character of string2 28 | append interleaving of string1 and remaining part of string2. 29 | ``` 30 | -------------------------------------------------------------------------------- /String Interleavings/src/in/kuldeepyadav/google/interview/InterleavingTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import org.junit.Test; 4 | 5 | /** 6 | * Junit test for {@link Interleaving}. 7 | * 8 | * @author kuldeep 9 | */ 10 | public class InterleavingTest { 11 | 12 | @Test 13 | public void testPrint() { 14 | 15 | Interleaving.print("ab", "cd", ""); 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Subarray with given Sum/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Subarray with given sum 2 | ====================== 3 | 4 |

5 | Statement : 6 |

7 | Given an unsorted array of non-negative integers, find a continous subarray which adds to a given number. 8 | 9 |

10 | Algorithm : 11 |

12 | Take two pointers indices, increment right pointer until sum becomes equal or more than given value. If 13 | it is more then increment left pointer until sum is equal or smaller than value. Give subarray from left 14 | to right if sum is equal to desired. -------------------------------------------------------------------------------- /Subarray with given Sum/src/com/kuldeep/SubarrayFinderTest.java: -------------------------------------------------------------------------------- 1 | package com.kuldeep; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * JUnit test for SubarrayFinder 9 | * 10 | * Created by kuldeep on 27/04/16. 11 | */ 12 | public class SubarrayFinderTest { 13 | 14 | @Test 15 | public void testSubarrayWithSum() throws Exception { 16 | 17 | int[] array = new int[]{20,34,23,6,8,9,1,27,36,10}; 18 | int sum = 24; 19 | int[] subarray = SubarrayFinder.subarrayWithSum(array, sum); 20 | int[] expected = new int[]{6,8,9,1}; 21 | assertArrayEquals(expected, subarray); 22 | } 23 | } -------------------------------------------------------------------------------- /Suffix Tree/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Pattern Searching (Suffix Tree Introduction) 2 | ============================================ 3 | 4 |

5 | Statement : 6 |

7 | Given a text txt[0..n-1] and a pattern pat[0..m-1], 8 | write a function search(char pat[], char txt[]) that 9 | prints all occurrences of pat[] in txt[]. You may assume that n > m. 10 | 11 | See also [GeeksForGeeks](http://www.geeksforgeeks.org/pattern-searching-set-8-suffix-tree-introduction/) -------------------------------------------------------------------------------- /Sum Along Diaginals in Binary Tree/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Sum Along Diaginals in Binary Tree/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sum Along Diaginals 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Sum Along Diaginals in Binary Tree/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Sum Along Diaginals in Binary Tree/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Sum along Left Diagonals 2 | ======================= 3 | 4 |

5 | Statement : 6 |

7 | Consider lines of slope -1 passing between nodes (dotted lines in below diagram). 8 | Diagonal sum in a binary tree is sum of all node’s data lying between these lines. 9 | Given a Binary Tree, print all diagonal sums. 10 | http://www.geeksforgeeks.org/diagonal-sum-binary-tree/ 11 | 12 |

13 | Algorithm : 14 |

15 | Use two queues. First queue will hold elements along diagonals and second queue 16 | will hold their right neighbors. Then process second queue considering as diagonal. 17 | 18 | 19 |

20 | Complexity : 21 |

22 | Time : O(n)
23 | Space : O(n),
24 | O(log(n)) for balanced tree. -------------------------------------------------------------------------------- /Sum Along Diaginals in Binary Tree/test/in/kuldeepyadav/google/interview/IntegerBinaryTreeTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import java.util.Arrays; 6 | import java.util.LinkedList; 7 | 8 | import org.junit.Test; 9 | 10 | /** 11 | * Junit test for {@link IntegerBinaryTree} 12 | * 13 | * @author kuldeep 14 | */ 15 | public class IntegerBinaryTreeTest { 16 | 17 | @Test 18 | public void test() { 19 | 20 | IntegerBinaryTree binaryTree = new IntegerBinaryTree(); 21 | 22 | // This is balanced binary tree just like a heap. 23 | binaryTree.insert(1,2,3,4,5,6,7,8,9); 24 | assertEquals(new LinkedList(Arrays.asList(15, 23, 7)), 25 | binaryTree.sumAlongDiagonals()); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Sum of Bit Difference/.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | *.class 3 | .mtj.tmp/ 4 | *.jar 5 | *.war 6 | *.ear 7 | hs_err_pid* 8 | .idea/ 9 | .idea/tasks.xml 10 | .idea/dictionaries 11 | .idea/jsLibraryMappings.xml 12 | .idea/dataSources.ids 13 | .idea/dataSources.xml 14 | .idea/dataSources.local.xml 15 | .idea/sqlDataSources.xml 16 | .idea/dynamic.xml 17 | .idea/uiDesigner.xml 18 | .idea/gradle.xml 19 | .idea/mongoSettings.xml 20 | *.iws 21 | /out/ 22 | .idea_modules/ 23 | atlassian-ide-plugin.xml 24 | com_crashlytics_export_strings.xml 25 | crashlytics.properties 26 | crashlytics-build.properties 27 | fabric.properties 28 | -------------------------------------------------------------------------------- /Sum of Bit Difference/src/test/java/com/inbhiwadi/BitsCounterTest.java: -------------------------------------------------------------------------------- 1 | package com.inbhiwadi; 2 | 3 | import junit.framework.TestCase; 4 | 5 | /** 6 | * Junit test for {@link BitsCounter}. 7 | *

8 | * Created by Kuldeep Yadav on 02/10/16. 9 | */ 10 | public class BitsCounterTest extends TestCase { 11 | public void testCountSumOfBitDifferences() throws Exception { 12 | 13 | int count = BitsCounter.countSumOfBitDifferences(new int[]{1, 2}); 14 | assertEquals(4, count); 15 | count = BitsCounter.countSumOfBitDifferences(new int[]{1, 3, 5}); 16 | assertEquals(8, count); 17 | } 18 | } -------------------------------------------------------------------------------- /Sum of Cube of Two equal to Cube of Third/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Sum of Cube of Two equal to Cube of Third/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Sum of Cube of Two equal to Cube of Third/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sum of Cube of Two equal to Cube of Third 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Sum of Cube of Two equal to Cube of Third/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Sum of Cube of Two equal to Cube of Third/test/in/kuldeepyadav/amazon/interview/SumOfCubesTest.java: -------------------------------------------------------------------------------- 1 | package in.kuldeepyadav.amazon.interview; 2 | 3 | import org.junit.Test; 4 | 5 | public class SumOfCubesTest { 6 | 7 | @Test 8 | public void test() { 9 | 10 | System.out.println(Integer.MAX_VALUE); 11 | System.out.println(Long.MAX_VALUE); 12 | SumOfCubes.printCubeTupples(900000); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Sum of Pair divisible by 'K'/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Sum of Pair divisible by 'K'/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Sum of Pair divisible by 'K'/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sum of Pair divisible by 'K' 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Sum of Pair divisible by 'K'/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Trapping Rain Water/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.kuldeep 5 | Trapping-Rain-Water 6 | jar 7 | 1.0-SNAPSHOT 8 | Trapping Rain Water 9 | http://maven.apache.org 10 | 11 | 12 | junit 13 | junit 14 | 3.8.2 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /Trapping Rain Water/src/main/java/com/kuldeep/TrapTest.java: -------------------------------------------------------------------------------- 1 | package com.kuldeep; 2 | 3 | import junit.framework.TestCase; 4 | 5 | /** 6 | * JUnit test for Trap 7 | *

8 | * Created by kuldeep on 01/05/16. 9 | */ 10 | public class TrapTest extends TestCase { 11 | 12 | public void testCalculateWater() throws Exception { 13 | 14 | int volume = Trap.calculateWater(new int[]{1, 2, 3, 4, 5, 4, 3, 2, 1}); 15 | assertEquals(20, volume); 16 | 17 | volume = Trap.calculateWater(new int[]{2, 5, 3, 6, 7, 2, 4, 1}); 18 | assertEquals(27, volume); 19 | } 20 | } -------------------------------------------------------------------------------- /Two Jugs/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /Two Jugs/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Two Jugs/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Two Jugs 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Two Jugs/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Two Jugs/src/in/interview/housing/Jugs.java: -------------------------------------------------------------------------------- 1 | package in.interview.housing; 2 | 3 | import in.interview.google.MathUtils; 4 | 5 | /** 6 | * Two jugs 7 | * 8 | * @author kuldeep 9 | */ 10 | public class Jugs { 11 | 12 | public static boolean isPossible(int firstJugCapacity, int secondJugCapacity, int fillUpTo) { 13 | 14 | if (fillUpTo > Math.max(firstJugCapacity, secondJugCapacity)) 15 | return false; 16 | 17 | int gcd = MathUtils.gcd(firstJugCapacity, secondJugCapacity); 18 | if(fillUpTo%gcd == 0) return true; 19 | else return false; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Two Jugs/test/in/interview/housing/JugsTest.java: -------------------------------------------------------------------------------- 1 | package in.interview.housing; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | import org.junit.Test; 6 | 7 | public class JugsTest { 8 | 9 | @Test 10 | public void testIsPossible() { 11 | 12 | assertEquals(true, Jugs.isPossible(5, 3, 4)); 13 | assertEquals(true, Jugs.isPossible(3, 5, 4)); 14 | assertEquals(false, Jugs.isPossible(3, 5, 8)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Walk Array/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Walk Array/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Walk Array/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Walk Array 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Walk Array/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Walk Array/pseudo code.md: -------------------------------------------------------------------------------- 1 | Question: minimum number of jumps. 2 | answer: take an array of size equal to given array wherev we will store minimum steps required to reach at that index. 3 | pseudo code: 4 | 1. Initialize jump array with zeros. 5 | 2. Process each entry: 6 | 2.1 Index of this entry in input is 'i'. Minimum jumps required to reach at 'i' be 'v'. Maximum jump that I can take from this position is equal to 'j'. Then go on overwriting entry from 'i+1' to 'i+j' with value 'v+1' if value at these entry are zero or larger than 'v+1'. Start from back will save time. 7 | 8 | Time: O(n) 9 | Space: O(n) 10 | -------------------------------------------------------------------------------- /Walk Array/test/com/kuldeep/google/interview/JumpTest.java: -------------------------------------------------------------------------------- 1 | package com.kuldeep.google.interview; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | import org.junit.Test; 6 | 7 | import com.kuldeep.google.interview.Jump; 8 | 9 | /** 10 | * Junit test for {@link Jump}. 11 | * 12 | * @author kuldeep 13 | */ 14 | public class JumpTest { 15 | 16 | @Test 17 | public void test() { 18 | 19 | int[] walk = new int[]{4,10,5,5,6,3}; 20 | Jump jump = new Jump(walk); 21 | assertEquals(2, jump.jump()); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /Zero Sum Triplets/.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.iml 3 | 4 | -------------------------------------------------------------------------------- /Zero Sum Triplets/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Statement: 2 | ===== 3 | Given an array of distinct elements. The task is to find triplets in array whose sum is zero. 4 | 5 | Solution 6 | ==== 7 | Brute force: 3 nested loops. 8 | 9 | Time: O(n^3) 10 | Space: O(n) 11 | 12 | Solution 13 | ====== 14 | 1. Sort the array 15 | 2. For every element in array start scanning from leftmost and rightmost elements. 16 | 17 | Time: O(n^2) 18 | Space: O(n) 19 | 20 | link: https://www.geeksforgeeks.org/find-triplets-array-whose-sum-equal-zero/ 21 | 22 | -------------------------------------------------------------------------------- /Zig Zag Sorting/.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | -------------------------------------------------------------------------------- /Zig Zag Sorting/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Zig Zag Sorting 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /Zig Zag Sorting/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 3 | org.eclipse.jdt.core.compiler.compliance=1.5 4 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 5 | org.eclipse.jdt.core.compiler.source=1.5 6 | -------------------------------------------------------------------------------- /Zig Zag Sorting/.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /Zig Zag Sorting/ALGORITHM.md: -------------------------------------------------------------------------------- 1 | Zig Zag Sorting 2 | =============== 3 | 4 |

5 | Statement : 6 |

7 | Given an array of integers, sort it in zig-zag order. That is second element is larger than first, 8 | third is smaller than second, fourth is larger than third and so on. There can be more than one such configurations, only one is needed to be found. 9 | 10 |

11 | Algorithm : 12 |

13 | If element at odd index is smaller than previous then swap it with that. 14 | If element at even index is larger than previous then swap it with that. 15 | Otherwise don't do anything. 16 | 17 |

18 | Complexity : 19 |

20 | Time : O(n)
21 | Space : O(1) -------------------------------------------------------------------------------- /Zig Zag Sorting/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | in.interview.microsoft 4 | zig-zag-sorting 5 | 0.1 6 | -------------------------------------------------------------------------------- /Zig Zag Sorting/src/main/java/in/interview/microsoft/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package in.interview.microsoft; 2 | 3 | /** 4 | * Array related utilities. 5 | * 6 | * @author kuldeep 7 | * 8 | */ 9 | public class ArrayUtils { 10 | 11 | /** 12 | * Sort an array in zig-zag pattern 13 | * i.e. second element is larger than first, 14 | * third is smaller than second, 15 | * fourth is larger than third, 16 | * and so on ... 17 | * @param array 18 | * input array 19 | * @return zig-zag sorted array 20 | */ 21 | public static int[] zigZagSort(int[] array) { 22 | 23 | for (int i = 1; i < array.length; i++) { 24 | if ((i % 2 == 1 && array[i] < array[i - 1]) || (i % 2 == 0 && array[i] > array[i - 1])) { 25 | int temp = array[i]; 26 | array[i] = array[i - 1]; 27 | array[i - 1] = temp; 28 | } 29 | } 30 | 31 | return array; 32 | } 33 | 34 | } 35 | --------------------------------------------------------------------------------