├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── arrays ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── AdvanceThroughArray.java │ │ ├── BuySellStockOnce.java │ │ ├── BuySellStockTwice.java │ │ ├── ComputePascalsTriangle.java │ │ ├── ComputeRandomPermutation.java │ │ ├── ComputeRandomSubset.java │ │ ├── ComputeSpiralOrdering.java │ │ ├── DeleteDuplicates.java │ │ ├── DutchNationalFlag.java │ │ ├── EnumeratePrimes.java │ │ ├── GenerateNonuniformRandomNumbers.java │ │ ├── IncrementArbitraryPrecisionInteger.java │ │ ├── MultipleArbitraryPrecisionIntegers.java │ │ ├── NextPermutation.java │ │ ├── PermuteElements.java │ │ ├── Rotate2DArray.java │ │ ├── SampleOfflineData.java │ │ ├── SampleOnlineData.java │ │ └── SudokuChecker.java │ └── test │ └── java │ ├── AdvanceThroughArrayTest.java │ ├── BuySellStockOnceTest.java │ ├── BuySellStockTwiceTest.java │ ├── ComputePascalsTriangleTest.java │ ├── ComputeRandomPermutationTest.java │ ├── ComputeRandomSubsetTest.java │ ├── ComputeSpiralOrderingTest.java │ ├── DeleteDuplicatesTest.java │ ├── DutchNationalFlagTest.java │ ├── EnumeratePrimesTest.java │ ├── GenerateNonuniformRandomNumbersTest.java │ ├── IncrementArbitraryPrecisionIntegerTest.java │ ├── MultipleArbitraryPrecisionIntegersTest.java │ ├── NextPermutationTest.java │ ├── PermuteElementsTest.java │ ├── Rotate2DArrayTest.java │ ├── SampleOfflineDataTest.java │ ├── SampleOnlineDataTest.java │ └── SudokuCheckerTest.java ├── binarysearchtrees ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── AreNodesOrdered.java │ │ ├── ClientCreditsInfo.java │ │ ├── ClosestEntries.java │ │ ├── ComputeLCA.java │ │ ├── EnumerateEntries.java │ │ ├── FindKLargest.java │ │ ├── FirstGreaterThan.java │ │ ├── InsertionAndDeletionBST.java │ │ ├── IsBST.java │ │ ├── MinimumHeightBST.java │ │ ├── MostVisitedPages.java │ │ ├── RangeLookup.java │ │ └── ReconstructBST.java │ └── test │ └── java │ ├── AreNodesOrderedTest.java │ ├── ClientCreditsInfoTest.java │ ├── ClosestEntriesTest.java │ ├── ComputeLCATest.java │ ├── EnumerateEntriesTest.java │ ├── FindKLargestTest.java │ ├── FirstGreaterThanTest.java │ ├── InsertionAndDeletionBSTTest.java │ ├── IsBSTTest.java │ ├── MinimumHeightBSTTest.java │ ├── MostVisitedPagesTest.java │ ├── RangeLookupTest.java │ └── ReconstructBSTTest.java ├── binarytrees ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── ComputeExterior.java │ │ ├── ComputeKthNodeInorder.java │ │ ├── ComputeLCAWithParent.java │ │ ├── ComputeLowestCommonAncestor.java │ │ ├── ComputeRightSiblingTree.java │ │ ├── ComputeSuccessor.java │ │ ├── FindRootToLeafSum.java │ │ ├── ImplementInorderSpaceEfficient.java │ │ ├── InorderIterative.java │ │ ├── IsHeightBalanced.java │ │ ├── IsSymmetric.java │ │ ├── LockingBinaryTree.java │ │ ├── PreorderIterative.java │ │ ├── ReconstructBinaryTree.java │ │ ├── ReconstructBinaryTreeWithMarkers.java │ │ ├── SumRootToLeaf.java │ │ └── TreeToLinkedList.java │ └── test │ └── java │ ├── ComputeExteriorTest.java │ ├── ComputeKthNodeInorderTest.java │ ├── ComputeLCAWithParentTest.java │ ├── ComputeLowestCommonAncestorTest.java │ ├── ComputeRightSiblingTreeTest.java │ ├── ComputeSuccessorTest.java │ ├── FindRootToLeafSumTest.java │ ├── ImplementInorderSpaceEfficientTest.java │ ├── InorderIterativeTest.java │ ├── IsHeightBalancedTest.java │ ├── IsSymmetricTest.java │ ├── LockingBinaryTreeTest.java │ ├── PreorderIterativeTest.java │ ├── ReconstructBinaryTreeTest.java │ ├── ReconstructBinaryTreeWithMarkersTest.java │ ├── SumRootToLeafTest.java │ └── TreeToLinkedListTest.java ├── datastructures ├── pom.xml └── src │ └── main │ └── java │ ├── BinaryTree.java │ ├── BinaryTreeLN.java │ ├── BinaryTreeParent.java │ ├── BuildingWithHeight.java │ ├── GraphVertex.java │ ├── ListNode.java │ ├── MinMax.java │ ├── PostingListNode.java │ ├── Rectangle.java │ ├── ServiceRequest.java │ ├── ServiceResponse.java │ ├── Star.java │ ├── TreeNode.java │ ├── Tuple.java │ └── Vertex.java ├── dynamicprogramming ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── BedBathBeyondProblem.java │ │ ├── ComputeBinomialCoefficients.java │ │ ├── ComputeLevenshtein.java │ │ ├── ComputeScoreCombinations.java │ │ ├── CountMovesToClimbStairs.java │ │ ├── CountPossibleTraversals.java │ │ ├── KnapsackProblem.java │ │ ├── LongestNondecreasingSubsequence.java │ │ ├── MaximumCoinsGain.java │ │ ├── MinimumWeightPathTriangle.java │ │ ├── PrettyPrintingProblem.java │ │ └── SearchSequence2D.java │ └── test │ └── java │ ├── BedBathBeyondProblemTest.java │ ├── ComputeBinomialCoefficientsTest.java │ ├── ComputeLevenshteinTest.java │ ├── ComputeScoreCombinationsTest.java │ ├── CountMovesToClimbStairsTest.java │ ├── CountPossibleTraversalsTest.java │ ├── KnapsackProblemTest.java │ ├── LongestNondecreasingSubsequenceTest.java │ ├── MaximumCoinsGainTest.java │ ├── MinimumWeightPathTriangleTest.java │ ├── PrettyPrintingProblemTest.java │ └── SearchSequence2DTest.java ├── eopi.jpg ├── graphs ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── CloneAGraph.java │ │ ├── ComputeEnclosedRegions.java │ │ ├── ComputeShortestPath.java │ │ ├── DeadlockDetection.java │ │ ├── MakingWiredConnections.java │ │ ├── PaintBooleanMatrix.java │ │ ├── SearchMaze.java │ │ ├── TeamPhotoDay.java │ │ └── TransformOneStringToAnother.java │ └── test │ └── java │ ├── CloneAGraphTest.java │ ├── ComputeEnclosedRegionsTest.java │ ├── ComputeShortestPathTest.java │ ├── DeadlockDetectionTest.java │ ├── MakingWiredConnectionsTest.java │ ├── PaintBooleanMatrixTest.java │ ├── SearchMazeTest.java │ ├── TeamPhotoDayTest.java │ └── TransformOneStringToAnotherTest.java ├── greedyalgorithms ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── ComputeLargestRectangle.java │ │ ├── ComputeMaximumWaterTrapped.java │ │ ├── ComputeOptimumAssignment.java │ │ ├── FindMajorityElement.java │ │ ├── GasUpProblem.java │ │ ├── IntervalCoveringProblem.java │ │ ├── ScheduleMinimizedWaitingTime.java │ │ └── ThreeSumProblem.java │ └── test │ └── java │ ├── ComputeLargestRectangleTest.java │ ├── ComputeMaximumWaterTrappedTest.java │ ├── ComputeOptimumAssignmentTest.java │ ├── FindMajorityElementTest.java │ ├── GasUpProblemTest.java │ ├── IntervalCoveringProblemTest.java │ ├── ScheduleMinimizedWaitingTimeTest.java │ └── ThreeSumProblemTest.java ├── hashtables ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── CollatzConjecture.java │ │ ├── ComputeAverageTopThree.java │ │ ├── ComputeKMostFrequent.java │ │ ├── ComputeLCA.java │ │ ├── ComputeStringDecompositions.java │ │ ├── HashFunctionChess.java │ │ ├── IsLetterConstructable.java │ │ ├── LRUCache.java │ │ ├── LongestContainedInterval.java │ │ ├── LongestSubarray.java │ │ ├── NearestRepeated.java │ │ ├── PalindromicPermutations.java │ │ ├── SmallestSequentialSubarray.java │ │ └── SmallestSubarray.java │ └── test │ └── java │ ├── CollatzConjectureTest.java │ ├── ComputeAverageTopThreeTest.java │ ├── ComputeKMostFrequentTest.java │ ├── ComputeLCATest.java │ ├── ComputeStringDecompositionsTest.java │ ├── IsLetterConstructableTest.java │ ├── LRUCacheTest.java │ ├── LongestContainedIntervalTest.java │ ├── LongestSubarrayTest.java │ ├── NearestRepeatedTest.java │ ├── PalindromicPermutationsTest.java │ ├── SmallestSequentialSubarrayTest.java │ └── SmallestSubarrayTest.java ├── heaps ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── ComputeKClosest.java │ │ ├── ComputeKLargest.java │ │ ├── ComputeMedianOnline.java │ │ ├── HeapStack.java │ │ ├── MergeSortedFiles.java │ │ ├── SortAlmostSorted.java │ │ └── SortIncDec.java │ └── test │ └── java │ ├── ComputeKClosestTest.java │ ├── ComputeKLargestTest.java │ ├── ComputeMedianOnlineTest.java │ ├── HeapStackTest.java │ ├── MergeSortedFilesTest.java │ ├── SortAlmostSortedTest.java │ └── SortIncDecTest.java ├── linkedlists ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── AddIntegers.java │ │ ├── CyclicRightShift.java │ │ ├── DeleteKthLastNode.java │ │ ├── DeleteNode.java │ │ ├── EvenOddMerge.java │ │ ├── FindOverlappingWithCycles.java │ │ ├── ListPivot.java │ │ ├── MergeSortedLists.java │ │ ├── Palindromic.java │ │ ├── RemoveDuplicatesFromSortedList.java │ │ ├── ReverseSingleSublist.java │ │ ├── TestCyclicity.java │ │ └── TestForOverlappingLists.java │ └── test │ └── java │ ├── AddIntegersTest.java │ ├── CyclicRightShiftTest.java │ ├── DeleteKthLastNodeTest.java │ ├── DeleteNodeTest.java │ ├── EvenOddMergeTest.java │ ├── FindOverlappingWithCyclesTest.java │ ├── LinkedListUtil.java │ ├── ListPivotTest.java │ ├── MergeSortedListsTest.java │ ├── PalindromicTest.java │ ├── RemoveDuplicatesFromSortedListTest.java │ ├── ReverseSingleSublistTest.java │ ├── TestCyclicityTest.java │ └── TestForOverlappingListsTest.java ├── parallelcomputing ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── AnalyzeUnsyncronizedThreads.java │ │ ├── Deadlock.java │ │ ├── ImplementCachingDictionary.java │ │ ├── ImplementThreadPool.java │ │ ├── ImplementThreadSyncronization.java │ │ ├── ImplementTimer.java │ │ ├── ReaderWriterProblem.java │ │ ├── ReaderWriterProblemPreference.java │ │ ├── SpellCheckService.java │ │ └── TestCollatzConjecture.java │ └── test │ └── java │ ├── DeadlockTest.java │ ├── EvenThreadTest.java │ ├── ReaderTest.java │ └── SafeSpellCheckServiceTest.java ├── pom.xml ├── primitives ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── CheckIfIntegerIsPalindrome.java │ │ ├── ComputeDivision.java │ │ ├── ComputeParity.java │ │ ├── ComputePower.java │ │ ├── FindClosestInteger.java │ │ ├── GenerateUniformRandomNumbers.java │ │ ├── MultiplicationNoArithmeticOperators.java │ │ ├── RectangleIntersection.java │ │ ├── ReverseBits.java │ │ ├── ReverseDigits.java │ │ └── SwapBits.java │ └── test │ └── java │ ├── CheckIfIntegerIsPalindromeTest.java │ ├── ComputeDivisionTest.java │ ├── ComputeParityTest.java │ ├── ComputePowerTest.java │ ├── FindClosestIntegerTest.java │ ├── GenerateUniformRandomNumbersTest.java │ ├── MultiplicationNoArithmeticOperatorsTest.java │ ├── RectangleIntersectionTest.java │ ├── ReverseBitsTest.java │ ├── ReverseDigitsTest.java │ └── SwapBitsTest.java ├── recursion ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── ComputeDiameter.java │ │ ├── ComputeGrayCode.java │ │ ├── GenerateBinaryTrees.java │ │ ├── GeneratePalindromicDecompositions.java │ │ ├── GeneratePermutations.java │ │ ├── GeneratePowerSet.java │ │ ├── GenerateStrings.java │ │ ├── GenerateSubsetsK.java │ │ ├── NQueens.java │ │ ├── SudokuSolver.java │ │ └── TowersOfHanoi.java │ └── test │ └── java │ ├── ComputeDiameterTest.java │ ├── ComputeGrayCodeTest.java │ ├── GenerateBinaryTreesTest.java │ ├── GeneratePalindromicDecompositionsTest.java │ ├── GeneratePermutationsTest.java │ ├── GeneratePowerSetTest.java │ ├── GenerateStringsTest.java │ ├── GenerateSubsetsKTest.java │ ├── NQueensTest.java │ ├── SudokuSolverTest.java │ └── TowersOfHanoiTest.java ├── searching ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── FindDuplicateAndMissing.java │ │ ├── FindKthLargest.java │ │ ├── FindMinAndMax.java │ │ ├── FindMissingIP.java │ │ ├── IntegerSquareRoot.java │ │ ├── RealSquareRoot.java │ │ ├── SearchSorted.java │ │ ├── SearchSorted2D.java │ │ ├── SearchSortedCyclic.java │ │ └── SearchSortedIndex.java │ └── test │ └── java │ ├── FindDuplicateAndMissingTest.java │ ├── FindKthLargestTest.java │ ├── FindMinAndMaxTest.java │ ├── IntegerSquareRootTest.java │ ├── RealSquareRootTest.java │ ├── SearchSorted2DTest.java │ ├── SearchSortedCyclicTest.java │ ├── SearchSortedIndexTest.java │ └── SearchSortedTest.java ├── sorting ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── ComputeIntersection.java │ │ ├── ComputeSalaryThreshold.java │ │ ├── ComputeUnion.java │ │ ├── ImplementFastSorting.java │ │ ├── MergeIntervals.java │ │ ├── MergeSorted.java │ │ ├── PartitionSortRepeats.java │ │ ├── RemoveFirstNameDuplicates.java │ │ ├── RenderCalendar.java │ │ └── TeamPhotoDay.java │ └── test │ └── java │ ├── ComputeIntersectionTest.java │ ├── ComputeSalaryThresholdTest.java │ ├── ComputeUnionTest.java │ ├── ImplementFastSortingTest.java │ ├── MergeIntervalsTest.java │ ├── MergeSortedTest.java │ ├── PartitionSortRepeatsTest.java │ ├── RemoveFirstNameDuplicatesTest.java │ ├── RenderCalendarTest.java │ └── TeamPhotoDayTest.java ├── stacksandqueues ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── CircularQueue.java │ │ ├── ComputeBinaryTreeNodes.java │ │ ├── ComputeBuildingsWithView.java │ │ ├── EvaluateRPNExpressions.java │ │ ├── IsStringWellFormed.java │ │ ├── NormalizedPathnames.java │ │ ├── QueueWithMax.java │ │ ├── QueueWithStacks.java │ │ ├── SearchPostingsList.java │ │ └── StackWithMax.java │ └── test │ └── java │ ├── CircularQueueTest.java │ ├── ComputeBinaryTreeNodesTest.java │ ├── ComputeBuildingsWithViewTest.java │ ├── EvaluateRPNExpressionsTest.java │ ├── IsStringWellFormedTest.java │ ├── NormalizedPathnamesTest.java │ ├── QueueWithMaxTest.java │ ├── QueueWithStacksTest.java │ ├── SearchPostingsListTest.java │ └── StackWithMaxTest.java ├── strings ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ ├── BaseConversion.java │ │ ├── ComputeMnemonicsPhoneNumber.java │ │ ├── ComputeValidIPAddresses.java │ │ ├── FindFirstOccurrenceOfSubstring.java │ │ ├── InterconvertStringAndInteger.java │ │ ├── LookAndSayProblem.java │ │ ├── PalindromeAlphanumeric.java │ │ ├── ReplaceAndRemove.java │ │ ├── ReverseWordsInASentence.java │ │ ├── RomanToDecimal.java │ │ ├── RunLengthEncoding.java │ │ ├── SpreadsheetColumnEncoding.java │ │ └── StringSinusoidal.java │ └── test │ └── java │ ├── BaseConversionTest.java │ ├── ComputeMnemonicsPhoneNumberTest.java │ ├── ComputeValidIPAddressesTest.java │ ├── FindFirstOccurrenceOfSubstringTest.java │ ├── InterconvertStringAndIntegerTest.java │ ├── LookAndSayProblemTest.java │ ├── PalindromeAlphanumericTest.java │ ├── ReplaceAndRemoveTest.java │ ├── ReverseWordsInASentenceTest.java │ ├── RomanToDecimalTest.java │ ├── RunLengthEncodingTest.java │ ├── SpreadsheetColumnEncodingTest.java │ └── StringSinusoidalTest.java └── utils ├── pom.xml └── src └── main └── java ├── AssertUtils.java ├── BinaryTreeUtil.java ├── NodeUtil.java ├── StreamUtil.java └── TreeNodeUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | .idea 3 | out 4 | *.iml 5 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Clay Gardner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /arrays/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6: Arrays 2 | 3 | - [ ] 6.1 DutchNationalFlag 4 | - [ ] 6.2 IncrementArbitraryPrecisionInteger 5 | - [ ] 6.3 MultipleArbitraryPrecisionInteger 6 | - [ ] 6.4 AdvanceThroughArray 7 | - [ ] 6.5 DeleteDuplicates 8 | - [ ] 6.6 BuySellStockOnce 9 | - [ ] 6.7 BuySellStockTwice 10 | - [ ] 6.8 EnumeratePrimes 11 | - [ ] 6.9 PermuteElements 12 | - [ ] 6.10 NextPermutation 13 | - [ ] 6.11 SampleOfflineData 14 | - [ ] 6.12 SampleOnlineData 15 | - [ ] 6.13 ComputeRandomPermutation 16 | - [ ] 6.14 ComputeRandomSubset 17 | - [ ] 6.15 GenerateNonuniformRandomNumbers 18 | - [ ] 6.16 SudokuChecker 19 | - [ ] 6.17 ComputeSpiralOrdering 20 | - [ ] 6.18 Rotate2DArray 21 | - [ ] 6.19 ComputePascalsTriangle -------------------------------------------------------------------------------- /arrays/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | arrays 13 | Chapter 6: Arrays 14 | 15 | 16 | 17 | gardncl 18 | utils 19 | 1.0 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /arrays/src/main/java/AdvanceThroughArray.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class AdvanceThroughArray { 4 | 5 | /* 6 | 6.4 7 | */ 8 | 9 | public static boolean arrayAdvance(List A) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /arrays/src/main/java/BuySellStockOnce.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class BuySellStockOnce { 4 | 5 | /* 6 | 6.6 7 | */ 8 | 9 | public static int buySellStockOnce(List A) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /arrays/src/main/java/BuySellStockTwice.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class BuySellStockTwice { 4 | 5 | /* 6 | 6.7 7 | */ 8 | 9 | public static int buySellStockTwice(List A) { 10 | 11 | return 0; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/ComputePascalsTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputePascalsTriangle { 5 | 6 | /* 7 | 6.19 8 | */ 9 | 10 | public static List> generatePascalTriangle(int n) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/ComputeRandomPermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeRandomPermutation { 5 | 6 | /* 7 | 6.13 8 | */ 9 | 10 | public static List computeRandomPermutation(int n) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /arrays/src/main/java/ComputeRandomSubset.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeRandomSubset { 5 | 6 | /* 7 | 6.14 8 | */ 9 | 10 | public static List randomSubset(int n, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/ComputeSpiralOrdering.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeSpiralOrdering { 5 | 6 | /* 7 | 6.17 8 | */ 9 | 10 | public static List matrixInSpiralOrder(List> squareMatrix) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/DeleteDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class DeleteDuplicates { 4 | 5 | /* 6 | 6.5 7 | */ 8 | 9 | public static int deleteDuplicates(List A) { 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /arrays/src/main/java/DutchNationalFlag.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class DutchNationalFlag { 4 | 5 | /* 6 | 6.1 7 | */ 8 | 9 | public static void dutchNationalFlag(int p, List A) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /arrays/src/main/java/EnumeratePrimes.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.List; 3 | 4 | public class EnumeratePrimes { 5 | 6 | /* 7 | 6.8 8 | */ 9 | 10 | public static List enumeratePrimes(int n) { 11 | return Arrays.asList(1); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /arrays/src/main/java/GenerateNonuniformRandomNumbers.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class GenerateNonuniformRandomNumbers { 4 | 5 | /* 6 | 6.15 7 | */ 8 | 9 | public static int getRandom(List values, List probabilities) { 10 | 11 | return 1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /arrays/src/main/java/IncrementArbitraryPrecisionInteger.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class IncrementArbitraryPrecisionInteger { 4 | 5 | /* 6 | 6.2 7 | */ 8 | 9 | public static List incrementInteger(List A) { 10 | return A; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /arrays/src/main/java/MultipleArbitraryPrecisionIntegers.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class MultipleArbitraryPrecisionIntegers { 5 | 6 | /* 7 | 6.3 8 | */ 9 | 10 | public static List multiply(List a, List b) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/NextPermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class NextPermutation { 5 | 6 | /* 7 | 6.10 8 | */ 9 | 10 | public static List nextPermutation(List permutation) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/PermuteElements.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class PermuteElements { 4 | 5 | /* 6 | 6.9 7 | */ 8 | 9 | public static void applyPermutation(List perm, List a) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /arrays/src/main/java/Rotate2DArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class Rotate2DArray { 5 | 6 | /* 7 | 6.18 8 | */ 9 | 10 | public static List> rotateMatix(List> squareMatrix) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/SampleOfflineData.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SampleOfflineData { 4 | 5 | /* 6 | 6.11 7 | */ 8 | 9 | public static void randomSampling(int k, List list) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /arrays/src/main/java/SampleOnlineData.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class SampleOnlineData { 5 | 6 | /* 7 | 6.12 8 | */ 9 | 10 | public static List runningSample(int k, List sequence) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arrays/src/main/java/SudokuChecker.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SudokuChecker { 4 | 5 | /* 6 | 6.16 7 | */ 8 | 9 | public static boolean isValidSudoku(List> partialAssignment) { 10 | 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /arrays/src/test/java/AdvanceThroughArrayTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class AdvanceThroughArrayTest { 9 | 10 | private List array; 11 | private boolean possible; 12 | 13 | @Test 14 | public void arrayAdvance1() { 15 | array = Arrays.asList(1,2,0,0);; 16 | possible = true; 17 | 18 | test(array, possible); 19 | } 20 | 21 | @Test 22 | public void arrayAdvance2() { 23 | array = Arrays.asList(1,1,0,0);; 24 | possible = false; 25 | 26 | test(array, possible); 27 | } 28 | 29 | @Test 30 | public void arrayAdvance3() { 31 | array = Arrays.asList(1);; 32 | possible = true; 33 | 34 | test(array, possible); 35 | } 36 | 37 | private void test(List array, boolean possible) { 38 | assertEquals(AdvanceThroughArray.arrayAdvance(array), possible); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /arrays/src/test/java/BuySellStockOnceTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class BuySellStockOnceTest { 9 | 10 | private List prices; 11 | private int maxProfit; 12 | 13 | @Test 14 | public void buySellStockOnce1() { 15 | prices = Arrays.asList(310, 315, 275, 295, 260, 270, 290, 230, 255, 250); 16 | maxProfit = 30; 17 | 18 | test(prices, maxProfit); 19 | } 20 | 21 | @Test 22 | public void buySellStockOnce2() { 23 | prices = Arrays.asList(100, 200); 24 | maxProfit = 100; 25 | 26 | test(prices, maxProfit); 27 | } 28 | 29 | @Test 30 | public void buySellStockOnce3() { 31 | prices = Arrays.asList(200, 100); 32 | maxProfit = 0; 33 | 34 | test(prices, maxProfit); 35 | } 36 | 37 | @Test 38 | public void buySellStockOnce4() { 39 | prices = Arrays.asList(2, 4, 2, 6, 3, 0, 5); 40 | maxProfit = 5; 41 | 42 | test(prices, maxProfit); 43 | } 44 | 45 | private void test(List prices, int maxProfit) { 46 | assertEquals(maxProfit, BuySellStockOnce.buySellStockOnce(prices)); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /arrays/src/test/java/BuySellStockTwiceTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class BuySellStockTwiceTest { 9 | private List prices; 10 | private int maxProfit; 11 | 12 | @Test 13 | public void buySellStockTwice1() { 14 | prices = Arrays.asList(310, 315, 275, 295, 260, 270, 290, 230, 255, 250); 15 | maxProfit = 55; 16 | 17 | test(prices, maxProfit); 18 | } 19 | 20 | @Test 21 | public void buySellStockTwice2() { 22 | prices = Arrays.asList(100, 200); 23 | maxProfit = 100; 24 | 25 | test(prices, maxProfit); 26 | } 27 | 28 | @Test 29 | public void buySellStockTwice3() { 30 | prices = Arrays.asList(200, 100); 31 | maxProfit = 0; 32 | 33 | test(prices, maxProfit); 34 | } 35 | 36 | @Test 37 | public void buySellStockTwice4() { 38 | prices = Arrays.asList(2, 4, 2, 6, 3, 0, 5); 39 | maxProfit = 9; 40 | 41 | test(prices, maxProfit); 42 | } 43 | 44 | private void test(List prices, int maxProfit) { 45 | assertEquals(maxProfit, BuySellStockTwice.buySellStockTwice(prices)); 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /arrays/src/test/java/ComputeRandomPermutationTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.List; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class ComputeRandomPermutationTest { 8 | 9 | private int n; 10 | 11 | @Test 12 | public void computeRandomPermutation1() { 13 | n = 1; 14 | 15 | test(n); 16 | } 17 | 18 | @Test 19 | public void computeRandomPermutation2() { 20 | n = 10; 21 | 22 | test(n); 23 | } 24 | 25 | @Test 26 | public void computeRandomPermutation3() { 27 | n = 100; 28 | 29 | test(n); 30 | } 31 | 32 | private void test(int n) { 33 | final List sequence = StreamUtil.sequence(n); 34 | final List result = ComputeRandomPermutation.computeRandomPermutation(n); 35 | assertNotEquals(sequence, result); 36 | assertEquals(sequence.size(), result.size()); 37 | for (Integer i : sequence) { 38 | assertTrue(result.contains(i)); 39 | result.remove(i); 40 | } 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /arrays/src/test/java/ComputeRandomSubsetTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.List; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class ComputeRandomSubsetTest { 8 | 9 | private int n; 10 | private int k; 11 | 12 | @Test 13 | public void randomSubset1() { 14 | n = 5; 15 | k = 3; 16 | 17 | test(n, k); 18 | } 19 | 20 | @Test 21 | public void randomSubset2() { 22 | n = 50; 23 | k = 15; 24 | 25 | test(n, k); 26 | } 27 | 28 | @Test 29 | public void randomSubset3() { 30 | n = 500; 31 | k = 50; 32 | 33 | test(n, k); 34 | } 35 | 36 | private void test(int n, int k) { 37 | final List sequence = StreamUtil.sequence(n); 38 | final List subset = ComputeRandomSubset.randomSubset(n, k); 39 | assertNotEquals(sequence, subset); 40 | assertEquals(k, subset.size()); 41 | for (Integer i : subset) { 42 | assertTrue(sequence.contains(i)); 43 | sequence.remove(i); 44 | } 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /arrays/src/test/java/ComputeSpiralOrderingTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class ComputeSpiralOrderingTest { 9 | 10 | private List expected; 11 | private List> matrix; 12 | 13 | @Test 14 | public void matrixInSpiralOrder1() { 15 | expected = Arrays.asList(1,2,3,6,9,8,7,4,5); 16 | matrix = Arrays.asList( 17 | Arrays.asList(1,2,3), 18 | Arrays.asList(4,5,6), 19 | Arrays.asList(7,8,9) 20 | ); 21 | 22 | test(expected, matrix); 23 | } 24 | 25 | @Test 26 | public void matrixInSpiralOrder2() { 27 | expected = Arrays.asList(1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10); 28 | matrix = Arrays.asList( 29 | Arrays.asList(1,2,3,4), 30 | Arrays.asList(5,6,7,8), 31 | Arrays.asList(9,10,11,12), 32 | Arrays.asList(13,14,15,16) 33 | ); 34 | 35 | test(expected, matrix); 36 | } 37 | 38 | private void test(List expected, List> matrix) { 39 | assertEquals(expected, ComputeSpiralOrdering.matrixInSpiralOrder(matrix)); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /arrays/src/test/java/EnumeratePrimesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class EnumeratePrimesTest { 9 | 10 | private List primes; 11 | private int n; 12 | 13 | @Test 14 | public void enumeratePrimes1() { 15 | primes = Arrays.asList(); 16 | n = 1; 17 | 18 | test(primes, n); 19 | } 20 | 21 | @Test 22 | public void enumeratePrimes2() { 23 | primes = Arrays.asList(2, 3, 5, 7, 11, 13, 17); 24 | n = 18; 25 | 26 | test(primes, n); 27 | } 28 | 29 | void test(List primes, int n) { 30 | assertEquals(primes, EnumeratePrimes.enumeratePrimes(n)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /arrays/src/test/java/NextPermutationTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class NextPermutationTest { 9 | 10 | private List expected; 11 | private List input; 12 | 13 | @Test 14 | public void nextPermutation1() { 15 | expected = Arrays.asList(1, 2, 0, 3); 16 | input = Arrays.asList(1, 0, 3, 2); 17 | 18 | test(expected, input); 19 | } 20 | 21 | @Test 22 | public void nextPermutation2() { 23 | expected = Arrays.asList(); 24 | input = Arrays.asList(3, 2, 1, 0); 25 | 26 | test(expected, input); 27 | } 28 | 29 | @Test 30 | public void nextPermutation3() { 31 | expected = Arrays.asList(2, 1, 0); 32 | input = Arrays.asList(2, 0, 1); 33 | 34 | test(expected, input); 35 | } 36 | 37 | private void test(List expected, List input) { 38 | assertEquals(expected, NextPermutation.nextPermutation(input)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /arrays/src/test/java/PermuteElementsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class PermuteElementsTest { 9 | 10 | List expected; 11 | List perm; 12 | List a; 13 | 14 | 15 | @Test 16 | public void applyPermutation1() { 17 | expected = Arrays.asList(2, 1); 18 | perm = Arrays.asList(1, 0); 19 | a = Arrays.asList(1, 2); 20 | 21 | test(expected, perm, a); 22 | } 23 | 24 | @Test 25 | public void applyPermutation2() { 26 | expected = Arrays.asList(1); 27 | perm = Arrays.asList(0); 28 | a = Arrays.asList(1); 29 | 30 | test(expected, perm, a); 31 | } 32 | 33 | @Test 34 | public void applyPermutation3() { 35 | expected = Arrays.asList(150, 50, 100, 200); 36 | perm = Arrays.asList(2, 0, 1, 3); 37 | a = Arrays.asList(50, 100, 150, 200); 38 | 39 | test(expected, perm, a); 40 | } 41 | 42 | private void test(List expected, List perm, List a) { 43 | PermuteElements.applyPermutation(perm, a); 44 | assertEquals(expected, a); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /arrays/src/test/java/Rotate2DArrayTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class Rotate2DArrayTest { 9 | 10 | private List> expected; 11 | private List> matrix; 12 | 13 | 14 | @Test 15 | public void rotateMatix1() { 16 | expected = Arrays.asList( 17 | Arrays.asList(13,9,5,1), 18 | Arrays.asList(14,10,6,2), 19 | Arrays.asList(15,11,7,3), 20 | Arrays.asList(16,12,8,4) 21 | ); 22 | matrix = Arrays.asList( 23 | Arrays.asList(1,2,3,4), 24 | Arrays.asList(5,6,7,8), 25 | Arrays.asList(9,10,11,12), 26 | Arrays.asList(13,14,15,16) 27 | ); 28 | 29 | test(expected, matrix); 30 | } 31 | 32 | private void test(List> expected, List> matrix) { 33 | assertEquals(expected, Rotate2DArray.rotateMatix(matrix)); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /arrays/src/test/java/SampleOnlineDataTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class SampleOnlineDataTest { 10 | 11 | private int k; 12 | private List input; 13 | 14 | @Test 15 | public void runningSample1() { 16 | k = 3; 17 | input = Arrays.asList(1, 2, 3, 4, 5); 18 | 19 | test(k, input); 20 | } 21 | 22 | @Test 23 | public void runningSample2() { 24 | k = 3; 25 | input = Arrays.asList(1, 2, 2, 3, 3, 3, 4, 4, 4, 4); 26 | 27 | test(k, input); 28 | } 29 | 30 | @Test 31 | public void runningSample3() { 32 | k = 5; 33 | input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4, 5, 5); 34 | 35 | test(k, input); 36 | } 37 | 38 | private void test(int k, List input) { 39 | final List result = SampleOnlineData.runningSample(k, input); 40 | assertEquals(k, result.size()); 41 | for (Integer i : result) { 42 | assertTrue(input.contains(i)); 43 | input.remove(i); 44 | } 45 | 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /binarysearchtrees/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 15: Binary Search Trees 2 | 3 | - [ ] 15.1 IsBST 4 | - [ ] 15.2 FirstGreaterThan 5 | - [ ] 15.3 FindKLargest 6 | - [ ] 15.4 ComputeLCA 7 | - [ ] 15.5 ReconstructBST 8 | - [ ] 15.6 ClosestEntries 9 | - [ ] 15.7 EnumerateEntries 10 | - [ ] 15.8 MostVisitedPages 11 | - [ ] 15.9 MinimumHeightBST 12 | - [ ] 15.10 InsertionAndDeletionBST 13 | - [ ] 15.11 AreNodesOrdered 14 | - [ ] 15.12 RangeLookup 15 | - [ ] 15.13 ClientCreditsInfo -------------------------------------------------------------------------------- /binarysearchtrees/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | binarysearchtrees 13 | Chapter 15: Binary Search Trees 14 | 15 | 16 | 17 | gardncl 18 | utils 19 | 1.0 20 | 21 | 22 | gardncl 23 | datastructures 24 | 1.0 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/AreNodesOrdered.java: -------------------------------------------------------------------------------- 1 | public class AreNodesOrdered { 2 | 3 | /* 4 | 15.11 5 | */ 6 | 7 | public static boolean totallyOrdered(BinaryTree possible1, 8 | BinaryTree possible2, 9 | BinaryTree middle) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/ClientCreditsInfo.java: -------------------------------------------------------------------------------- 1 | public class ClientCreditsInfo { 2 | 3 | /* 4 | 15.13 5 | */ 6 | 7 | public void insert(String clientID, int c) { 8 | 9 | } 10 | 11 | public boolean remove(String clientID) { 12 | 13 | return false; 14 | } 15 | 16 | public int lookup(String clientID) { 17 | 18 | return -1; 19 | } 20 | 21 | public void addAll(int c) { 22 | 23 | } 24 | 25 | public String max() { 26 | 27 | return ""; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/ClosestEntries.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ClosestEntries { 4 | 5 | /* 6 | 15.6 7 | */ 8 | 9 | public static int findMin(List> sortedArrays) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/ComputeLCA.java: -------------------------------------------------------------------------------- 1 | public class ComputeLCA { 2 | 3 | /* 4 | 15.4 5 | */ 6 | 7 | public static BinaryTree findLCA(BinaryTree tree, 8 | BinaryTree s, 9 | BinaryTree b) { 10 | 11 | return new BinaryTree<>(0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/EnumerateEntries.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class EnumerateEntries { 5 | 6 | /* 7 | 15.7 8 | */ 9 | 10 | public static class ABSqrt2 implements Comparable { 11 | public int a, b; 12 | public double val; 13 | 14 | public ABSqrt2(int a, int b) { 15 | this.a = a; 16 | this.b = b; 17 | this.val = a + b * Math.sqrt(2); 18 | } 19 | 20 | @Override 21 | public int compareTo(ABSqrt2 o) { 22 | return Double.compare(val, o.val); 23 | } 24 | } 25 | 26 | public static List generateFirst(int k) { 27 | 28 | return Collections.emptyList(); 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/FindKLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class FindKLargest { 5 | 6 | /* 7 | 15.3 8 | */ 9 | 10 | public static List findLargest(BinaryTree tree, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/FirstGreaterThan.java: -------------------------------------------------------------------------------- 1 | public class FirstGreaterThan { 2 | 3 | /* 4 | 15.2 5 | */ 6 | 7 | public static BinaryTree find(BinaryTree tree, Integer k) { 8 | 9 | return new BinaryTree<>(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/InsertionAndDeletionBST.java: -------------------------------------------------------------------------------- 1 | public class InsertionAndDeletionBST extends BinaryTree { 2 | 3 | /* 4 | 15.10 5 | */ 6 | 7 | public InsertionAndDeletionBST(Integer data) { 8 | super(data); 9 | } 10 | 11 | public boolean insert(Integer key) { 12 | 13 | return false; 14 | } 15 | 16 | public boolean delete(Integer key) { 17 | 18 | return false; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/IsBST.java: -------------------------------------------------------------------------------- 1 | public class IsBST { 2 | 3 | /* 4 | 15.1 5 | */ 6 | 7 | public static boolean isBST(BinaryTree tree) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/MinimumHeightBST.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MinimumHeightBST { 4 | 5 | /* 6 | 15.9 7 | */ 8 | 9 | public static BinaryTree build(List A) { 10 | 11 | return new BinaryTree<>(0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/MostVisitedPages.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class MostVisitedPages { 5 | 6 | /* 7 | 15.8 8 | */ 9 | 10 | public static List findMostVisited(List pages, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/RangeLookup.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class RangeLookup { 5 | 6 | /* 7 | 15.12 8 | */ 9 | 10 | public static List range(BinaryTree tree, 11 | Tuple tuple) { 12 | 13 | return Collections.emptyList(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /binarysearchtrees/src/main/java/ReconstructBST.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ReconstructBST { 4 | 5 | /* 6 | 15.5 7 | */ 8 | 9 | public static BinaryTree reconstruct(List preOrder) { 10 | 11 | return new BinaryTree<>(0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/ClosestEntriesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ClosestEntriesTest { 9 | 10 | private int expected; 11 | private List> sortedArrays; 12 | 13 | @Test 14 | public void findMin1() throws Exception { 15 | expected = 1; 16 | sortedArrays = Arrays.asList( 17 | Arrays.asList(5,10,15), 18 | Arrays.asList(3,6,9,12,15), 19 | Arrays.asList(8,16,24) 20 | ); 21 | 22 | test(expected, sortedArrays); 23 | } 24 | 25 | @Test 26 | public void findMin2() throws Exception { 27 | expected = 7; 28 | sortedArrays = Arrays.asList( 29 | Arrays.asList(1,2,3), 30 | Arrays.asList(3,5,7,9), 31 | Arrays.asList(9,10,11) 32 | ); 33 | 34 | test(expected, sortedArrays); 35 | } 36 | 37 | private void test(int expected, List> sortedArrays) { 38 | assertEquals(expected, ClosestEntries.findMin(sortedArrays)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/ComputeLCATest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class ComputeLCATest { 6 | 7 | private BinaryTree expected; 8 | private BinaryTree tree; 9 | private BinaryTree s; 10 | private BinaryTree b; 11 | 12 | @Test 13 | public void findLCA1() throws Exception { 14 | tree = BinaryTreeUtil.getEvenBST(); 15 | expected = tree; 16 | s = tree.left; 17 | b = tree.right; 18 | 19 | test(expected, tree, s, b); 20 | } 21 | 22 | @Test 23 | public void findLCA2() throws Exception { 24 | tree = BinaryTreeUtil.getFigureFifteenDotOne(); 25 | expected = tree.left; 26 | s = tree.left.left.right; 27 | b = tree.left.right.right.left; 28 | 29 | test(expected, tree, s, b); 30 | } 31 | 32 | private void test(BinaryTree expected, 33 | BinaryTree tree, 34 | BinaryTree s, 35 | BinaryTree b) { 36 | assertEquals(expected, ComputeLCA.findLCA(tree, s, b)); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/FirstGreaterThanTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class FirstGreaterThanTest { 6 | 7 | private BinaryTree expected; 8 | private BinaryTree tree; 9 | private Integer k; 10 | 11 | @Test 12 | public void find1() throws Exception { 13 | tree = BinaryTreeUtil.getEvenBST(); 14 | expected = tree; 15 | k = 0; 16 | 17 | test(expected, tree, k); 18 | } 19 | 20 | @Test 21 | public void find2() throws Exception { 22 | tree = BinaryTreeUtil.getFigureFifteenDotOne(); 23 | expected = tree.right.left.right.left; 24 | k = 23; 25 | 26 | test(expected, tree, k); 27 | } 28 | 29 | @Test 30 | public void find3() throws Exception { 31 | tree = BinaryTreeUtil.getFigureFifteenDotOne(); 32 | 33 | test(expected, tree, k); 34 | } 35 | 36 | private void test(BinaryTree expected, BinaryTree tree, Integer k) { 37 | assertEquals(expected, FirstGreaterThan.find(tree,k)); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/InsertionAndDeletionBSTTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class InsertionAndDeletionBSTTest { 6 | 7 | @Test 8 | public void test1() { 9 | InsertionAndDeletionBST tree = new InsertionAndDeletionBST(2); 10 | tree.insert(0); 11 | tree.insert(1); 12 | assertEquals(tree, BinaryTreeUtil.getEvenBST()); 13 | } 14 | 15 | @Test 16 | public void test2() { 17 | InsertionAndDeletionBST tree = new InsertionAndDeletionBST(4); 18 | tree.insert(6); 19 | tree.insert(1); 20 | tree.insert(2); 21 | tree.insert(5); 22 | tree.insert(7); 23 | tree.insert(3); 24 | assertEquals(tree, BinaryTreeUtil.getFullBST()); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/IsBSTTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class IsBSTTest { 6 | 7 | private boolean expected; 8 | private BinaryTree tree; 9 | 10 | @Test 11 | public void isBST1() throws Exception { 12 | expected = false; 13 | tree = BinaryTreeUtil.getEvenTree(); 14 | 15 | test(expected, tree); 16 | } 17 | 18 | @Test 19 | public void isBST2() throws Exception { 20 | expected = true; 21 | tree = BinaryTreeUtil.getEvenBST(); 22 | 23 | test(expected, tree); 24 | } 25 | 26 | @Test 27 | public void isBST3() throws Exception { 28 | expected = false; 29 | tree = BinaryTreeUtil.getFigureTenDotOne(); 30 | 31 | test(expected, tree); 32 | } 33 | 34 | @Test 35 | public void isBST4() throws Exception { 36 | expected = true; 37 | tree = BinaryTreeUtil.getFigureFifteenDotOne(); 38 | 39 | test(expected, tree); 40 | } 41 | 42 | private void test(boolean expected, BinaryTree tree) { 43 | assertEquals(expected, IsBST.isBST(tree)); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/MinimumHeightBSTTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class MinimumHeightBSTTest { 9 | 10 | private BinaryTree expected; 11 | private List A; 12 | 13 | @Test 14 | public void build1() throws Exception { 15 | expected = BinaryTreeUtil.getEvenBST(); 16 | A = Arrays.asList(0,1,2); 17 | 18 | test(expected, A); 19 | } 20 | 21 | @Test 22 | public void build2() throws Exception { 23 | expected = BinaryTreeUtil.getFullBST(); 24 | A = Arrays.asList(1,2,3,4,5,6,7); 25 | 26 | test(expected, A); 27 | } 28 | 29 | @Test 30 | public void build3() throws Exception { 31 | expected = BinaryTreeUtil.getFullBST(); 32 | A = Arrays.asList(1,2,3,4,5,6,7); 33 | 34 | test(expected, A); 35 | } 36 | 37 | private void test(BinaryTree expected, List A) { 38 | assertEquals(expected, MinimumHeightBST.build(A)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /binarysearchtrees/src/test/java/ReconstructBSTTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class ReconstructBSTTest { 9 | 10 | private BinaryTree expected; 11 | private List preOrder; 12 | 13 | @Test 14 | public void reconstruct1() throws Exception { 15 | expected = BinaryTreeUtil.getEvenBST(); 16 | preOrder = Arrays.asList(1,0,2); 17 | 18 | test(expected, preOrder); 19 | } 20 | 21 | @Test 22 | public void reconstruct2() throws Exception { 23 | expected = BinaryTreeUtil.getFigureFifteenDotOne(); 24 | preOrder = Arrays.asList(19,7,3,2,5,11,17,13,43,23,37,29,31,41,47,53); 25 | 26 | test(expected, preOrder); 27 | } 28 | 29 | private void test(BinaryTree expected, List preOrder) { 30 | assertEquals(expected, ReconstructBST.reconstruct(preOrder)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /binarytrees/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 10: Binary Trees 2 | 3 | - [ ] 10.1 IsHeightBalanced 4 | - [ ] 10.2 IsSymmetric 5 | - [ ] 10.3 ComputeLowestCommonAncestor 6 | - [ ] 10.4 ComputeLCAWithParent 7 | - [ ] 10.5 SumRootToLeaf 8 | - [ ] 10.6 FindRootToLeafSum 9 | - [ ] 10.7 InorderIterative 10 | - [ ] 10.8 PreorderIterative 11 | - [ ] 10.9 ComputeKthNodeInorder 12 | - [ ] 10.10 ComputeSuccessor 13 | - [ ] 10.11 ImplementInorderSpaceEfficient 14 | - [ ] 10.12 ReconstructBinaryTree 15 | - [ ] 10.13 ReconstructBinaryTreeWithMarkers 16 | - [ ] 10.14 TreeToLinkedList 17 | - [ ] 10.15 ComputeExterior 18 | - [ ] 10.16 ComputeRightSiblingTree 19 | - [ ] 10.17 LockingBinaryTree -------------------------------------------------------------------------------- /binarytrees/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | binarytrees 13 | Chapter 10: Binary Trees 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeExterior.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeExterior { 5 | 6 | /* 7 | 10.15 8 | */ 9 | 10 | public static List> exteriorBinaryTree(BinaryTree tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeKthNodeInorder.java: -------------------------------------------------------------------------------- 1 | public class ComputeKthNodeInorder { 2 | 3 | /* 4 | 10.9 5 | */ 6 | 7 | public static BinaryTree findKthNodeBinaryTree(BinaryTree tree, int k) { 8 | 9 | return new BinaryTree(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeLCAWithParent.java: -------------------------------------------------------------------------------- 1 | public class ComputeLCAWithParent { 2 | 3 | /* 4 | 10.4 5 | */ 6 | 7 | public static BinaryTreeParent LCA(BinaryTreeParent node0, BinaryTreeParent node1) { 8 | 9 | return new BinaryTreeParent<>(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeLowestCommonAncestor.java: -------------------------------------------------------------------------------- 1 | public class ComputeLowestCommonAncestor { 2 | 3 | /* 4 | 10.3 5 | */ 6 | 7 | public static BinaryTree LCA(BinaryTree tree, BinaryTree node0, BinaryTree node1) { 8 | 9 | return new BinaryTree<>(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeRightSiblingTree.java: -------------------------------------------------------------------------------- 1 | public class ComputeRightSiblingTree { 2 | 3 | /* 4 | 10.16 5 | */ 6 | 7 | public static void constructRightSibling(BinaryTreeLN tree) { 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ComputeSuccessor.java: -------------------------------------------------------------------------------- 1 | public class ComputeSuccessor { 2 | 3 | /* 4 | 10.10 5 | */ 6 | 7 | public static BinaryTreeParent findSuccessor(BinaryTreeParent node) { 8 | 9 | return new BinaryTreeParent(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/FindRootToLeafSum.java: -------------------------------------------------------------------------------- 1 | public class FindRootToLeafSum { 2 | 3 | /* 4 | 10.6 5 | */ 6 | 7 | public static boolean hasPathSum(BinaryTree tree, int targetSum) { 8 | 9 | return false; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ImplementInorderSpaceEfficient.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ImplementInorderSpaceEfficient { 5 | 6 | /* 7 | 10.11 8 | */ 9 | 10 | public static List inorderTraversal(BinaryTreeParent tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/InorderIterative.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class InorderIterative { 5 | 6 | /* 7 | 10.7 8 | */ 9 | 10 | public static List BSTInOrder(BinaryTree tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/IsHeightBalanced.java: -------------------------------------------------------------------------------- 1 | public class IsHeightBalanced { 2 | 3 | /* 4 | 10.1 5 | */ 6 | 7 | public static boolean isBalanced(BinaryTree tree) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/IsSymmetric.java: -------------------------------------------------------------------------------- 1 | public class IsSymmetric { 2 | 3 | /* 4 | 10.2 5 | */ 6 | 7 | public static boolean isSymmetric(BinaryTree tree) { 8 | 9 | return false; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/LockingBinaryTree.java: -------------------------------------------------------------------------------- 1 | public class LockingBinaryTree extends BinaryTree { 2 | 3 | /* 4 | 10.17 5 | */ 6 | 7 | public LockingBinaryTree(Integer data) { 8 | super(data); 9 | } 10 | 11 | public boolean isLocked() { 12 | return false; 13 | } 14 | 15 | public boolean lock() { 16 | return false; 17 | } 18 | 19 | public void unlock() { 20 | 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/PreorderIterative.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class PreorderIterative { 5 | 6 | /* 7 | 10.8 8 | */ 9 | 10 | public static List BSTPreOrder(BinaryTree tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ReconstructBinaryTree.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ReconstructBinaryTree { 4 | 5 | /* 6 | 10.12 7 | */ 8 | 9 | public static BinaryTree binaryTreeFromPreorderInorder(List preorder, List inorder) { 10 | 11 | return new BinaryTree(0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/ReconstructBinaryTreeWithMarkers.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ReconstructBinaryTreeWithMarkers { 4 | 5 | /* 6 | 10.13 7 | */ 8 | 9 | public static BinaryTree reconstructPreorder(List preorder) { 10 | 11 | return new BinaryTree(0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/SumRootToLeaf.java: -------------------------------------------------------------------------------- 1 | public class SumRootToLeaf { 2 | 3 | /* 4 | 10.5 5 | */ 6 | 7 | public static int sumRootToLeaf(BinaryTree tree) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /binarytrees/src/main/java/TreeToLinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class TreeToLinkedList { 5 | 6 | /* 7 | 10.14 8 | */ 9 | 10 | public static List> createListOfLeaves(BinaryTree tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /binarytrees/src/test/java/InorderIterativeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class InorderIterativeTest { 9 | 10 | private List expected; 11 | private BinaryTree tree; 12 | 13 | @Test 14 | public void BSTInOrder1() throws Exception { 15 | expected = Arrays.asList(2,1,0); 16 | tree = BinaryTreeUtil.getOddTree(); 17 | 18 | test(expected, tree); 19 | } 20 | 21 | @Test 22 | public void BSTInOrder2() throws Exception { 23 | expected = Arrays.asList(1,2,3,4,5,6,7); 24 | tree = BinaryTreeUtil.getFullTree(); 25 | 26 | test(expected, tree); 27 | } 28 | 29 | @Test 30 | public void BSTInOrder3() throws Exception { 31 | expected = Arrays.asList(28,271,0,6,561,17,3,314,2,401,641,1,257,6,271,28); 32 | tree = BinaryTreeUtil.getFigureTenDotOne(); 33 | 34 | test(expected, tree); 35 | } 36 | 37 | private void test(List expected, BinaryTree tree) { 38 | assertEquals(expected, InorderIterative.BSTInOrder(tree)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /binarytrees/src/test/java/IsHeightBalancedTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class IsHeightBalancedTest { 6 | 7 | private boolean expected; 8 | private BinaryTree tree; 9 | 10 | @Test 11 | public void isBalanced1() throws Exception { 12 | expected = true; 13 | tree = BinaryTreeUtil.getEvenTree(); 14 | 15 | test(expected, tree); 16 | } 17 | 18 | @Test 19 | public void isBalanced2() throws Exception { 20 | expected = false; 21 | tree = BinaryTreeUtil.getOddTree(); 22 | 23 | test(expected, tree); 24 | } 25 | 26 | @Test 27 | public void isBalanced3() throws Exception { 28 | expected = true; 29 | tree = BinaryTreeUtil.getFullTree(); 30 | 31 | test(expected, tree); 32 | } 33 | 34 | @Test 35 | public void isBalanced4() throws Exception { 36 | expected = false; 37 | tree = BinaryTreeUtil.getFigureTenDotOne(); 38 | 39 | test(expected, tree); 40 | } 41 | 42 | private void test(boolean expected, BinaryTree tree) { 43 | assertEquals(expected, IsHeightBalanced.isBalanced(tree)); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /binarytrees/src/test/java/PreorderIterativeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class PreorderIterativeTest { 9 | 10 | private List expected; 11 | private BinaryTree tree; 12 | 13 | @Test 14 | public void BSTPreOrder1() throws Exception { 15 | expected = Arrays.asList(0,1,2); 16 | tree = BinaryTreeUtil.getOddTree(); 17 | 18 | test(expected, tree); 19 | } 20 | 21 | @Test 22 | public void BSTPreOrder2() throws Exception { 23 | expected = Arrays.asList(4,2,1,3,6,5,7); 24 | tree = BinaryTreeUtil.getFullTree(); 25 | 26 | test(expected, tree); 27 | } 28 | 29 | @Test 30 | public void BSTPreOrder3() throws Exception { 31 | expected = Arrays.asList(314,6,271,28,0,561,3,17,6,2,1,401,641,257,271,28); 32 | tree = BinaryTreeUtil.getFigureTenDotOne(); 33 | 34 | test(expected, tree); 35 | } 36 | 37 | private void test(List expected, BinaryTree tree) { 38 | assertEquals(expected, PreorderIterative.BSTPreOrder(tree)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /datastructures/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | datastructures 13 | Data Structures 14 | 15 | -------------------------------------------------------------------------------- /datastructures/src/main/java/BinaryTreeLN.java: -------------------------------------------------------------------------------- 1 | public class BinaryTreeLN extends BinaryTree { 2 | 3 | public BinaryTreeLN levelNext; 4 | 5 | public BinaryTreeLN(T data) { 6 | super(data); 7 | } 8 | 9 | @Override 10 | public boolean equals(Object o) { 11 | if (this == o) return true; 12 | if (o == null || getClass() != o.getClass()) return false; 13 | if (!super.equals(o)) return false; 14 | 15 | BinaryTreeLN that = (BinaryTreeLN) o; 16 | 17 | return levelNext != null ? levelNext.equals(that.levelNext) : that.levelNext == null; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /datastructures/src/main/java/BinaryTreeParent.java: -------------------------------------------------------------------------------- 1 | public class BinaryTreeParent { 2 | 3 | public BinaryTreeParent parent; 4 | public T data; 5 | public BinaryTreeParent left, right; 6 | public Boolean isVisited = false; 7 | 8 | public BinaryTreeParent(T data) { 9 | this.data = data; 10 | } 11 | 12 | public void setRight(BinaryTreeParent node) { 13 | this.right = node; 14 | node.parent = this; 15 | } 16 | 17 | public void setLeft(BinaryTreeParent node) { 18 | this.left = node; 19 | node.parent = this; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) return true; 25 | if (o == null || getClass() != o.getClass()) return false; 26 | if (!super.equals(o)) return false; 27 | 28 | BinaryTreeParent that = (BinaryTreeParent) o; 29 | 30 | return parent != null ? parent.equals(that.parent) : that.parent == null; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /datastructures/src/main/java/BuildingWithHeight.java: -------------------------------------------------------------------------------- 1 | public class BuildingWithHeight { 2 | 3 | public Integer id; 4 | public Integer height; 5 | 6 | public BuildingWithHeight(Integer id, Integer height) { 7 | this.id = id; 8 | this.height = height; 9 | } 10 | 11 | @Override 12 | public boolean equals(Object o) { 13 | BuildingWithHeight b = (BuildingWithHeight)o; 14 | if ((this.id == b.id) && (this.height == b.height)) 15 | return true; 16 | return false; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /datastructures/src/main/java/GraphVertex.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | public class GraphVertex { 6 | public int data; 7 | public List edges; 8 | public boolean visited; 9 | 10 | public GraphVertex(int data) { 11 | this.data = data; 12 | this.edges = new ArrayList<>(); 13 | this.visited = false; 14 | } 15 | 16 | public GraphVertex(int data, GraphVertex... graphVertices) { 17 | this.data = data; 18 | this.edges = Arrays.asList(graphVertices); 19 | this.visited = false; 20 | } 21 | 22 | @Override 23 | public boolean equals(Object o) { 24 | if (this == o) return true; 25 | if (o == null || getClass() != o.getClass()) return false; 26 | 27 | GraphVertex that = (GraphVertex) o; 28 | 29 | if (data != that.data) return false; 30 | return edges != null ? edges.equals(that.edges) : that.edges == null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /datastructures/src/main/java/MinMax.java: -------------------------------------------------------------------------------- 1 | public class MinMax { 2 | public Integer min; 3 | public Integer max; 4 | 5 | public MinMax(Integer min, Integer max) { 6 | this.min = min; 7 | this.max = max; 8 | } 9 | 10 | @Override 11 | public boolean equals(Object o) { 12 | if (this == o) return true; 13 | if (o == null || getClass() != o.getClass()) return false; 14 | 15 | MinMax minMax = (MinMax) o; 16 | 17 | if (min != null ? !min.equals(minMax.min) : minMax.min != null) return false; 18 | return max != null ? max.equals(minMax.max) : minMax.max == null; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /datastructures/src/main/java/PostingListNode.java: -------------------------------------------------------------------------------- 1 | public class PostingListNode { 2 | 3 | public T data; 4 | public PostingListNode next; 5 | public PostingListNode jump; 6 | 7 | public PostingListNode(T data) { 8 | this.data = data; 9 | } 10 | 11 | public PostingListNode(T data, PostingListNode next) { 12 | this.data = data; 13 | this.next = next; 14 | } 15 | 16 | public PostingListNode(T data, PostingListNode next, PostingListNode jump) { 17 | this.data = data; 18 | this.next = next; 19 | this.jump = jump; 20 | } 21 | 22 | public PostingListNode get(int n) { 23 | if (n == 0) { 24 | return this; 25 | } else if (n < 0) { 26 | return null; 27 | } else if (this.next == null){ 28 | throw new RuntimeException("Index out of bounds."); 29 | } else { 30 | return this.next.get(--n); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /datastructures/src/main/java/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle { 2 | public int x, y, width, height; 3 | 4 | public Rectangle(int x, int y, int width, int height) { 5 | this.x = x; 6 | this.y = y; 7 | this.width = width; 8 | this.height = height; 9 | } 10 | 11 | @Override 12 | public boolean equals(Object o) { 13 | if (this == o) return true; 14 | if (o == null || getClass() != o.getClass()) return false; 15 | 16 | Rectangle rectangle = (Rectangle) o; 17 | 18 | if (x != rectangle.x) return false; 19 | if (y != rectangle.y) return false; 20 | if (width != rectangle.width) return false; 21 | return height == rectangle.height; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /datastructures/src/main/java/ServiceRequest.java: -------------------------------------------------------------------------------- 1 | public class ServiceRequest { 2 | private String w; 3 | 4 | public ServiceRequest(String w) { 5 | this.w = w; 6 | } 7 | 8 | public String extractWordToCheckFromRequest() { 9 | return w; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /datastructures/src/main/java/ServiceResponse.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedHashMap; 2 | import java.util.List; 3 | 4 | public class ServiceResponse { 5 | private LinkedHashMap> closest; 6 | 7 | public LinkedHashMap> getClosest() { 8 | return this.closest; 9 | } 10 | 11 | public void encodeIntoResponse(LinkedHashMap> response) { 12 | this.closest = response; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /datastructures/src/main/java/Star.java: -------------------------------------------------------------------------------- 1 | public class Star implements Comparable { 2 | 3 | private double x, y, z; 4 | 5 | public Star(double x, double y, double z) { 6 | this.x = x; 7 | this.y = y; 8 | this.z = z; 9 | } 10 | 11 | public double distance() { 12 | return Math.sqrt(x * x + y * y + z * z); 13 | } 14 | 15 | @Override 16 | public int compareTo(Star rhs) { 17 | return Double.compare(this.distance(), rhs.distance()); 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return "Star{" + 23 | "x=" + x + 24 | ", y=" + y + 25 | ", z=" + z + 26 | '}'; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /datastructures/src/main/java/TreeNode.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class TreeNode { 5 | public List edges = new ArrayList<>(); 6 | 7 | public static class Edge { 8 | public TreeNode root; 9 | public double length; 10 | 11 | public Edge(double length) { 12 | this.length = length; 13 | root = new TreeNode(); 14 | } 15 | 16 | public Edge(TreeNode root, double length) { 17 | this.root = root; 18 | this.length = length; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /datastructures/src/main/java/Tuple.java: -------------------------------------------------------------------------------- 1 | public class Tuple { 2 | 3 | public Integer first; 4 | public Integer second; 5 | 6 | public Tuple() { 7 | } 8 | 9 | public Tuple(Integer first, Integer second) { 10 | this.first = first; 11 | this.second = second; 12 | } 13 | 14 | @Override 15 | public boolean equals(Object o) { 16 | if (this == o) return true; 17 | if (o == null || getClass() != o.getClass()) return false; 18 | 19 | Tuple that = (Tuple) o; 20 | 21 | if (first != null ? !first.equals(that.first) : that.first != null) return false; 22 | return second != null ? second.equals(that.second) : that.second == null; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /datastructures/src/main/java/Vertex.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class Vertex { 5 | public Map edges = new HashMap<>(); 6 | public Character id; 7 | public boolean visited = false; 8 | 9 | public Vertex(Character id) { 10 | this.id = id; 11 | } 12 | 13 | @Override 14 | public boolean equals(Object o) { 15 | if (this == o) return true; 16 | if (o == null || getClass() != o.getClass()) return false; 17 | 18 | Vertex vertex = (Vertex) o; 19 | 20 | if (visited != vertex.visited) return false; 21 | if (edges != null ? !edges.equals(vertex.edges) : vertex.edges != null) return false; 22 | return id != null ? id.equals(vertex.id) : vertex.id == null; 23 | } 24 | } -------------------------------------------------------------------------------- /dynamicprogramming/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 17: Dynamic Programming 2 | 3 | - [ ] 17.1 ComputeScoreCombinations 4 | - [ ] 17.2 ComputeLevenshtein 5 | - [ ] 17.3 CountPossibleTraversals 6 | - [ ] 17.4 ComputeBinomialCoefficients 7 | - [ ] 17.5 SearchSequence2D 8 | - [ ] 17.6 KnapsackProblem 9 | - [ ] 17.7 BedBathBeyondProblem 10 | - [ ] 17.8 MinimumWeightPathTriangle 11 | - [ ] 17.9 MaximumCoinsGain 12 | - [ ] 17.10 CountMovesToClimbStairs 13 | - [ ] 17.11 PrettyPrintingProblem 14 | - [ ] 17.12 LongestNondecreasingSubsequence -------------------------------------------------------------------------------- /dynamicprogramming/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | dynamicprogramming 13 | Chapter 17: Dynamic Programming 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/BedBathBeyondProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | import java.util.Set; 4 | 5 | public class BedBathBeyondProblem { 6 | 7 | /* 8 | 17.7 9 | */ 10 | 11 | public static List decompose(String domain, Set dictionary) { 12 | 13 | return Collections.emptyList(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/ComputeBinomialCoefficients.java: -------------------------------------------------------------------------------- 1 | public class ComputeBinomialCoefficients { 2 | 3 | /* 4 | 17.4 5 | */ 6 | 7 | public static int compute(int n, int k) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/ComputeLevenshtein.java: -------------------------------------------------------------------------------- 1 | public class ComputeLevenshtein { 2 | 3 | /* 4 | 17.2 5 | */ 6 | 7 | public static int levenschteinDistance(String A, String B) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/ComputeScoreCombinations.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ComputeScoreCombinations { 4 | 5 | /* 6 | 17.1 7 | */ 8 | 9 | public static int compute(int finalScore, List playScores) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/CountMovesToClimbStairs.java: -------------------------------------------------------------------------------- 1 | public class CountMovesToClimbStairs { 2 | 3 | /* 4 | 17.10 5 | */ 6 | 7 | public static int numberOfWays(int top, int maximumStep) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/CountPossibleTraversals.java: -------------------------------------------------------------------------------- 1 | public class CountPossibleTraversals { 2 | 3 | /* 4 | 17.3 5 | */ 6 | 7 | public static int numberOfWays(int n, int m) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/KnapsackProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class KnapsackProblem { 4 | 5 | /* 6 | 17.6 7 | */ 8 | 9 | public static int compute(List items, int capacity) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/LongestNondecreasingSubsequence.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class LongestNondecreasingSubsequence { 4 | 5 | /* 6 | 17.12 7 | */ 8 | 9 | public static int compute(List A) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/MaximumCoinsGain.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MaximumCoinsGain { 4 | 5 | /* 6 | 17.9 7 | */ 8 | 9 | public static int computeMaximum(List coins) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/MinimumWeightPathTriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MinimumWeightPathTriangle { 4 | 5 | /* 6 | 17.8 7 | */ 8 | 9 | public static int minimum(List> triangle) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/PrettyPrintingProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class PrettyPrintingProblem { 4 | 5 | /* 6 | 17.11 7 | */ 8 | 9 | public static int minimumMessiness(List words, int lineLength) { 10 | 11 | return 0; 12 | } 13 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/main/java/SearchSequence2D.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SearchSequence2D { 4 | 5 | /* 6 | 17.5 7 | */ 8 | 9 | public static boolean isContained(List> grid, List pattern) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/BedBathBeyondProblemTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | public class BedBathBeyondProblemTest { 11 | 12 | private List expected; 13 | private String domain; 14 | private Set dictionary; 15 | 16 | @Test 17 | public void decompose1() throws Exception { 18 | expected = Arrays.asList( 19 | "a", 20 | "man", 21 | "a", 22 | "plan", 23 | "a", 24 | "canal" 25 | ); 26 | domain = "amanaplanacanal"; 27 | dictionary = new HashSet<>(Arrays.asList( 28 | "a", 29 | "man", 30 | "plan", 31 | "canal" 32 | )); 33 | 34 | test(expected, domain, dictionary); 35 | } 36 | 37 | private static void test(List expected, String domain, Set dictionary) { 38 | assertEquals(expected, BedBathBeyondProblem.decompose(domain, dictionary)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/ComputeBinomialCoefficientsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class ComputeBinomialCoefficientsTest { 6 | 7 | private int expected; 8 | private int n; 9 | private int m; 10 | 11 | @Test 12 | public void compute1() throws Exception { 13 | expected = 10; 14 | n = 5; 15 | m = 2; 16 | 17 | test(expected, n, m); 18 | } 19 | 20 | @Test 21 | public void compute2() throws Exception { 22 | expected = 850668; 23 | n = 42; 24 | m = 37; 25 | 26 | test(expected, n, m); 27 | } 28 | 29 | @Test 30 | public void compute3() throws Exception { 31 | expected = 245157; 32 | n = 23; 33 | m = 7; 34 | 35 | test(expected, n, m); 36 | } 37 | 38 | private void test(int expected, int n, int m) { 39 | assertEquals(expected, ComputeBinomialCoefficients.compute(n, m)); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/ComputeLevenshteinTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class ComputeLevenshteinTest { 6 | 7 | private int expected; 8 | private String A; 9 | private String B; 10 | 11 | @Test 12 | public void levenschteinDistance1() throws Exception { 13 | expected = 4; 14 | A = "Saturday"; 15 | B = "Sunday"; 16 | 17 | test(expected, A, B); 18 | } 19 | 20 | @Test 21 | public void levenschteinDistance2() throws Exception { 22 | expected = 2; 23 | A = "book"; 24 | B = "back"; 25 | 26 | test(expected, A, B); 27 | } 28 | 29 | @Test 30 | public void levenschteinDistance3() throws Exception { 31 | expected = 9; 32 | A = "fantastic"; 33 | B = "excellent"; 34 | 35 | test(expected, A, B); 36 | } 37 | 38 | private void test(int expected, String A, String B) { 39 | assertEquals(expected, ComputeLevenshtein.levenschteinDistance(A,B)); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/ComputeScoreCombinationsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeScoreCombinationsTest { 9 | 10 | private int expected; 11 | private int finalScore; 12 | private List playScores; 13 | 14 | 15 | @Test 16 | public void compute1() throws Exception { 17 | expected = 4; 18 | finalScore = 12; 19 | playScores = Arrays.asList(2,3,7); 20 | 21 | test(expected,finalScore, playScores); 22 | } 23 | 24 | @Test 25 | public void compute2() throws Exception { 26 | expected = 2; 27 | finalScore = 9; 28 | playScores = Arrays.asList(2,3,7); 29 | 30 | test(expected,finalScore, playScores); 31 | } 32 | 33 | private void test(int expected, int finalScore, List playScores) { 34 | assertEquals(expected, ComputeScoreCombinations.compute(finalScore, playScores)); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/CountMovesToClimbStairsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class CountMovesToClimbStairsTest { 6 | 7 | private int expected; 8 | private int top; 9 | private int maximumStep; 10 | 11 | @Test 12 | public void numberOfWays1() throws Exception { 13 | expected = 5; 14 | top = 4; 15 | maximumStep = 2; 16 | 17 | test(expected, top, maximumStep); 18 | } 19 | 20 | @Test 21 | public void numberOfWays2() throws Exception { 22 | expected = 12; 23 | top = 5; 24 | maximumStep = 3; 25 | 26 | test(expected, top, maximumStep); 27 | } 28 | 29 | private void test(int expected, int top, int maximumStep) { 30 | assertEquals(expected, CountMovesToClimbStairs.numberOfWays(top, maximumStep)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/LongestNondecreasingSubsequenceTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class LongestNondecreasingSubsequenceTest { 9 | 10 | private int expected; 11 | private List A; 12 | 13 | @Test 14 | public void compute1() throws Exception { 15 | expected = 4; 16 | A = Arrays.asList(0,8,4,12,2,10,6,14,1,9); 17 | 18 | test(expected, A); 19 | } 20 | 21 | @Test 22 | public void compute2() throws Exception { 23 | expected = 5; 24 | A = Arrays.asList(0,8,4,12,2,10,6,14,1,14); 25 | 26 | test(expected, A); 27 | } 28 | 29 | @Test 30 | public void compute3() throws Exception { 31 | expected = 4; 32 | A = Arrays.asList(0,8,4,12,2,15,6,14,1,9); 33 | 34 | test(expected, A); 35 | } 36 | 37 | private void test(int expected, List A) { 38 | assertEquals(expected, LongestNondecreasingSubsequence.compute(A)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/MaximumCoinsGainTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class MaximumCoinsGainTest { 9 | 10 | private int expected; 11 | private List coins; 12 | 13 | @Test 14 | public void computeMaximum1() throws Exception { 15 | expected = 5; 16 | coins = Arrays.asList(1,5); 17 | 18 | test(expected, coins); 19 | } 20 | 21 | @Test 22 | public void computeMaximum2() throws Exception { 23 | expected = 15; 24 | coins = Arrays.asList(5,1,10,5); 25 | 26 | test(expected, coins); 27 | } 28 | 29 | @Test 30 | public void computeMaximum3() throws Exception { 31 | expected = 31; 32 | coins = Arrays.asList(10,25,5,1,10,5); 33 | 34 | test(expected, coins); 35 | } 36 | 37 | private static void test(int expected, List coins) { 38 | assertEquals(expected, MaximumCoinsGain.computeMaximum(coins)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /dynamicprogramming/src/test/java/PrettyPrintingProblemTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class PrettyPrintingProblemTest { 9 | 10 | private int expected; 11 | private List words; 12 | private int lineLength; 13 | 14 | @Test 15 | public void minimumMessiness1() throws Exception { 16 | expected = 82; 17 | words = Arrays.asList(("I have inserted a large number " + 18 | "of new examples from the papers for the Mathematical " + 19 | "Tripos during the last twenty years, which should be " + 20 | "useful to Cambridge students.").split(" ")); 21 | lineLength = 36; 22 | 23 | test(expected, words, lineLength); 24 | } 25 | 26 | private void test(int expected, List words, int lineLength) { 27 | assertEquals(expected, PrettyPrintingProblem.minimumMessiness(words, lineLength)); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /eopi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gardncl/elements-of-programming-interviews/3684e4ff4b50991b07935ec70d935434d81b12a4/eopi.jpg -------------------------------------------------------------------------------- /graphs/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 19: Graphs 2 | 3 | - [ ] 19.1 SearchMaze 4 | - [ ] 19.2 PaintBooleanMatrix 5 | - [ ] 19.3 ComputeEnclosedRegions 6 | - [ ] 19.4 DeadlockDetection 7 | - [ ] 19.5 CloneAGraph 8 | - [ ] 19.6 MakingWiredConnections 9 | - [ ] 19.7 TransformOneStringToAnother 10 | - [ ] 19.8 TeamPhotoDay 11 | - [ ] 19.9 ComputeShortestPath -------------------------------------------------------------------------------- /graphs/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | graphs 13 | Chapter 19: Graphs 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /graphs/src/main/java/CloneAGraph.java: -------------------------------------------------------------------------------- 1 | public class CloneAGraph { 2 | 3 | /* 4 | 19.5 5 | */ 6 | 7 | public static GraphVertex cloneGraph(GraphVertex g) { 8 | 9 | return new GraphVertex(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /graphs/src/main/java/ComputeEnclosedRegions.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ComputeEnclosedRegions { 4 | 5 | /* 6 | 19.3 7 | */ 8 | 9 | public static void fillSurroundingRegions(List> board) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /graphs/src/main/java/ComputeShortestPath.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeShortestPath { 5 | 6 | /* 7 | 19.9 8 | */ 9 | 10 | public static List compute(int cost, List graph, Vertex s, Vertex t) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /graphs/src/main/java/DeadlockDetection.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class DeadlockDetection { 4 | 5 | /* 6 | 19.4 7 | */ 8 | 9 | public static boolean isDeadlocked(List G) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /graphs/src/main/java/MakingWiredConnections.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MakingWiredConnections { 4 | 5 | /* 6 | 19.6 7 | */ 8 | 9 | public static boolean isAnyPlacementFeasible(List G) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /graphs/src/main/java/PaintBooleanMatrix.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class PaintBooleanMatrix { 4 | 5 | /* 6 | 19.2 7 | */ 8 | 9 | public static void flipColor(List> A, int x, int y) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /graphs/src/main/java/SearchMaze.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class SearchMaze { 5 | 6 | /* 7 | 19.1 8 | */ 9 | 10 | public static List searchMaze(List> maze, Tuple s, Tuple e) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /graphs/src/main/java/TeamPhotoDay.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class TeamPhotoDay { 4 | 5 | /* 6 | 19.8 7 | */ 8 | 9 | public static int findLargestNumberTeams(List G) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /graphs/src/main/java/TransformOneStringToAnother.java: -------------------------------------------------------------------------------- 1 | import java.util.Set; 2 | 3 | public class TransformOneStringToAnother { 4 | 5 | /* 6 | 19.7 7 | */ 8 | 9 | public static int transformString(Set D,String s, String d) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /graphs/src/test/java/CloneAGraphTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class CloneAGraphTest { 9 | 10 | private GraphVertex expected; 11 | private GraphVertex G; 12 | 13 | @Test 14 | public void cloneGraph1() throws Exception { 15 | GraphVertex a = new GraphVertex(0); 16 | GraphVertex b = new GraphVertex(1); 17 | GraphVertex c = new GraphVertex(2); 18 | GraphVertex d = new GraphVertex(3); 19 | a.edges.add(b); 20 | a.edges.add(c); 21 | b.edges.add(c); 22 | b.edges.add(d); 23 | 24 | expected = a; 25 | G = a; 26 | 27 | test(expected, G); 28 | } 29 | 30 | private void test(GraphVertex expected, GraphVertex G) { 31 | assertEquals(expected, CloneAGraph.cloneGraph(G)); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /greedyalgorithms/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 18: Greedy Algorithms and Invariants 2 | 3 | - [ ] 18.1 ComputeOptimumAssignment 4 | - [ ] 18.2 ScheduleMinimizedWaitingTime 5 | - [ ] 18.3 IntervalCoveringProblem 6 | - [ ] 18.4 ThreeSumProblem 7 | - [ ] 18.5 FindMajorityElement 8 | - [ ] 18.6 GasUpProblem 9 | - [ ] 18.7 ComputeMaximumWaterTrapped 10 | - [ ] 18.8 ComputeLargestRectangle 11 | -------------------------------------------------------------------------------- /greedyalgorithms/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | greedyalgorithms 13 | Chapter 18: Greedy Algorithms and Invariants 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/ComputeLargestRectangle.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ComputeLargestRectangle { 4 | 5 | /* 6 | 18.8 7 | */ 8 | 9 | public static int calculateLargestRectangle(List heights) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/ComputeMaximumWaterTrapped.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ComputeMaximumWaterTrapped { 4 | 5 | /* 6 | 18.7 7 | */ 8 | 9 | public static int getMaxTrappedWater(List heights) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/ComputeOptimumAssignment.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeOptimumAssignment { 5 | 6 | /* 7 | 18.1 8 | */ 9 | 10 | public static List optimumTaskAssignment(List taskDurations) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/FindMajorityElement.java: -------------------------------------------------------------------------------- 1 | import java.util.Iterator; 2 | 3 | public class FindMajorityElement { 4 | 5 | /* 6 | 18.5 7 | */ 8 | 9 | public static String majoritySearch(Iterator sequence) { 10 | 11 | return ""; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/GasUpProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class GasUpProblem { 4 | 5 | /* 6 | 18.6 7 | */ 8 | 9 | public static int findAmpleCity(List gallons, List distances) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/IntervalCoveringProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class IntervalCoveringProblem { 5 | 6 | /* 7 | 18.3 8 | */ 9 | 10 | public static List findMinimumVisits(List intervals) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/ScheduleMinimizedWaitingTime.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ScheduleMinimizedWaitingTime { 4 | 5 | /* 6 | 18.2 7 | */ 8 | 9 | public static int minimumTotalWaitingTime(List serviceTime) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/main/java/ThreeSumProblem.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ThreeSumProblem { 4 | 5 | /* 6 | 18.4 7 | */ 8 | 9 | public static boolean hasThreeSum(List A, int t) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/ComputeLargestRectangleTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeLargestRectangleTest { 9 | 10 | private int expected; 11 | private List heights; 12 | 13 | @Test 14 | public void calculateLargestRectangle1() throws Exception { 15 | expected = 20; 16 | heights = Arrays.asList( 17 | 0,1,4,2,5,6,3,2,6,6,5,2,1,3 18 | ); 19 | 20 | test(expected, heights); 21 | } 22 | 23 | private void test(int expected, List heights) { 24 | assertEquals(expected, ComputeLargestRectangle.calculateLargestRectangle(heights)); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/ComputeMaximumWaterTrappedTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeMaximumWaterTrappedTest { 9 | 10 | private Tuple expected; 11 | private List heights; 12 | 13 | @Test 14 | public void getMaxTrappedWater1() throws Exception { 15 | expected = new Tuple(4,16); 16 | heights = Arrays.asList( 17 | 1,2,1,3,4,4,5,6,2,1,3,1,3,2,1,2,4,1 18 | ); 19 | 20 | test(expected, heights); 21 | } 22 | 23 | @Test 24 | public void getMaxTrappedWater2() throws Exception { 25 | expected = new Tuple(7,15); 26 | heights = Arrays.asList( 27 | 1,2,1,3,4,4,5,6,2,1,3,1,3,2,1,6,4,1 28 | ); 29 | 30 | test(expected, heights); 31 | } 32 | 33 | private void test(Tuple expected, List heights) { 34 | assertEquals(expected, ComputeMaximumWaterTrapped.getMaxTrappedWater(heights)); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/ComputeOptimumAssignmentTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeOptimumAssignmentTest { 9 | 10 | private List expected; 11 | private List taskDurations; 12 | 13 | @Test 14 | public void optimumTaskAssignment1() throws Exception { 15 | expected = Arrays.asList( 16 | new Tuple(2,5), 17 | new Tuple(1,6), 18 | new Tuple(4,4) 19 | ); 20 | taskDurations = Arrays.asList(1,2,4,4,5,6); 21 | 22 | test(expected, taskDurations); 23 | } 24 | 25 | private void test(List expected, List taskDurations) { 26 | assertEquals(expected, ComputeOptimumAssignment.optimumTaskAssignment(taskDurations)); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/FindMajorityElementTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class FindMajorityElementTest { 9 | 10 | private Character expected; 11 | private String sequence; 12 | 13 | @Test 14 | public void majoritySearch1() throws Exception { 15 | expected = 'a'; 16 | sequence = "aca"; 17 | 18 | test(expected, sequence); 19 | } 20 | 21 | @Test 22 | public void majoritySearch2() throws Exception { 23 | expected = 'a'; 24 | sequence = "bacaabaaca"; 25 | 26 | test(expected, sequence); 27 | } 28 | 29 | private void test(Character expected, String sequence) { 30 | assertEquals(expected, FindMajorityElement.majoritySearch(Arrays.asList(sequence).iterator())); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/GasUpProblemTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class GasUpProblemTest { 9 | 10 | private int expected; 11 | private List gallons; 12 | private List distances; 13 | 14 | @Test 15 | public void findAmpleCity1() throws Exception { 16 | expected = 3; 17 | gallons = Arrays.asList( 18 | 50,20,5,30,25,10,10 19 | ); 20 | distances = Arrays.asList( 21 | 900,600,200,400,600,200,100 22 | ); 23 | 24 | test(expected,gallons,distances); 25 | } 26 | 27 | private void test(int expected, List gallons, List distances) { 28 | assertEquals(expected, GasUpProblem.findAmpleCity(gallons, distances)); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/ScheduleMinimizedWaitingTimeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ScheduleMinimizedWaitingTimeTest { 9 | 10 | private int expected; 11 | private List serviceTime; 12 | 13 | @Test 14 | public void minimumTotalWaitingTime1() throws Exception { 15 | expected = 10; 16 | serviceTime = Arrays.asList(2,5,1,3); 17 | 18 | test(expected, serviceTime); 19 | } 20 | 21 | private void test(int expected, List serviceTime) { 22 | assertEquals(expected, ScheduleMinimizedWaitingTime.minimumTotalWaitingTime(serviceTime)); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /greedyalgorithms/src/test/java/ThreeSumProblemTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ThreeSumProblemTest { 9 | 10 | private boolean expected; 11 | private List A; 12 | private int t; 13 | 14 | @Test 15 | public void hasThreeSum1() throws Exception { 16 | expected = true; 17 | A = Arrays.asList(11,2,5,7,3); 18 | t = 21; 19 | 20 | test(expected, A, t); 21 | } 22 | 23 | @Test 24 | public void hasThreeSum2() throws Exception { 25 | expected = true; 26 | A = Arrays.asList(2,7); 27 | t = 20; 28 | 29 | test(expected, A, t); 30 | } 31 | 32 | @Test 33 | public void hasThreeSum3() throws Exception { 34 | expected = false; 35 | A = Arrays.asList(2,7); 36 | t = 19; 37 | 38 | test(expected, A, t); 39 | } 40 | 41 | private void test(boolean expected, List A, int t) { 42 | assertEquals(expected, ThreeSumProblem.hasThreeSum(A, t)); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /hashtables/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 13: Hash Tables 2 | 3 | - [ ] 13.1 PalindromicPermutations 4 | - [ ] 13.2 IsLetterConstructable 5 | - [ ] 13.3 LRUCache 6 | - [ ] 13.4 ComputeLCA 7 | - [ ] 13.5 ComputeKMostFrequent 8 | - [ ] 13.6 NearestRepeated 9 | - [ ] 13.7 SmallestSubarray 10 | - [ ] 13.8 SmallestSequentialSubarray 11 | - [ ] 13.9 LongestSubarray 12 | - [ ] 13.10 LongestContainedInterval 13 | - [ ] 13.11 ComputeAverageTopThree 14 | - [ ] 13.12 ComputeStringDecompositions 15 | - [ ] 13.13 CollatzConjecture 16 | - [ ] 13.14 HashFunctionChess 17 | -------------------------------------------------------------------------------- /hashtables/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | hashtables 13 | Chapter 13: Hash Tables 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /hashtables/src/main/java/CollatzConjecture.java: -------------------------------------------------------------------------------- 1 | public class CollatzConjecture { 2 | 3 | /* 4 | 13.13 5 | */ 6 | 7 | public static boolean testCollatzConjecture(int n) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtables/src/main/java/ComputeAverageTopThree.java: -------------------------------------------------------------------------------- 1 | import java.util.Iterator; 2 | 3 | public class ComputeAverageTopThree { 4 | 5 | /* 6 | 13.11 7 | */ 8 | 9 | public static class NameScore { 10 | public String name; 11 | public Integer score; 12 | 13 | public NameScore(String name, Integer score) { 14 | this.name = name; 15 | this.score = score; 16 | } 17 | } 18 | 19 | public static String findStudent(Iterator iterator) { 20 | 21 | return ""; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /hashtables/src/main/java/ComputeKMostFrequent.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeKMostFrequent { 5 | 6 | /* 7 | 13.5 8 | */ 9 | 10 | public static List mostFrequent(List list, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hashtables/src/main/java/ComputeLCA.java: -------------------------------------------------------------------------------- 1 | public class ComputeLCA { 2 | 3 | /* 4 | 13.4 5 | */ 6 | 7 | public static BinaryTreeParent LCA(BinaryTreeParent node0, BinaryTreeParent node1) { 8 | 9 | return new BinaryTreeParent<>(0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtables/src/main/java/ComputeStringDecompositions.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeStringDecompositions { 5 | 6 | /* 7 | 13.12 8 | */ 9 | 10 | public static List findAllSubstring(String s, List words) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hashtables/src/main/java/HashFunctionChess.java: -------------------------------------------------------------------------------- 1 | public class HashFunctionChess { 2 | 3 | /* 4 | 13.14 5 | */ 6 | 7 | 8 | } 9 | -------------------------------------------------------------------------------- /hashtables/src/main/java/IsLetterConstructable.java: -------------------------------------------------------------------------------- 1 | public class IsLetterConstructable { 2 | 3 | /* 4 | 13.2 5 | */ 6 | 7 | public static boolean isConstructable(String letterText, String magazineText) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtables/src/main/java/LRUCache.java: -------------------------------------------------------------------------------- 1 | public class LRUCache { 2 | 3 | /* 4 | 13.3 5 | */ 6 | 7 | private int capacity; 8 | 9 | public LRUCache(int capacity) { 10 | this.capacity = capacity; 11 | } 12 | 13 | public Integer lookup(Integer key) { 14 | 15 | return 0; 16 | } 17 | 18 | public Integer insert(Integer key, Integer value) { 19 | 20 | return 0; 21 | } 22 | 23 | public Integer remove(Integer key) { 24 | 25 | return 0; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /hashtables/src/main/java/LongestContainedInterval.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class LongestContainedInterval { 4 | 5 | /* 6 | 13.10 7 | */ 8 | 9 | public static int longestInterval(List list) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /hashtables/src/main/java/LongestSubarray.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class LongestSubarray { 4 | 5 | /* 6 | 13.9 7 | */ 8 | 9 | public static int longestSubarray(List list) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /hashtables/src/main/java/NearestRepeated.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class NearestRepeated { 4 | 5 | /* 6 | 13.6 7 | */ 8 | 9 | public static int findNearest(List list) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /hashtables/src/main/java/PalindromicPermutations.java: -------------------------------------------------------------------------------- 1 | public class PalindromicPermutations { 2 | 3 | /* 4 | 13.1 5 | */ 6 | 7 | public static boolean canFormPalindrome(String s) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /hashtables/src/main/java/SmallestSequentialSubarray.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.Set; 3 | 4 | public class SmallestSequentialSubarray { 5 | 6 | /* 7 | 13.8 8 | */ 9 | 10 | public static Tuple findSubarray(List paragraph, Set keywords) { 11 | 12 | return new Tuple(0,0); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hashtables/src/main/java/SmallestSubarray.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.Set; 3 | 4 | public class SmallestSubarray { 5 | 6 | /* 7 | 13.7 8 | */ 9 | 10 | public static Tuple findSubarray(List paragraph, Set keywords) { 11 | 12 | return new Tuple(0,0); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hashtables/src/test/java/CollatzConjectureTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class CollatzConjectureTest { 6 | 7 | private boolean expected; 8 | private int n; 9 | 10 | @Test 11 | public void testCollatzConjecture() throws Exception { 12 | expected = true; 13 | n = 10000; 14 | 15 | test(expected, n); 16 | } 17 | 18 | private void test(boolean expected, int n) { 19 | assertEquals(expected, CollatzConjecture.testCollatzConjecture(n)); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /hashtables/src/test/java/ComputeStringDecompositionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeStringDecompositionsTest { 9 | 10 | private List expected; 11 | private String s; 12 | private List words; 13 | 14 | @Test 15 | public void findAllSubstring1() throws Exception { 16 | expected = Arrays.asList( 17 | "aplanacan", 18 | "canaplana" 19 | ); 20 | s = "amanaplanacanalcanaplanaalpmna"; 21 | words = Arrays.asList( 22 | "can", 23 | "apl", 24 | "ana" 25 | ); 26 | 27 | test(expected, s, words); 28 | } 29 | 30 | private void test(List expected, String s, List words) { 31 | AssertUtils.assertSameContentsString(expected, ComputeStringDecompositions.findAllSubstring(s, words)); 32 | } 33 | } -------------------------------------------------------------------------------- /hashtables/src/test/java/LRUCacheTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import static org.junit.Assert.*; 9 | 10 | public class LRUCacheTest { 11 | 12 | @Test 13 | public void lruCache() { 14 | final int capacity = 8; 15 | final Map map = new HashMap<>(); 16 | final List list = Arrays.asList(1,2,3,4,5,6,7,8,9,10); 17 | final LRUCache cache = new LRUCache(capacity); 18 | list.forEach(i -> { 19 | cache.insert(i, i); 20 | }); 21 | 22 | //FIRST TWO HAVE BEEN REPLACED 23 | assertNull(cache.lookup(1)); 24 | assertNull(cache.lookup(2)); 25 | 26 | //LRU IS NOW MOST RECENTLY ACCESSED 27 | assertEquals(list.get(3), cache.lookup(3)); 28 | 29 | //INSERT ONE AND BUMP OFF LAST ENTRY 30 | cache.insert(1, 1); 31 | 32 | //ASSERT PREVIOUSLY LRU IS GONE 33 | assertNull(cache.lookup(4)); 34 | 35 | //ASSERT THAT REMOVING AN ELEMENT WORKS 36 | cache.remove(1); 37 | assertNull(cache.lookup(1)); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /hashtables/src/test/java/LongestContainedIntervalTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class LongestContainedIntervalTest { 9 | 10 | private int expected; 11 | private List list; 12 | 13 | @Test 14 | public void longestSubarray1() throws Exception { 15 | expected = 6; 16 | list = Arrays.asList(3,-2,7,9,8,1,2,0,-1,5,8); 17 | 18 | test(expected, list); 19 | } 20 | 21 | @Test 22 | public void longestSubarray2() throws Exception { 23 | expected = 5; 24 | list = Arrays.asList(92,4,13,-47,1,78,79,80,3,101,2000,2,56,45,23,5); 25 | 26 | test(expected, list); 27 | } 28 | 29 | private void test(int expected, List list) { 30 | assertEquals(expected, LongestContainedInterval.longestInterval(list)); 31 | } 32 | } -------------------------------------------------------------------------------- /hashtables/src/test/java/LongestSubarrayTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class LongestSubarrayTest { 9 | 10 | private int expected; 11 | private List list; 12 | 13 | @Test 14 | public void longestSubarray1() throws Exception { 15 | expected = 5; 16 | list = Arrays.asList(1,2,3,4,5); 17 | 18 | test(expected, list); 19 | } 20 | 21 | @Test 22 | public void longestSubarray2() throws Exception { 23 | expected = 5; 24 | list = Arrays.asList(7,1,8,7,9,3,4,1,2,3,4,5,3,7,2,9); 25 | 26 | test(expected, list); 27 | } 28 | 29 | private void test(int expected, List list) { 30 | assertEquals(expected, LongestSubarray.longestSubarray(list)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /hashtables/src/test/java/PalindromicPermutationsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class PalindromicPermutationsTest { 6 | 7 | private boolean expected; 8 | private String s; 9 | 10 | @Test 11 | public void canFormPalindrome1() throws Exception { 12 | expected = true; 13 | s = "edified"; 14 | 15 | test(expected, s); 16 | } 17 | 18 | @Test 19 | public void canFormPalindrome2() throws Exception { 20 | expected = false; 21 | s = "abc"; 22 | 23 | test(expected, s); 24 | } 25 | 26 | @Test 27 | public void canFormPalindrome3() throws Exception { 28 | expected = true; 29 | s = "aabbcc"; 30 | 31 | test(expected, s); 32 | } 33 | 34 | @Test 35 | public void canFormPalindrome4() throws Exception { 36 | expected = false; 37 | s = "aabbbccc"; 38 | 39 | test(expected, s); 40 | } 41 | 42 | private void test(boolean expected, String s) { 43 | assertEquals(expected, PalindromicPermutations.canFormPalindrome(s)); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /heaps/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 11: Heaps 2 | 3 | - [ ] 11.1 MergeSortedFiles 4 | - [ ] 11.2 SortIncDec 5 | - [ ] 11.3 SortAlmostSorted 6 | - [ ] 11.4 ComputeKClosest 7 | - [ ] 11.5 ComputeMedianOnline 8 | - [ ] 11.6 ComputeKLargest 9 | - [ ] 11.7 HeapStack -------------------------------------------------------------------------------- /heaps/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | heaps 13 | Chapter 11: Heaps 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /heaps/src/main/java/ComputeKClosest.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.Iterator; 3 | import java.util.List; 4 | 5 | public class ComputeKClosest { 6 | 7 | /* 8 | 11.4 9 | */ 10 | 11 | private static final Star EARTH_COORDINATES = new Star(0,0,0); 12 | 13 | public static List getKClosest(int k, Iterator stars) { 14 | 15 | return Collections.emptyList(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /heaps/src/main/java/ComputeKLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeKLargest { 5 | 6 | /* 7 | 11.6 8 | */ 9 | 10 | public static List kLargestInBinaryHeap(final List list, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /heaps/src/main/java/ComputeMedianOnline.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.Iterator; 3 | import java.util.List; 4 | 5 | public class ComputeMedianOnline { 6 | 7 | /* 8 | 11.5 9 | */ 10 | 11 | public static List onlineMedian(Iterator sequence) { 12 | 13 | return Collections.emptyList(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /heaps/src/main/java/HeapStack.java: -------------------------------------------------------------------------------- 1 | public class HeapStack { 2 | 3 | /* 4 | 11.7 5 | */ 6 | 7 | public void push(Integer x) { 8 | 9 | } 10 | 11 | public Integer pop() { 12 | 13 | return 0; 14 | } 15 | 16 | public Integer peek() { 17 | 18 | return 0; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /heaps/src/main/java/MergeSortedFiles.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class MergeSortedFiles { 5 | 6 | /* 7 | 11.1 8 | */ 9 | 10 | public static List mergeSorted(List> sortedArrays) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /heaps/src/main/java/SortAlmostSorted.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.Iterator; 3 | import java.util.List; 4 | 5 | public class SortAlmostSorted { 6 | 7 | /* 8 | 11.3 9 | */ 10 | 11 | public static List sort(Iterator sequence, int k) { 12 | 13 | return Collections.emptyList(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /heaps/src/main/java/SortIncDec.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class SortIncDec { 5 | 6 | /* 7 | 11.2 8 | */ 9 | 10 | public static List sort(List list) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /heaps/src/test/java/ComputeMedianOnlineTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class ComputeMedianOnlineTest { 10 | 11 | private List expected; 12 | private Iterator input; 13 | 14 | @Test 15 | public void onlineMedian1() throws Exception { 16 | expected = Arrays.asList(1.,.5,1.,2.,2.,1.5,1.); 17 | input = Arrays.asList(1,0,3,5,2,0,1).iterator(); 18 | 19 | test(expected, input); 20 | } 21 | 22 | private void test(List expected, Iterator input) { 23 | assertEquals(expected, ComputeMedianOnline.onlineMedian(input)); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /heaps/src/test/java/HeapStackTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Assert; 2 | import org.junit.Test; 3 | 4 | import java.util.Collections; 5 | import java.util.List; 6 | import java.util.stream.IntStream; 7 | 8 | import static org.junit.Assert.assertEquals; 9 | 10 | public class HeapStackTest { 11 | 12 | private int length; 13 | 14 | @Test 15 | public void heapStack1(){ 16 | length = 10; 17 | 18 | test(length); 19 | } 20 | 21 | @Test 22 | public void heapStack2(){ 23 | length = 100; 24 | 25 | test(length); 26 | } 27 | 28 | private void test(int length) { 29 | List values = StreamUtil.sequence(length); 30 | HeapStack stack = createStack(values); 31 | StreamUtil.revRange(0, length) 32 | .forEach(n -> { 33 | assertEquals((Integer)n,stack.pop()); 34 | }); 35 | } 36 | 37 | private HeapStack createStack(List values) { 38 | final HeapStack stack = new HeapStack(); 39 | IntStream.range(0, values.size()) 40 | .forEach(i -> 41 | { 42 | stack.push(values.get(i)); 43 | }); 44 | return stack; 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /heaps/src/test/java/MergeSortedFilesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class MergeSortedFilesTest { 9 | 10 | private List expected; 11 | private List> input; 12 | 13 | @Test 14 | public void mergeSorted1() throws Exception { 15 | expected = Arrays.asList(1,2,3,4,5,6,7,8,9); 16 | input = Arrays.asList( 17 | Arrays.asList(1,4,7), 18 | Arrays.asList(2,5,8), 19 | Arrays.asList(3,6,9) 20 | ); 21 | 22 | test(expected, input); 23 | } 24 | 25 | @Test 26 | public void mergeSorted2() throws Exception { 27 | expected = Arrays.asList(0,0,3,5,6,6,7,28); 28 | input = Arrays.asList( 29 | Arrays.asList(3,5,7), 30 | Arrays.asList(0,6), 31 | Arrays.asList(0,6,28) 32 | ); 33 | 34 | test(expected, input); 35 | } 36 | 37 | private void test(List expected, List> input) { 38 | assertEquals(expected, MergeSortedFiles.mergeSorted(input)); 39 | } 40 | } -------------------------------------------------------------------------------- /heaps/src/test/java/SortAlmostSortedTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | public class SortAlmostSortedTest { 10 | 11 | private List expected; 12 | private Iterator sequence; 13 | private int k; 14 | 15 | @Test 16 | public void sort1() throws Exception { 17 | expected = Arrays.asList(1,2,3,4,5,6,7,8,9); 18 | sequence = Arrays.asList(1,4,7,2,5,8,3,6,9).iterator(); 19 | k = 5; 20 | 21 | test(expected, sequence, k); 22 | } 23 | 24 | @Test 25 | public void sort2() throws Exception { 26 | expected = Arrays.asList(-1,2,3,4,5,6,8); 27 | sequence = Arrays.asList(3,-1,2,6,4,5,8).iterator(); 28 | k = 2; 29 | 30 | test(expected, sequence, k); 31 | } 32 | 33 | private void test(List expected, Iterator sequence, int k) { 34 | assertEquals(expected, SortAlmostSorted.sort(sequence, k)); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /heaps/src/test/java/SortIncDecTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class SortIncDecTest { 9 | 10 | private List expected; 11 | private List input; 12 | 13 | @Test 14 | public void sort1() throws Exception { 15 | expected = Arrays.asList(1,2,3,4,5,6,7,8,9); 16 | input = Arrays.asList(2,3,1,5,6,4,8,9,7); 17 | 18 | test(expected, input); 19 | } 20 | 21 | @Test 22 | public void sort2() throws Exception { 23 | expected = Arrays.asList(57,131,190,221,294,339,418,442,452,493); 24 | input = Arrays.asList(57,131,493,294,221,339,418,452,442,190); 25 | 26 | test(expected, input); 27 | } 28 | 29 | private void test(List expected, List input) { 30 | assertEquals(expected, SortIncDec.sort(input)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /linkedlists/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 8: Linked Lists 2 | 3 | - [ ] 8.1 MergeSortedLists 4 | - [ ] 8.2 ReverseSingleSublist 5 | - [ ] 8.3 TestCyclicity 6 | - [ ] 8.4 TestForOverlappingLists 7 | - [ ] 8.5 FindOverlappingWithCycles 8 | - [ ] 8.6 DeleteNode 9 | - [ ] 8.7 DeleteKthLastNode 10 | - [ ] 8.8 RemoveDuplicatesFromSortedList 11 | - [ ] 8.9 CyclicRightShift 12 | - [ ] 8.10 EvenOddMerge 13 | - [ ] 8.11 Palindromic 14 | - [ ] 8.12 ListPivot 15 | - [ ] 8.13 AddIntegers -------------------------------------------------------------------------------- /linkedlists/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | linkedlists 13 | Chapter 8: Linked Lists 14 | 15 | 16 | 17 | gardncl 18 | datastructures 19 | 1.0 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/AddIntegers.java: -------------------------------------------------------------------------------- 1 | public class AddIntegers { 2 | 3 | /* 4 | 8.13 5 | */ 6 | 7 | public static ListNode addIntegers(ListNode a, ListNode b) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/CyclicRightShift.java: -------------------------------------------------------------------------------- 1 | public class CyclicRightShift { 2 | 3 | /* 4 | 8.9 5 | */ 6 | 7 | public static ListNode shift(int k, ListNode list) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/DeleteKthLastNode.java: -------------------------------------------------------------------------------- 1 | public class DeleteKthLastNode { 2 | 3 | /* 4 | 8.7 5 | */ 6 | 7 | public static void deleteNode(ListNode list, int k) { 8 | 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/DeleteNode.java: -------------------------------------------------------------------------------- 1 | public class DeleteNode { 2 | 3 | /* 4 | 8.6 5 | */ 6 | 7 | public static void deleteNode(ListNode node) { 8 | 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/EvenOddMerge.java: -------------------------------------------------------------------------------- 1 | public class EvenOddMerge { 2 | 3 | /* 4 | 8.10 5 | */ 6 | 7 | public static ListNode merge(ListNode list) { 8 | return new ListNode(null); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/FindOverlappingWithCycles.java: -------------------------------------------------------------------------------- 1 | public class FindOverlappingWithCycles { 2 | 3 | /* 4 | 8.5 5 | */ 6 | 7 | public static ListNode testOverlappingWithCycles(ListNode list1, ListNode list2) { 8 | return null; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/ListPivot.java: -------------------------------------------------------------------------------- 1 | public class ListPivot { 2 | 3 | /* 4 | 8.12 5 | */ 6 | 7 | public static ListNode pivot(ListNode list, int k) { 8 | return new ListNode<>(null); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/MergeSortedLists.java: -------------------------------------------------------------------------------- 1 | public class MergeSortedLists { 2 | 3 | /* 4 | 8.1 5 | */ 6 | 7 | public static ListNode mergeLists(ListNode list1, ListNode list2) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/Palindromic.java: -------------------------------------------------------------------------------- 1 | public class Palindromic { 2 | 3 | /* 4 | 8.11 5 | */ 6 | 7 | public static boolean isPalindromic(ListNode list) { 8 | 9 | return false; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/RemoveDuplicatesFromSortedList.java: -------------------------------------------------------------------------------- 1 | public class RemoveDuplicatesFromSortedList { 2 | 3 | /* 4 | 8.8 5 | */ 6 | 7 | public static void removeDuplicates(ListNode list) { 8 | 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/ReverseSingleSublist.java: -------------------------------------------------------------------------------- 1 | public class ReverseSingleSublist { 2 | 3 | /* 4 | 8.2 5 | */ 6 | 7 | public static ListNode reverseSublist(ListNode input, int s, int f) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/TestCyclicity.java: -------------------------------------------------------------------------------- 1 | public class TestCyclicity { 2 | 3 | /* 4 | 8.3 5 | */ 6 | 7 | public static ListNode isCyclic(ListNode list) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/main/java/TestForOverlappingLists.java: -------------------------------------------------------------------------------- 1 | public class TestForOverlappingLists { 2 | 3 | /* 4 | 8.4 5 | */ 6 | 7 | public static ListNode doListsOverlap(ListNode list1, ListNode list2) { 8 | 9 | return new ListNode<>(null); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /linkedlists/src/test/java/AddIntegersTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | public class AddIntegersTest { 4 | 5 | private ListNode sum; 6 | private ListNode a; 7 | private ListNode b; 8 | 9 | @Test 10 | public void addIntegers1() { 11 | sum = LinkedListUtil.createLinkedList(2); 12 | a = LinkedListUtil.createLinkedList(1); 13 | b = LinkedListUtil.createLinkedList(1); 14 | 15 | test(sum, a, b); 16 | } 17 | 18 | @Test 19 | public void addIntegers2() { 20 | sum = LinkedListUtil.createLinkedList(4, 2, 0, 1); 21 | a = LinkedListUtil.createLinkedList(2, 1, 5); 22 | b = LinkedListUtil.createLinkedList(2, 1, 5); 23 | 24 | test(sum, a, b); 25 | } 26 | 27 | @Test 28 | public void addIntegers3() { 29 | sum = LinkedListUtil.createLinkedList(1, 0, 0, 1); 30 | a = LinkedListUtil.createLinkedList(1, 0, 0, 0); 31 | b = LinkedListUtil.createLinkedList(0, 0, 0, 1); 32 | 33 | test(sum, a, b); 34 | } 35 | 36 | private static void test(ListNode sum, ListNode a, ListNode b) { 37 | LinkedListUtil.assertSameList(sum, AddIntegers.addIntegers(a, b)); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /linkedlists/src/test/java/DeleteNodeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | public class DeleteNodeTest { 4 | 5 | private ListNode expected; 6 | private ListNode listToDeleteFrom; 7 | private ListNode node; 8 | 9 | @Test 10 | public void deleteNode1() { 11 | node = new ListNode<>(10); 12 | expected = LinkedListUtil.createLinkedList(1, 2, 3, 4, 5); 13 | listToDeleteFrom = LinkedListUtil.createLinkedList(1, 2, 3, 4, 5); 14 | listToDeleteFrom.get(3).insertAfter(node); 15 | 16 | test(expected, listToDeleteFrom, node); 17 | } 18 | 19 | @Test 20 | public void deleteNode2() { 21 | node = new ListNode<>(10); 22 | node.insertAfter(new ListNode<>(1)); 23 | expected = LinkedListUtil.createLinkedList(1); 24 | listToDeleteFrom = node; 25 | 26 | test(expected, listToDeleteFrom, node); 27 | } 28 | 29 | 30 | private void test(ListNode expected, ListNode listToDeleteFrom, ListNode node) { 31 | DeleteNode.deleteNode(node); 32 | LinkedListUtil.assertSameList(expected, listToDeleteFrom); 33 | } 34 | 35 | 36 | } -------------------------------------------------------------------------------- /linkedlists/src/test/java/MergeSortedListsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | public class MergeSortedListsTest { 4 | 5 | private ListNode expected; 6 | private ListNode list1; 7 | private ListNode list2; 8 | 9 | @Test 10 | public void mergeLists1() { 11 | expected = LinkedListUtil.createLinkedList(1, 2); 12 | list1 = new ListNode<>(1); 13 | list2 = new ListNode<>(2); 14 | 15 | test(expected, list1, list2); 16 | } 17 | 18 | @Test 19 | public void mergeLists2() { 20 | expected = new ListNode<>(1); 21 | list1 = new ListNode<>(1); 22 | 23 | test(expected, list1, list2); 24 | } 25 | 26 | @Test 27 | public void mergeLists3() { 28 | expected = LinkedListUtil.createLinkedList(1, 2, 3, 4, 5, 6); 29 | list1 = LinkedListUtil.createLinkedList(1, 3, 5); 30 | list2 = LinkedListUtil.createLinkedList(2, 4, 6); 31 | 32 | test(expected, list1, list2); 33 | } 34 | 35 | private void test(ListNode expected, ListNode list1, ListNode list2) { 36 | ListNode result = MergeSortedLists.mergeLists(list1, list2); 37 | LinkedListUtil.assertSameList(expected, result); 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /linkedlists/src/test/java/ReverseSingleSublistTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | public class ReverseSingleSublistTest { 4 | 5 | ListNode expected; 6 | ListNode list; 7 | int s; 8 | int f; 9 | 10 | @Test 11 | public void reverseSublist1() { 12 | expected = LinkedListUtil.createLinkedList(1, 2, 3); 13 | list = LinkedListUtil.createLinkedList(3, 2, 1); 14 | s = 1; 15 | f = 3; 16 | 17 | test(expected, list, s, f); 18 | } 19 | 20 | @Test 21 | public void reverseSublist2() { 22 | expected = LinkedListUtil.createLinkedList(1, 2, 6, 5, 4, 3, 7, 8, 9); 23 | list = LinkedListUtil.createLinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9); 24 | s = 3; 25 | f = 6; 26 | 27 | test(expected, list, s, f); 28 | } 29 | 30 | private void test(ListNode expected, ListNode list, int s, int f) { 31 | LinkedListUtil.assertSameList(expected, ReverseSingleSublist.reverseSublist(list, s, f)); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /parallelcomputing/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 20: Parallel Computing 2 | 3 | - [ ] 20.1 ImplementCachingDictionary 4 | - [ ] 20.2 AnalyzeUnsyncronizedThreads 5 | - [ ] 20.3 ImplementThreadSyncronization 6 | - [ ] 20.4 ImplementThreadPool 7 | - [ ] 20.5 Deadlock 8 | - [ ] 20.6 ReaderWriterProblem 9 | - [ ] 20.7 ReaderWriterProblemPreference 10 | - [ ] 20.8 ImplementTimer 11 | - [ ] 20.9 TestCollatzConjecture -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/AnalyzeUnsyncronizedThreads.java: -------------------------------------------------------------------------------- 1 | public class AnalyzeUnsyncronizedThreads { 2 | 3 | /* 4 | 20.2 5 | */ 6 | } 7 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/Deadlock.java: -------------------------------------------------------------------------------- 1 | public class Deadlock { 2 | 3 | /* 4 | 20.5 5 | */ 6 | 7 | public static class Account { 8 | private int balance; 9 | private int id; 10 | private static int globalId; 11 | 12 | public Account(int balance) { 13 | this.balance = balance; 14 | this.id = ++globalId; 15 | } 16 | 17 | public int getBalance() { 18 | return balance; 19 | } 20 | 21 | private boolean move(Account to, int amount) { 22 | synchronized (this) { 23 | synchronized (to) { 24 | if (amount > balance) { 25 | return false; 26 | } 27 | } 28 | to.balance += amount; 29 | this.balance -= amount; 30 | return true; 31 | } 32 | } 33 | 34 | public static void transfer(final Account from, final Account to, final int amount) { 35 | Thread transfer = new Thread(new Runnable() { 36 | @Override 37 | public void run() {from.move(to,amount);} 38 | }); 39 | transfer.start(); 40 | } 41 | } 42 | 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/ImplementThreadPool.java: -------------------------------------------------------------------------------- 1 | import java.io.IOException; 2 | import java.net.ServerSocket; 3 | import java.net.Socket; 4 | 5 | public class ImplementThreadPool { 6 | 7 | /* 8 | 20.4 9 | */ 10 | 11 | public static class WebServer { 12 | public static final int PORT = 8080; 13 | public static void main(String[] args) throws IOException { 14 | ServerSocket serverSocket = new ServerSocket(PORT); 15 | for (;;) { 16 | Socket socket = serverSocket.accept(); 17 | processReq(socket); 18 | } 19 | } 20 | 21 | private static void processReq(Socket socket) throws IOException{ 22 | socket.close(); 23 | } 24 | } 25 | 26 | 27 | } 28 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/ImplementThreadSyncronization.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ImplementThreadSyncronization { 4 | 5 | /* 6 | 20.3 7 | */ 8 | 9 | public static class OddThread extends Thread { 10 | 11 | private List list; 12 | 13 | public OddThread(List list) { 14 | this.list = list; 15 | } 16 | 17 | @Override 18 | public void run() { 19 | 20 | } 21 | } 22 | 23 | public static class EvenThread extends Thread { 24 | 25 | private List list; 26 | 27 | public EvenThread(List list) { 28 | this.list = list; 29 | } 30 | 31 | @Override 32 | public void run() { 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/ImplementTimer.java: -------------------------------------------------------------------------------- 1 | public class ImplementTimer { 2 | } 3 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/ReaderWriterProblem.java: -------------------------------------------------------------------------------- 1 | public class ReaderWriterProblem { 2 | 3 | /* 4 | 20.6 5 | */ 6 | 7 | public static class Reader extends Thread { 8 | 9 | @Override 10 | public void run() { 11 | 12 | } 13 | 14 | } 15 | 16 | public static class Writer extends Thread { 17 | 18 | @Override 19 | public void run() { 20 | 21 | } 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/ReaderWriterProblemPreference.java: -------------------------------------------------------------------------------- 1 | public class ReaderWriterProblemPreference { 2 | } 3 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/SpellCheckService.java: -------------------------------------------------------------------------------- 1 | public interface SpellCheckService { 2 | 3 | public void service(ServiceRequest request, ServiceResponse response); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /parallelcomputing/src/main/java/TestCollatzConjecture.java: -------------------------------------------------------------------------------- 1 | public class TestCollatzConjecture { 2 | } 3 | -------------------------------------------------------------------------------- /parallelcomputing/src/test/java/DeadlockTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.stream.IntStream; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class DeadlockTest { 8 | 9 | @Test 10 | public void test() { 11 | Deadlock.Account ben = new Deadlock.Account(1000); 12 | Deadlock.Account greg = new Deadlock.Account(800); 13 | Deadlock.Account liam = new Deadlock.Account(400); 14 | Deadlock.Account billy = new Deadlock.Account(200); 15 | 16 | IntStream.range(0,200).forEach(i -> { 17 | Deadlock.Account.transfer(ben, liam, 1); 18 | Deadlock.Account.transfer(ben, billy, 2); 19 | Deadlock.Account.transfer(greg, liam, 3); 20 | Deadlock.Account.transfer(liam, billy, 1); 21 | }); 22 | 23 | assertEquals(400,ben.getBalance()); 24 | assertEquals(200,greg.getBalance()); 25 | assertEquals(1000,liam.getBalance()); 26 | assertEquals(800,billy.getBalance()); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /parallelcomputing/src/test/java/EvenThreadTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class EvenThreadTest { 9 | 10 | @Test 11 | public void test() throws Exception { 12 | List result = new ArrayList<>(); 13 | Thread t1 = new ImplementThreadSyncronization.OddThread(result); 14 | Thread t2 = new ImplementThreadSyncronization.EvenThread(result); 15 | t1.start(); 16 | t2.start(); 17 | t1.join(); 18 | t2.join(); 19 | assertEquals(StreamUtil.sequence(100), result); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /parallelcomputing/src/test/java/ReaderTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class ReaderTest { 6 | 7 | @Test 8 | public void test() { 9 | 10 | } 11 | 12 | } -------------------------------------------------------------------------------- /primitives/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5: Primitives 2 | 3 | - [ ] 5.1 ComputeParity 4 | - [ ] 5.2 SwapBits 5 | - [ ] 5.3 ReverseBits 6 | - [ ] 5.4 FindClosestInteger 7 | - [ ] 5.5 MultiplicationNoArithmeticOperators 8 | - [ ] 5.6 ComputerDivision 9 | - [ ] 5.7 ComputePower 10 | - [ ] 5.8 ReverseDigits 11 | - [ ] 5.9 CheckIfIntegerIsPalindrome 12 | - [ ] 5.10 GenerateUniformRandomNumbers 13 | - [ ] 5.11 RectangleIntersection -------------------------------------------------------------------------------- /primitives/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | primitives 13 | Chapter 5: Primitives 14 | 15 | 16 | 17 | gardncl 18 | datastructures 19 | 1.0 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /primitives/src/main/java/CheckIfIntegerIsPalindrome.java: -------------------------------------------------------------------------------- 1 | public class CheckIfIntegerIsPalindrome { 2 | 3 | /* 4 | 5.9 5 | */ 6 | 7 | public static boolean isPalindrome(int x) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/ComputeDivision.java: -------------------------------------------------------------------------------- 1 | public class ComputeDivision { 2 | 3 | /* 4 | 5.6 5 | */ 6 | 7 | public static long divide(long x, long y) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/ComputeParity.java: -------------------------------------------------------------------------------- 1 | public class ComputeParity { 2 | 3 | /* 4 | 5.1 5 | */ 6 | 7 | public static short parity(long n) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/ComputePower.java: -------------------------------------------------------------------------------- 1 | public class ComputePower { 2 | 3 | /* 4 | 5.7 5 | */ 6 | 7 | public static double power(double x, int y) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/FindClosestInteger.java: -------------------------------------------------------------------------------- 1 | public class FindClosestInteger { 2 | 3 | /* 4 | 5.4 5 | */ 6 | 7 | public static long closestIntSameBitCount(long x) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/GenerateUniformRandomNumbers.java: -------------------------------------------------------------------------------- 1 | public class GenerateUniformRandomNumbers { 2 | 3 | /* 4 | 5.10 5 | */ 6 | 7 | public static int uniformRandom(int lowerBound, int upperBound) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/MultiplicationNoArithmeticOperators.java: -------------------------------------------------------------------------------- 1 | public class MultiplicationNoArithmeticOperators { 2 | 3 | /* 4 | 5.5 5 | */ 6 | 7 | public static long multiply(long x, long y) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/RectangleIntersection.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class RectangleIntersection { 4 | 5 | /* 6 | 5.11 7 | */ 8 | 9 | public static Rectangle intersectRectangle(Rectangle r1, Rectangle r2) { 10 | 11 | return new Rectangle(1,1,1, 1); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /primitives/src/main/java/ReverseBits.java: -------------------------------------------------------------------------------- 1 | public class ReverseBits { 2 | 3 | /* 4 | 5.3 5 | */ 6 | 7 | public static int reverse(int x) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/ReverseDigits.java: -------------------------------------------------------------------------------- 1 | public class ReverseDigits { 2 | 3 | /* 4 | 5.8 5 | */ 6 | 7 | public static long reverse(int x) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/main/java/SwapBits.java: -------------------------------------------------------------------------------- 1 | public class SwapBits { 2 | 3 | /* 4 | 5.2 5 | */ 6 | 7 | public static long swapBits(long x, int i, int j) { 8 | 9 | return 0L; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /primitives/src/test/java/CheckIfIntegerIsPalindromeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class CheckIfIntegerIsPalindromeTest { 6 | 7 | boolean expected; 8 | int input; 9 | 10 | @Test 11 | public void isPalindrome1() { 12 | expected = false; 13 | input = 12; 14 | 15 | test(expected, input); 16 | } 17 | 18 | @Test 19 | public void isPalindrome2() { 20 | expected = true; 21 | input = 0; 22 | 23 | test(expected, input); 24 | } 25 | 26 | @Test 27 | public void isPalindrome3() { 28 | expected = false; 29 | input = -1; 30 | 31 | test(expected, input); 32 | } 33 | 34 | @Test 35 | public void isPalindrome5() { 36 | expected = false; 37 | input = 51342123; 38 | 39 | test(expected, input); 40 | } 41 | 42 | 43 | private void test(boolean expected, int input) { 44 | assertEquals(expected, CheckIfIntegerIsPalindrome.isPalindrome(input)); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /primitives/src/test/java/ComputeDivisionTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ComputeDivisionTest { 6 | 7 | private long expected; 8 | private long x; 9 | private long y; 10 | 11 | @Test 12 | public void divide1() { 13 | expected = 5; 14 | x = 10; 15 | y = 2; 16 | 17 | test(expected, x, y); 18 | } 19 | 20 | @Test 21 | public void divide2() { 22 | expected = 17; 23 | x = 527; 24 | y = 31; 25 | 26 | test(expected, x, y); 27 | } 28 | 29 | private void test(long expected, long x, long y) { 30 | assertEquals(expected, ComputeDivision.divide(x,y)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /primitives/src/test/java/ComputeParityTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ComputeParityTest { 6 | 7 | private short expected; 8 | private long n; 9 | 10 | 11 | @Test 12 | public void parity1() { 13 | expected = 0; 14 | n = 0; 15 | 16 | test(expected, n); 17 | } 18 | 19 | @Test 20 | public void parity2() { 21 | expected = 1; 22 | n = 1; 23 | 24 | test(expected, n); 25 | } 26 | 27 | @Test 28 | public void parity3() { 29 | expected = 1; 30 | n = 2; 31 | 32 | test(expected, n); 33 | } 34 | 35 | @Test 36 | public void parity4() { 37 | expected = 0; 38 | n = 152950; 39 | 40 | test(expected, n); 41 | } 42 | 43 | private void test(short expected, long n) { 44 | assertEquals(expected, ComputeParity.parity(n)); 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /primitives/src/test/java/ComputePowerTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ComputePowerTest { 6 | 7 | private final double EPSILON = .1; 8 | private double expected; 9 | private double x; 10 | private int y; 11 | 12 | @Test 13 | public void power1() { 14 | expected = 4; 15 | x = 2; 16 | y = 2; 17 | 18 | test(expected, x, y); 19 | } 20 | 21 | @Test 22 | public void power2() { 23 | expected = 783.11; 24 | x = 2.3; 25 | y = 8; 26 | 27 | test(expected, x, y); 28 | } 29 | 30 | @Test 31 | public void power4() { 32 | expected = 9; 33 | x = -3; 34 | y = 2; 35 | 36 | test(expected, x, y); 37 | } 38 | 39 | @Test 40 | public void power5() { 41 | expected = -27; 42 | x = -3; 43 | y = 3; 44 | 45 | test(expected, x, y); 46 | } 47 | 48 | private void test(double expected, double x, int y) { 49 | assertEquals(expected,ComputePower.power(x,y),EPSILON); 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /primitives/src/test/java/FindClosestIntegerTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class FindClosestIntegerTest { 6 | 7 | private long expected; 8 | private long x; 9 | 10 | @Test 11 | public void closestIntSameBitCount1() { 12 | expected = 209; 13 | x = 210; 14 | 15 | test(expected, x); 16 | } 17 | 18 | @Test 19 | public void closestIntSameBitCount2() { 20 | expected = -211; 21 | x = -210; 22 | 23 | test(expected, x); 24 | } 25 | 26 | @Test 27 | public void closestIntSameBitCount3() { 28 | expected = 23; 29 | x = 15; 30 | 31 | test(expected, x); 32 | } 33 | 34 | @Test 35 | public void closestIntSameBitCount4() { 36 | expected = -14; 37 | x = -15; 38 | 39 | test(expected, x); 40 | } 41 | 42 | private void test(long expected, long x) { 43 | assertEquals(expected, FindClosestInteger.closestIntSameBitCount(x)); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /primitives/src/test/java/MultiplicationNoArithmeticOperatorsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class MultiplicationNoArithmeticOperatorsTest { 6 | 7 | private long expected; 8 | private long x; 9 | private long y; 10 | 11 | @Test 12 | public void multiply1() { 13 | expected = 4; 14 | x = 2; 15 | y = 2; 16 | 17 | test(expected,x,y); 18 | } 19 | 20 | @Test 21 | public void multiply2() { 22 | expected = 0; 23 | x = 2; 24 | y = 0; 25 | 26 | test(expected,x,y); 27 | } 28 | 29 | @Test 30 | public void multiply3() { 31 | expected = 27; 32 | x = 9; 33 | y = 3; 34 | 35 | test(expected,x,y); 36 | } 37 | 38 | private void test(long expected, long x, long y) { 39 | assertEquals(expected, MultiplicationNoArithmeticOperators.multiply(x, y)); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /primitives/src/test/java/RectangleIntersectionTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class RectangleIntersectionTest { 6 | 7 | Rectangle expected; 8 | Rectangle r1; 9 | Rectangle r2; 10 | 11 | @Test 12 | public void intersectRectangle1() { 13 | expected = null; 14 | r1 = new Rectangle(0,0,1,1); 15 | r2 = new Rectangle(0,0,-1,-1); 16 | 17 | test(expected, r1, r2); 18 | } 19 | 20 | @Test 21 | public void intersectRectangle2() { 22 | expected = new Rectangle(2,2,4,4); 23 | r1 = new Rectangle(0,0,4,4); 24 | r2 = new Rectangle(2,2,4,4); 25 | 26 | test(expected, r1, r2); 27 | } 28 | 29 | @Test 30 | public void intersectRectangle3() { 31 | expected = new Rectangle(2,2,4,4); 32 | r1 = new Rectangle(0,0,4,4); 33 | r2 = new Rectangle(2,2,6,6); 34 | 35 | test(expected, r1, r2); 36 | } 37 | 38 | private void test(Rectangle expected, Rectangle r1, Rectangle r2) { 39 | assertEquals(expected, RectangleIntersection.intersectRectangle(r1, r2)); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /primitives/src/test/java/ReverseBitsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ReverseBitsTest { 6 | 7 | private int expected; 8 | private int input; 9 | 10 | @Test 11 | public void reverse1() { 12 | expected = 43261596; 13 | input = 964176192; 14 | 15 | test(expected, input); 16 | } 17 | 18 | private void test(int expected, int input) { 19 | assertEquals(expected, ReverseBits.reverse(input)); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /primitives/src/test/java/ReverseDigitsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ReverseDigitsTest { 6 | 7 | private long expected; 8 | private int input; 9 | 10 | @Test 11 | public void reverse1() { 12 | expected = 24; 13 | input = 42; 14 | 15 | test(expected, input); 16 | } 17 | 18 | @Test 19 | public void reverse2() { 20 | expected = -413; 21 | input = -314; 22 | 23 | test(expected, input); 24 | } 25 | 26 | private void test(long expected, int x) { 27 | assertEquals(expected, ReverseDigits.reverse(x)); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /primitives/src/test/java/SwapBitsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class SwapBitsTest { 6 | 7 | private long expected; 8 | private long x; 9 | private int i; 10 | private int j; 11 | 12 | @Test 13 | public void swapBits1() { 14 | expected = 7; 15 | x = 7; 16 | i = 0; 17 | j = 2; 18 | 19 | test(expected, x, i, j); 20 | } 21 | 22 | @Test 23 | public void swapBits2() { 24 | expected = 22; 25 | x = 7; 26 | i = 0; 27 | j = 4; 28 | 29 | test(expected, x, i, j); 30 | } 31 | 32 | @Test 33 | public void swapBits3() { 34 | expected = 1066; 35 | x = 1570; 36 | i = 3; 37 | j = 9; 38 | 39 | test(expected, x, i, j); 40 | } 41 | 42 | private void test(long expected, long x, int i, int j) { 43 | assertEquals(expected, SwapBits.swapBits(x, i, j)); 44 | 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /recursion/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 16: Recursion 2 | 3 | - [ ] 16.1 TowersOfHanoi 4 | - [ ] 16.2 NQueens 5 | - [ ] 16.3 GeneratePermutations 6 | - [ ] 16.4 GeneratePowerSet 7 | - [ ] 16.5 GenerateSubsetsK 8 | - [ ] 16.6 GenerateStrings 9 | - [ ] 16.7 GeneratePalindromicDecompositions 10 | - [ ] 16.8 GenerateBinaryTrees 11 | - [ ] 16.9 SudokuSolver 12 | - [ ] 16.10 ComputeGrayCode 13 | - [ ] 16.11 ComputeDiameter -------------------------------------------------------------------------------- /recursion/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | recursion 13 | Chapter 16: Recursion 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /recursion/src/main/java/ComputeDiameter.java: -------------------------------------------------------------------------------- 1 | public class ComputeDiameter { 2 | 3 | /* 4 | 16.11 5 | */ 6 | 7 | public static int computeDiameter(TreeNode T) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /recursion/src/main/java/ComputeGrayCode.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeGrayCode { 5 | 6 | /* 7 | 16.10 8 | */ 9 | 10 | public static List grayCode(int numBits) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GenerateBinaryTrees.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GenerateBinaryTrees { 5 | 6 | /* 7 | 16.8 8 | */ 9 | 10 | public static List> generateAllBinaryTrees(int numNodes) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GeneratePalindromicDecompositions.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GeneratePalindromicDecompositions { 5 | 6 | /* 7 | 16.7 8 | */ 9 | 10 | public static List> palindromicPartitioning(String input) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GeneratePermutations.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GeneratePermutations { 5 | 6 | /* 7 | 16.3 8 | */ 9 | 10 | public static List> permutations(List A) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GeneratePowerSet.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GeneratePowerSet { 5 | 6 | /* 7 | 16.4 8 | */ 9 | 10 | public static List> generatePowerSet(List inputSet) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GenerateStrings.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GenerateStrings { 5 | 6 | /* 7 | 16.6 8 | */ 9 | 10 | public static List generateBalancedParentheses(int numpairs) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/GenerateSubsetsK.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class GenerateSubsetsK { 5 | 6 | /* 7 | 16.5 8 | */ 9 | 10 | public static List> combinations(int n, int k) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/NQueens.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class NQueens { 5 | 6 | /* 7 | 16.2 8 | */ 9 | 10 | public static List> nQueens(int n) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /recursion/src/main/java/SudokuSolver.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SudokuSolver { 4 | 5 | /* 6 | 16.9 7 | */ 8 | 9 | public static boolean solveSudoku(List> partialAssignment) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /recursion/src/main/java/TowersOfHanoi.java: -------------------------------------------------------------------------------- 1 | public class TowersOfHanoi { 2 | 3 | /* 4 | 16.1 5 | */ 6 | 7 | private static final int NUM_PEGS = 3; 8 | 9 | public static void compute(int numRings) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /recursion/src/test/java/ComputeDiameterTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class ComputeDiameterTest { 6 | 7 | private int expected; 8 | private TreeNode T; 9 | 10 | @Test 11 | public void computeDiameter1() throws Exception { 12 | expected = 31; 13 | T = TreeNodeUtil.getTreeNode(); 14 | 15 | test(expected, T); 16 | } 17 | 18 | private void test(int expected, TreeNode T) { 19 | assertEquals(expected, ComputeDiameter.computeDiameter(T)); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /recursion/src/test/java/ComputeGrayCodeTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeGrayCodeTest { 9 | 10 | private List expected; 11 | private int numBits; 12 | 13 | @Test 14 | public void grayCode1() throws Exception { 15 | expected = Arrays.asList(0,1,2,3); 16 | numBits = 2; 17 | 18 | test(expected, numBits); 19 | } 20 | 21 | @Test 22 | public void grayCode2() throws Exception { 23 | expected = Arrays.asList(0,1,3,2,6,7,5,4); 24 | numBits = 3; 25 | 26 | test(expected, numBits); 27 | } 28 | 29 | private void test(List expected, int numBits) { 30 | AssertUtils.assertSameContentsInt(expected, ComputeGrayCode.grayCode(numBits)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /recursion/src/test/java/TowersOfHanoiTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class TowersOfHanoiTest { 6 | 7 | private int n; 8 | 9 | @Test 10 | public void compute1() throws Exception { 11 | n = 3; 12 | 13 | test(n); 14 | } 15 | 16 | private void test(int n) { 17 | TowersOfHanoi.compute(n); 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /searching/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 12: Searching 2 | 3 | - [ ] 12.1 SearchSorted 4 | - [ ] 12.2 SearchSortedIndex 5 | - [ ] 12.3 SearchSortedCyclic 6 | - [ ] 12.4 IntegerSquareRoot 7 | - [ ] 12.5 RealSquareRoot 8 | - [ ] 12.6 SearchSorted2D 9 | - [ ] 12.7 FindMinAndMax 10 | - [ ] 12.8 FindKthLargest 11 | - [ ] 12.9 FindMissingIP 12 | - [ ] 12.10 FindDuplicateAndMissing -------------------------------------------------------------------------------- /searching/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | searching 13 | Chapter 12: Searching 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /searching/src/main/java/FindDuplicateAndMissing.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class FindDuplicateAndMissing { 4 | 5 | /* 6 | 12.10 7 | */ 8 | 9 | public static Tuple search(List list) { 10 | 11 | return new Tuple(0,0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/main/java/FindKthLargest.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class FindKthLargest { 4 | 5 | /* 6 | 12.8 7 | */ 8 | 9 | public static int findKth(List list, int k) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/main/java/FindMinAndMax.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class FindMinAndMax { 4 | 5 | /* 6 | 12.7 7 | */ 8 | 9 | public static MinMax findMinMax(List list) { 10 | 11 | return new MinMax(0,0); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /searching/src/main/java/FindMissingIP.java: -------------------------------------------------------------------------------- 1 | public class FindMissingIP { 2 | 3 | /* 4 | 12.9 5 | */ 6 | 7 | private static int search(Iterable sequence) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /searching/src/main/java/IntegerSquareRoot.java: -------------------------------------------------------------------------------- 1 | public class IntegerSquareRoot { 2 | 3 | /* 4 | 12.4 5 | */ 6 | 7 | public static int squareRoot(int n) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /searching/src/main/java/RealSquareRoot.java: -------------------------------------------------------------------------------- 1 | public class RealSquareRoot { 2 | 3 | /* 4 | 12.5 5 | */ 6 | 7 | public static double squareRoot(double x) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /searching/src/main/java/SearchSorted.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SearchSorted { 4 | 5 | /* 6 | 12.1 7 | */ 8 | 9 | public static int search(List list, int k) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/main/java/SearchSorted2D.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SearchSorted2D { 4 | 5 | /* 6 | 12.6 7 | */ 8 | 9 | public static boolean search(List> matrix, int x) { 10 | 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/main/java/SearchSortedCyclic.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SearchSortedCyclic { 4 | 5 | /* 6 | 12.3 7 | */ 8 | 9 | public static int search(List list) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/main/java/SearchSortedIndex.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class SearchSortedIndex { 4 | 5 | /* 6 | 12.2 7 | */ 8 | 9 | public static int search(List list) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /searching/src/test/java/FindKthLargestTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class FindKthLargestTest { 9 | 10 | private int expected; 11 | private List list; 12 | private int k; 13 | 14 | @Test 15 | public void findKth1() throws Exception { 16 | expected = 5; 17 | list = Arrays.asList(3,2,1,5,4); 18 | k = 1; 19 | 20 | test(expected, list, k); 21 | } 22 | 23 | @Test 24 | public void findKth2() throws Exception { 25 | expected = 3; 26 | list = Arrays.asList(3,2,1,5,4); 27 | k = 3; 28 | 29 | test(expected, list, k); 30 | } 31 | 32 | @Test 33 | public void findKth3() throws Exception { 34 | expected = 1; 35 | list = Arrays.asList(3,2,1,5,4); 36 | k = 5; 37 | 38 | test(expected, list, k); 39 | } 40 | 41 | private void test(int expected, List list, int k) { 42 | assertEquals(expected, FindKthLargest.findKth(list, k)); 43 | } 44 | } -------------------------------------------------------------------------------- /searching/src/test/java/FindMinAndMaxTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class FindMinAndMaxTest { 9 | 10 | private MinMax expected; 11 | private List list; 12 | 13 | @Test 14 | public void findMinMax1() throws Exception { 15 | expected = new MinMax(1,5); 16 | list = Arrays.asList(3,2,5,1,2,4); 17 | 18 | test(expected, list); 19 | } 20 | 21 | @Test 22 | public void findMinMax2() throws Exception { 23 | expected = new MinMax(0, 9); 24 | list = Arrays.asList(0,1,2,3,4,5,6,7,8,9); 25 | 26 | test(expected, list); 27 | } 28 | 29 | private void test(MinMax expected, List list) { 30 | assertEquals(expected, FindMinAndMax.findMinMax(list)); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /searching/src/test/java/IntegerSquareRootTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class IntegerSquareRootTest { 6 | 7 | private int expected; 8 | private int n; 9 | 10 | @Test 11 | public void squareRoot1() throws Exception { 12 | expected = 4; 13 | n = 4; 14 | 15 | test(expected, n); 16 | } 17 | 18 | @Test 19 | public void squareRoot2() throws Exception { 20 | expected = 10; 21 | n = 108; 22 | 23 | test(expected, n); 24 | } 25 | 26 | @Test 27 | public void squareRoot3() throws Exception { 28 | expected = 13; 29 | n = 185; 30 | 31 | test(expected, n); 32 | } 33 | 34 | private void test(int expected, int n) { 35 | assertEquals(expected, IntegerSquareRoot.squareRoot(n)); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /searching/src/test/java/RealSquareRootTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.*; 4 | 5 | public class RealSquareRootTest { 6 | 7 | private final double EPSILON = .01; 8 | 9 | private double x; 10 | 11 | @Test 12 | public void squareRoot1() throws Exception { 13 | x = 2.3; 14 | 15 | test(x); 16 | } 17 | 18 | @Test 19 | public void squareRoot3() throws Exception { 20 | x = 534234.948; 21 | 22 | test(x); 23 | } 24 | 25 | @Test 26 | public void squareRoot4() throws Exception { 27 | x = 100000; 28 | 29 | test(x); 30 | } 31 | 32 | private void test(double x) { 33 | assertEquals(Math.sqrt(x), RealSquareRoot.squareRoot(x),EPSILON); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /searching/src/test/java/SearchSortedCyclicTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class SearchSortedCyclicTest { 9 | 10 | private int expected; 11 | private List list; 12 | 13 | @Test 14 | public void search1() throws Exception { 15 | expected = 0; 16 | list = Arrays.asList(0,1,2,3,4); 17 | 18 | test(expected, list); 19 | } 20 | 21 | @Test 22 | public void search2() throws Exception { 23 | expected = 2; 24 | list = Arrays.asList(4,5,0,1,2,3); 25 | 26 | test(expected, list); 27 | } 28 | 29 | @Test 30 | public void search3() throws Exception { 31 | expected = 8; 32 | list = Arrays.asList(2,3,4,5,6,7,8,9,0,1); 33 | 34 | test(expected, list); 35 | } 36 | 37 | public void test(int expected, List list) throws Exception { 38 | assertEquals(expected, SearchSortedCyclic.search(list)); 39 | } 40 | 41 | 42 | } -------------------------------------------------------------------------------- /searching/src/test/java/SearchSortedIndexTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class SearchSortedIndexTest { 9 | 10 | private int expected; 11 | private List list; 12 | 13 | @Test 14 | public void search1() throws Exception { 15 | expected = 3; 16 | list = Arrays.asList(3,3,3,3,3); 17 | 18 | test(expected, list); 19 | } 20 | 21 | @Test 22 | public void search2() throws Exception { 23 | expected = 4; 24 | list = Arrays.asList(9,2,6,3,4); 25 | 26 | test(expected, list); 27 | } 28 | 29 | @Test 30 | public void search3() throws Exception { 31 | expected = 0; 32 | list = Arrays.asList(0,6,3,2,5,7,4,3,1); 33 | 34 | test(expected, list); 35 | } 36 | 37 | public void test(int expected, List list) throws Exception { 38 | assertEquals(expected, SearchSortedIndex.search(list)); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /searching/src/test/java/SearchSortedTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | import org.mockito.internal.util.collections.ArrayUtils; 3 | 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class SearchSortedTest { 9 | 10 | private int n; 11 | private int k; 12 | 13 | @Test 14 | public void search1() throws Exception { 15 | n = 10; 16 | k = 5; 17 | 18 | test(n,k); 19 | } 20 | 21 | @Test 22 | public void search2() throws Exception { 23 | n = 50; 24 | k = 20; 25 | 26 | test(n,k); 27 | } 28 | 29 | @Test 30 | public void search3() throws Exception { 31 | n = 100; 32 | k = 73; 33 | 34 | test(n,k); 35 | } 36 | 37 | @Test 38 | public void search4() throws Exception { 39 | n = 100; 40 | k = 1; 41 | 42 | test(n,k); 43 | } 44 | 45 | @Test 46 | public void search5() throws Exception { 47 | n = 100; 48 | k = 99; 49 | 50 | test(n,k); 51 | } 52 | 53 | public void test(int n, int k) throws Exception { 54 | List list = StreamUtil.sequence(n); 55 | assertEquals(list.indexOf(k), SearchSorted.search(list, k)); 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /sorting/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 14: Sorting 2 | 3 | - [ ] 14.1 ComputeIntersection 4 | - [ ] 14.2 MergeSorted 5 | - [ ] 14.3 RemoveFirstNameDuplicates 6 | - [ ] 14.4 RenderCalendar 7 | - [ ] 14.5 MergeIntervals 8 | - [ ] 14.6 ComputeUnion 9 | - [ ] 14.7 PartitionSortRepeats 10 | - [ ] 14.8 TeamPhotoDay 11 | - [ ] 14.9 ImplementFastSorting 12 | - [ ] 14.10 ComputeSalaryThreshold -------------------------------------------------------------------------------- /sorting/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | sorting 13 | Chapter 14: Sorting 14 | 15 | 16 | gardncl 17 | datastructures 18 | 1.0 19 | 20 | 21 | gardncl 22 | utils 23 | 1.0 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sorting/src/main/java/ComputeIntersection.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeIntersection { 5 | 6 | /* 7 | 14.1 8 | */ 9 | 10 | public static List intersection(List A, List B) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /sorting/src/main/java/ComputeSalaryThreshold.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class ComputeSalaryThreshold { 4 | 5 | /* 6 | 14.10 7 | */ 8 | 9 | public static double findSalaryCap(double targetPayroll, List currentSalaries) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sorting/src/main/java/ImplementFastSorting.java: -------------------------------------------------------------------------------- 1 | public class ImplementFastSorting { 2 | 3 | /* 4 | 14.9 5 | */ 6 | 7 | public static ListNode sort(final ListNode L) { 8 | 9 | return new ListNode<>(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /sorting/src/main/java/MergeIntervals.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class MergeIntervals { 5 | 6 | /* 7 | 14.5 8 | */ 9 | 10 | public static List addInterval(List disjoint, Tuple newInterval) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /sorting/src/main/java/MergeSorted.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MergeSorted { 4 | 5 | /* 6 | 14.2 7 | */ 8 | 9 | public static void merge(List A, int m, List B, int n) { 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /sorting/src/main/java/PartitionSortRepeats.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class PartitionSortRepeats { 4 | 5 | /* 6 | 14.7 7 | */ 8 | 9 | public static void groupByAge(List people) { 10 | 11 | } 12 | 13 | public static class Person { 14 | public Integer age; 15 | public String name; 16 | 17 | public Person(Integer age, String name) { 18 | this.age = age; 19 | this.name = name; 20 | } 21 | 22 | @Override 23 | public String toString() { 24 | return "Person{" + 25 | "age=" + age + 26 | ", name='" + name + '\'' + 27 | '}'; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /sorting/src/main/java/RemoveFirstNameDuplicates.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class RemoveFirstNameDuplicates { 4 | 5 | /* 6 | 14.3 7 | */ 8 | 9 | public static void eliminateDuplicates(List A) { 10 | 11 | } 12 | 13 | public static class Name { 14 | String first; 15 | String last; 16 | 17 | public Name(String first, String last) { 18 | this.first = first; 19 | this.last = last; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sorting/src/main/java/RenderCalendar.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class RenderCalendar { 4 | 5 | /* 6 | 14.4 7 | */ 8 | 9 | public static int findEvents(List A) { 10 | 11 | return 0; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sorting/src/test/java/ImplementFastSortingTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Collections; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ImplementFastSortingTest { 9 | 10 | private ListNode expected; 11 | private ListNode L; 12 | private int n; 13 | 14 | @Test 15 | public void sort1() throws Exception { 16 | n = 10; 17 | 18 | test(n); 19 | } 20 | 21 | @Test 22 | public void sort2() throws Exception { 23 | n = 50; 24 | 25 | test(n); 26 | } 27 | 28 | @Test 29 | public void sort3() throws Exception { 30 | n = 100; 31 | 32 | test(n); 33 | } 34 | 35 | private void test(int n) { 36 | List list = StreamUtil.sequence(n); 37 | expected = NodeUtil.createList(list); 38 | Collections.shuffle(list); 39 | L = NodeUtil.createList(list); 40 | AssertUtils.assertSameList(expected, ImplementFastSorting.sort(L)); 41 | } 42 | } -------------------------------------------------------------------------------- /sorting/src/test/java/MergeSortedTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class MergeSortedTest { 9 | 10 | private List expected; 11 | private List A; 12 | private int m; 13 | private List B; 14 | private int n; 15 | 16 | @Test 17 | public void merge1() throws Exception { 18 | expected = Arrays.asList(3,5,7,11,13,17,19,null); 19 | A = Arrays.asList(5,13,17,null,null,null,null,null); 20 | m = 3; 21 | B = Arrays.asList(3,7,11,19); 22 | n = B.size(); 23 | 24 | test(expected, A, m, B, n); 25 | } 26 | 27 | @Test 28 | public void merge2() throws Exception { 29 | expected = Arrays.asList(1,2,3,4,5,6,7); 30 | A = Arrays.asList(1,2,3,4,5,null,null); 31 | m = 5; 32 | B = Arrays.asList(3,4,5,6,7); 33 | n = B.size(); 34 | 35 | test(expected, A, m, B, n); 36 | } 37 | 38 | 39 | private void test(List expected, List A, int m, List B, int n) { 40 | MergeSorted.merge(A,m,B,n); 41 | assertEquals(expected, A); 42 | } 43 | 44 | 45 | } -------------------------------------------------------------------------------- /stacksandqueues/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9: Stacks and Queues 2 | 3 | - [ ] 9.1 StackWithMax 4 | - [ ] 9.2 EvaluateRPNExpressions 5 | - [ ] 9.3 IsStringWellFormed 6 | - [ ] 9.4 NormalizedPathnames 7 | - [ ] 9.5 SearchPostingsList 8 | - [ ] 9.6 ComputeBuildingsWithView 9 | - [ ] 9.7 ComputeBinaryTreeNodes 10 | - [ ] 9.8 CircularQueue 11 | - [ ] 9.9 QueueWithStacks 12 | - [ ] 9.10 QueueWithMax -------------------------------------------------------------------------------- /stacksandqueues/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | stacksandqueues 13 | Chapter 9: Stacks and Queues 14 | 15 | 16 | 17 | gardncl 18 | datastructures 19 | 1.0 20 | 21 | 22 | gardncl 23 | utils 24 | 1.0 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/CircularQueue.java: -------------------------------------------------------------------------------- 1 | public class CircularQueue { 2 | 3 | /* 4 | 9.8 5 | */ 6 | 7 | private int head = 0, tail = 0, numQueueElements = 0; 8 | private static final int SCALE_FACTOR = 2; 9 | private Integer[] entries; 10 | 11 | public CircularQueue(int capacity) { 12 | this.entries = new Integer[capacity]; 13 | } 14 | 15 | public void enqueue(Integer x) { 16 | 17 | } 18 | 19 | public Integer dequeue() { 20 | 21 | return 0; 22 | } 23 | 24 | public int size() { 25 | return 0; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/ComputeBinaryTreeNodes.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeBinaryTreeNodes { 5 | 6 | /* 7 | 9.7 8 | */ 9 | 10 | public static List> binaryTreeDepthOrder(BinaryTree tree) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/ComputeBuildingsWithView.java: -------------------------------------------------------------------------------- 1 | import java.util.Deque; 2 | import java.util.Iterator; 3 | import java.util.LinkedList; 4 | 5 | public class ComputeBuildingsWithView { 6 | 7 | /* 8 | 9.6 9 | */ 10 | 11 | public static Deque examineBuildingsWithSunset(Iterator sequence) { 12 | 13 | return new LinkedList<>(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/EvaluateRPNExpressions.java: -------------------------------------------------------------------------------- 1 | public class EvaluateRPNExpressions { 2 | 3 | /* 4 | 9.2 5 | */ 6 | 7 | public static Integer eval(String RPNExpression) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/IsStringWellFormed.java: -------------------------------------------------------------------------------- 1 | public class IsStringWellFormed { 2 | 3 | /* 4 | 9.3 5 | */ 6 | 7 | public static boolean isWellFormed(String s) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/NormalizedPathnames.java: -------------------------------------------------------------------------------- 1 | public class NormalizedPathnames { 2 | 3 | /* 4 | 9.4 5 | */ 6 | 7 | public static String shortestEquivalentPath(String path) { 8 | 9 | return ""; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/QueueWithMax.java: -------------------------------------------------------------------------------- 1 | public class QueueWithMax { 2 | 3 | /* 4 | 9.10 5 | */ 6 | 7 | public void enqueue(Integer x) { 8 | 9 | } 10 | 11 | public Integer dequeue() { 12 | 13 | return 0; 14 | } 15 | 16 | public Integer max() { 17 | 18 | return 0; 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/QueueWithStacks.java: -------------------------------------------------------------------------------- 1 | public class QueueWithStacks { 2 | 3 | /* 4 | 9.9 5 | */ 6 | 7 | public QueueWithStacks() { 8 | } 9 | 10 | public void enqueue(Integer x) { 11 | 12 | } 13 | 14 | public Integer dequeue() { 15 | 16 | return 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/SearchPostingsList.java: -------------------------------------------------------------------------------- 1 | public class SearchPostingsList { 2 | 3 | /* 4 | 9.5 5 | */ 6 | 7 | public static void setJumpOrderRecursive(PostingListNode node) { 8 | 9 | } 10 | 11 | public static void setJumpOrderIterative(PostingListNode node) { 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stacksandqueues/src/main/java/StackWithMax.java: -------------------------------------------------------------------------------- 1 | public class StackWithMax { 2 | 3 | /* 4 | 9.1 5 | */ 6 | 7 | public StackWithMax() { 8 | 9 | } 10 | 11 | public Integer max() { 12 | return 0; 13 | } 14 | 15 | public Integer pop() { 16 | return 0; 17 | } 18 | 19 | public void push(Integer x) { 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stacksandqueues/src/test/java/CircularQueueTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.List; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | public class CircularQueueTest { 8 | 9 | private final int capacity = 10; 10 | private int size; 11 | 12 | @Test 13 | public void circularQueue1() throws Exception { 14 | size = 5; 15 | 16 | test(size); 17 | } 18 | 19 | @Test 20 | public void circularQueue2() throws Exception { 21 | size = 10; 22 | 23 | test(size); 24 | } 25 | 26 | @Test 27 | public void circularQueue3() throws Exception { 28 | size = 50; 29 | 30 | test(size); 31 | } 32 | 33 | private void test(int size) { 34 | final CircularQueue queue = new CircularQueue(capacity); 35 | List integers = StreamUtil.sequence(size); 36 | integers.forEach( 37 | i -> queue.enqueue(i) 38 | ); 39 | integers.forEach( 40 | i -> assertEquals(i,queue.dequeue()) 41 | ); 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /stacksandqueues/src/test/java/ComputeBinaryTreeNodesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public class ComputeBinaryTreeNodesTest { 9 | 10 | private List> expected; 11 | private BinaryTree input; 12 | 13 | @Test 14 | public void binaryTreeDepthOrder1() { 15 | expected = Arrays.asList( 16 | Arrays.asList(314), 17 | Arrays.asList(6,6), 18 | Arrays.asList(271,561,2,271), 19 | Arrays.asList(28,0,3,1,28), 20 | Arrays.asList(17,401,257), 21 | Arrays.asList(641) 22 | ); 23 | input = BinaryTreeUtil.getFigureTenDotOne(); 24 | 25 | test(expected, input); 26 | } 27 | 28 | private void test(List> expected, BinaryTree input) { 29 | assertEquals(expected, ComputeBinaryTreeNodes.binaryTreeDepthOrder(input)); 30 | } 31 | } -------------------------------------------------------------------------------- /stacksandqueues/src/test/java/EvaluateRPNExpressionsTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class EvaluateRPNExpressionsTest { 6 | 7 | private Integer expected; 8 | private String expression; 9 | 10 | @Test 11 | public void eval1() { 12 | expected = 1729; 13 | expression = "1729"; 14 | 15 | test(expected, expression); 16 | } 17 | 18 | @Test 19 | public void eval2() { 20 | expected = 15; 21 | expression = "3,4,+,2,x,1,+"; 22 | 23 | test(expected, expression); 24 | } 25 | 26 | @Test 27 | public void eval3() { 28 | expected = -4; 29 | expression = "1,1,+,-2,x"; 30 | 31 | test(expected, expression); 32 | } 33 | 34 | private void test(Integer expected, String expression) { 35 | assertEquals(expected, EvaluateRPNExpressions.eval(expression)); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /stacksandqueues/src/test/java/NormalizedPathnamesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class NormalizedPathnamesTest { 6 | 7 | private String expected; 8 | private String pathname; 9 | 10 | @Test 11 | public void shortestEquivalentPath1() { 12 | expected = "/usr/bin/gcc"; 13 | pathname = "/usr/lib/../bin/gcc"; 14 | 15 | test(expected, pathname); 16 | } 17 | 18 | @Test 19 | public void shortestEquivalentPath2() { 20 | expected = "scripts/awkscripts"; 21 | pathname = "scripts/./../scripts/awkscripts/./."; 22 | 23 | test(expected, pathname); 24 | } 25 | 26 | @Test 27 | public void shortestEquivalentPath3() { 28 | expected = "scripts/awkscripts"; 29 | pathname = "scripts/./../scripts/../../../usr/lib/scripts/awkscripts/./."; 30 | 31 | test(expected, pathname); 32 | } 33 | 34 | private void test(String expected, String pathname) { 35 | assertEquals(expected, NormalizedPathnames.shortestEquivalentPath(pathname)); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /stacksandqueues/src/test/java/QueueWithStacksTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.List; 4 | 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public class QueueWithStacksTest { 8 | 9 | private int size; 10 | 11 | @Test 12 | public void circularQueue1() throws Exception { 13 | size = 5; 14 | 15 | test(size); 16 | } 17 | 18 | @Test 19 | public void circularQueue2() throws Exception { 20 | size = 10; 21 | 22 | test(size); 23 | } 24 | 25 | @Test 26 | public void circularQueue3() throws Exception { 27 | size = 50; 28 | 29 | test(size); 30 | } 31 | 32 | private void test(int size) { 33 | final QueueWithStacks queue = new QueueWithStacks(); 34 | List integers = StreamUtil.sequence(size); 35 | integers.forEach( 36 | i -> queue.enqueue(i) 37 | ); 38 | integers.forEach( 39 | i -> assertEquals(i,queue.dequeue()) 40 | ); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /strings/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7: Strings 2 | 3 | - [ ] 7.1 InterconvertStringAndInteger 4 | - [ ] 7.2 BaseConversion 5 | - [ ] 7.3 SpreadsheetColumnEncoding 6 | - [ ] 7.4 ReplaceAndRemove 7 | - [ ] 7.5 PalindromeAlphanumeric 8 | - [ ] 7.6 ReverseWordsInASentence 9 | - [ ] 7.7 ComputeMnemonicsPhoneNumber 10 | - [ ] 7.8 LookAndSayProblem 11 | - [ ] 7.9 RomanToDecimal 12 | - [ ] 7.10 ComputeValidIPAddresses 13 | - [ ] 7.11 StringSinusoidal 14 | - [ ] 7.12 RunLengthEncoding 15 | - [ ] 7.13 FindFirstOccurrenceOfSubstring -------------------------------------------------------------------------------- /strings/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | strings 13 | Chapter 7: Strings 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /strings/src/main/java/BaseConversion.java: -------------------------------------------------------------------------------- 1 | public class BaseConversion { 2 | 3 | /* 4 | 7.2 5 | */ 6 | 7 | public static String baseConversion(String s, int b1, int b2) { 8 | 9 | return ""; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /strings/src/main/java/ComputeMnemonicsPhoneNumber.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ComputeMnemonicsPhoneNumber { 4 | 5 | /* 6 | 7.7 7 | */ 8 | 9 | 10 | public static List computeMnemonics(String phoneNumber) { 11 | 12 | return Arrays.asList(""); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /strings/src/main/java/ComputeValidIPAddresses.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | 4 | public class ComputeValidIPAddresses { 5 | 6 | /* 7 | 7.10 8 | */ 9 | 10 | public static List computeValidIPAddresses(String s) { 11 | 12 | return Collections.emptyList(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /strings/src/main/java/FindFirstOccurrenceOfSubstring.java: -------------------------------------------------------------------------------- 1 | public class FindFirstOccurrenceOfSubstring { 2 | 3 | /* 4 | 7.13 5 | */ 6 | 7 | public static int findFirst(String string, String substring) { 8 | 9 | return 0; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /strings/src/main/java/InterconvertStringAndInteger.java: -------------------------------------------------------------------------------- 1 | public class InterconvertStringAndInteger { 2 | 3 | /* 4 | 7.1 5 | */ 6 | 7 | public static Integer stringToInt(String number) { 8 | 9 | return 0; 10 | } 11 | 12 | public static String intToString(Integer number) { 13 | 14 | return ""; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /strings/src/main/java/LookAndSayProblem.java: -------------------------------------------------------------------------------- 1 | public class LookAndSayProblem { 2 | 3 | /* 4 | 7.8 5 | */ 6 | 7 | public static String lookAndSay(int n) { 8 | 9 | return ""; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /strings/src/main/java/PalindromeAlphanumeric.java: -------------------------------------------------------------------------------- 1 | public class PalindromeAlphanumeric { 2 | 3 | /* 4 | 7.5 5 | */ 6 | 7 | public static boolean isPalindrome(String input) { 8 | 9 | return false; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /strings/src/main/java/ReplaceAndRemove.java: -------------------------------------------------------------------------------- 1 | public class ReplaceAndRemove { 2 | 3 | /* 4 | 7.4 5 | */ 6 | 7 | public static String replaceAndRemove(char[] s, int k) { 8 | 9 | return s.toString(); 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /strings/src/main/java/ReverseWordsInASentence.java: -------------------------------------------------------------------------------- 1 | public class ReverseWordsInASentence { 2 | 3 | /* 4 | 7.6 5 | */ 6 | 7 | public static String reverseWordsInASentence(String input) { 8 | 9 | return ""; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /strings/src/main/java/RomanToDecimal.java: -------------------------------------------------------------------------------- 1 | public class RomanToDecimal { 2 | 3 | /* 4 | 7.9 5 | */ 6 | 7 | public static int romanToDecimal(String roman) { 8 | 9 | return 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /strings/src/main/java/RunLengthEncoding.java: -------------------------------------------------------------------------------- 1 | public class RunLengthEncoding { 2 | 3 | /* 4 | 7.12 5 | */ 6 | 7 | public static String encode(String s) { 8 | 9 | return ""; 10 | } 11 | 12 | public static String decode(String s) { 13 | 14 | return ""; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /strings/src/main/java/SpreadsheetColumnEncoding.java: -------------------------------------------------------------------------------- 1 | public class SpreadsheetColumnEncoding { 2 | 3 | /* 4 | 7.3 5 | */ 6 | 7 | public static int decodeSpreadsheetColumn(String code) { 8 | 9 | return 0; 10 | } 11 | 12 | 13 | } 14 | -------------------------------------------------------------------------------- /strings/src/main/java/StringSinusoidal.java: -------------------------------------------------------------------------------- 1 | public class StringSinusoidal { 2 | 3 | /* 4 | 7.11 5 | */ 6 | 7 | public static String snakeString(String s) { 8 | 9 | return ""; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /strings/src/test/java/ComputeMnemonicsPhoneNumberTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.Arrays; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.*; 7 | 8 | public class ComputeMnemonicsPhoneNumberTest { 9 | 10 | private List expectedResults; 11 | private String phoneNumber; 12 | 13 | @Test 14 | public void computeMnemonics1() { 15 | phoneNumber = "2276696"; 16 | expectedResults = Arrays.asList("ACRONYM", "ABPOMZN"); 17 | 18 | test(expectedResults, phoneNumber); 19 | } 20 | 21 | @Test 22 | public void computeMnemonics2() { 23 | phoneNumber = "5387739"; 24 | expectedResults = Arrays.asList("JETPREY", "LETSSEX", "JETSPEW"); 25 | 26 | test(expectedResults, phoneNumber); 27 | } 28 | 29 | private void test(List expectedResults, String phoneNumber) { 30 | List actualResults = ComputeMnemonicsPhoneNumber.computeMnemonics(phoneNumber); 31 | for (String mnemonic : expectedResults) { 32 | assertTrue(actualResults.contains(mnemonic)); 33 | } 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /strings/src/test/java/ComputeValidIPAddressesTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import static org.junit.Assert.assertTrue; 7 | 8 | public class ComputeValidIPAddressesTest { 9 | 10 | private List expectedToContain; 11 | private String s; 12 | 13 | @Test 14 | public void computeValidIPAddresses1() { 15 | expectedToContain = new ArrayList<>(); 16 | expectedToContain.add("192.168.1.1"); 17 | expectedToContain.add("19.216.81.1"); 18 | s = "19216811"; 19 | 20 | test(expectedToContain, s); 21 | } 22 | 23 | @Test 24 | public void computeValidIPAddresses2() { 25 | expectedToContain = new ArrayList<>(); 26 | expectedToContain.add("255.255.11.135"); 27 | expectedToContain.add("255.255.111.35"); 28 | s = "25525511135"; 29 | 30 | test(expectedToContain, s); 31 | } 32 | 33 | private void test(List expectedToContain, String s) { 34 | List result = ComputeValidIPAddresses.computeValidIPAddresses(s); 35 | for (String expected : expectedToContain) 36 | assertTrue(result.contains(expected)); 37 | } 38 | 39 | 40 | } -------------------------------------------------------------------------------- /strings/src/test/java/PalindromeAlphanumericTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class PalindromeAlphanumericTest { 6 | 7 | boolean palindrome; 8 | String input; 9 | 10 | @Test 11 | public void isPalindrome1() { 12 | palindrome = true; 13 | input = "A man, a plan, a canal, Panama."; 14 | 15 | test(palindrome, input); 16 | } 17 | 18 | @Test 19 | public void isPalindrome2() { 20 | palindrome = true; 21 | input = "Able was I, ere I saw Elba!"; 22 | 23 | test(palindrome, input); 24 | } 25 | 26 | @Test 27 | public void isPalindrome3() { 28 | palindrome = false; 29 | input = "Ray a Ray"; 30 | 31 | test(palindrome, input); 32 | } 33 | 34 | public void test(boolean palindrome, String input) { 35 | assertEquals(palindrome, PalindromeAlphanumeric.isPalindrome(input)); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /strings/src/test/java/ReplaceAndRemoveTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ReplaceAndRemoveTest { 6 | 7 | private String expected; 8 | private String input; 9 | private int k; 10 | 11 | @Test 12 | public void replaceAndRemove1() { 13 | expected = "dd"; 14 | input = "a "; 15 | k=1; 16 | 17 | test(expected, input, k); 18 | } 19 | 20 | @Test 21 | public void replaceAndRemove2() { 22 | expected = "dd"; 23 | input = "ab"; 24 | k=2; 25 | 26 | test(expected, input, k); 27 | } 28 | 29 | @Test 30 | public void replaceAndRemove3() { 31 | expected = "ddddc"; 32 | input = "abac "; 33 | k=4; 34 | 35 | test(expected, input, k); 36 | } 37 | 38 | @Test 39 | public void replaceAndRemove4() { 40 | expected = "ddcdcdd"; 41 | input = "acdbbca"; 42 | k=7; 43 | 44 | test(expected, input, k); 45 | } 46 | 47 | private void test(String expected, String input, int k) { 48 | assertEquals(expected, ReplaceAndRemove.replaceAndRemove(input.toCharArray(), k)); 49 | } 50 | } -------------------------------------------------------------------------------- /strings/src/test/java/ReverseWordsInASentenceTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class ReverseWordsInASentenceTest { 6 | 7 | private String expected; 8 | private String input; 9 | 10 | @Test 11 | public void reverseWordsInASentence1() { 12 | expected = "Alice"; 13 | input = "Alice"; 14 | 15 | test(expected, input); 16 | } 17 | 18 | @Test 19 | public void reverseWordsInASentence2() { 20 | expected = "Alice likes"; 21 | input = "likes Alice"; 22 | 23 | test(expected, input); 24 | } 25 | 26 | @Test 27 | public void reverseWordsInASentence3() { 28 | expected = "Alice likes Bob"; 29 | input = "Bob likes Alice"; 30 | 31 | test(expected, input); 32 | } 33 | 34 | @Test 35 | public void reverseWordsInASentence4() { 36 | expected = "Alice doesn't like Bob"; 37 | input = "Bob like doesn't Alice"; 38 | 39 | test(expected, input); 40 | } 41 | 42 | private void test(String expected, String input) { 43 | assertEquals(expected, ReverseWordsInASentence.reverseWordsInASentence(input)); 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /strings/src/test/java/RomanToDecimalTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class RomanToDecimalTest { 6 | 7 | private int expected; 8 | private String roman; 9 | 10 | @Test 11 | public void romanToDecimal1() { 12 | expected = 1; 13 | roman = "I"; 14 | 15 | test(expected, roman); 16 | } 17 | 18 | @Test 19 | public void romanToDecimal2() { 20 | expected = 27; 21 | roman = "XXVII"; 22 | 23 | test(expected, roman); 24 | } 25 | 26 | @Test 27 | public void romanToDecimal3() { 28 | expected = 3492; 29 | roman = "MMMCDXCII"; 30 | 31 | test(expected, roman); 32 | } 33 | 34 | @Test 35 | public void romanToDecimal4() { 36 | expected = 4999; 37 | roman = "MMMMCMXCIX"; 38 | 39 | test(expected, roman); 40 | } 41 | 42 | private void test(int expected, String roman) { 43 | assertEquals(expected, RomanToDecimal.romanToDecimal(roman)); 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /strings/src/test/java/RunLengthEncodingTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class RunLengthEncodingTest { 6 | 7 | private String expected; 8 | private String input; 9 | 10 | 11 | @Test 12 | public void encode1() { 13 | expected = "4a1b3c2a"; 14 | input = "aaaabcccaa"; 15 | 16 | testEncode(expected, input); 17 | } 18 | 19 | @Test 20 | public void encode2() { 21 | expected = "3e4f2e"; 22 | input = "eeeffffee"; 23 | 24 | testEncode(expected, input); 25 | } 26 | 27 | private void testEncode(String expected, String input) { 28 | assertEquals(expected, RunLengthEncoding.encode(input)); 29 | } 30 | 31 | @Test 32 | public void decode1() { 33 | expected = "aaaabcccaa"; 34 | input = "4a1b3c2a"; 35 | 36 | testDecode(expected, input); 37 | } 38 | 39 | @Test 40 | public void decode2() { 41 | expected = "eeeffffee"; 42 | input = "3e4f2e"; 43 | 44 | testDecode(expected, input); 45 | } 46 | 47 | private void testDecode(String expected, String input) { 48 | assertEquals(expected, RunLengthEncoding.decode(input)); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /strings/src/test/java/SpreadsheetColumnEncodingTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | 6 | public class SpreadsheetColumnEncodingTest { 7 | 8 | private int result; 9 | private String code; 10 | 11 | @Test 12 | public void decodeSpreadsheetColumn1() { 13 | result = 1; 14 | code = "A"; 15 | 16 | test(result, code); 17 | } 18 | 19 | @Test 20 | public void decodeSpreadsheetColumn2() { 21 | result = 4; 22 | code = "D"; 23 | 24 | test(result, code); 25 | } 26 | 27 | @Test 28 | public void decodeSpreadsheetColumn3() { 29 | result = 27; 30 | code = "AA"; 31 | 32 | test(result, code); 33 | } 34 | 35 | @Test 36 | public void decodeSpreadsheetColumn4() { 37 | result = 702; 38 | code = "ZZ"; 39 | 40 | test(result, code); 41 | } 42 | 43 | private void test(int result, String code) { 44 | assertEquals(result, SpreadsheetColumnEncoding.decodeSpreadsheetColumn(code)); 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /strings/src/test/java/StringSinusoidalTest.java: -------------------------------------------------------------------------------- 1 | import org.junit.Test; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | 5 | public class StringSinusoidalTest { 6 | 7 | private String expected; 8 | private String input; 9 | 10 | @Test 11 | public void snakeString1() { 12 | expected = "bacd"; 13 | input = "abcd"; 14 | 15 | test(expected, input); 16 | } 17 | 18 | @Test 19 | public void snakeString2() { 20 | expected = "e lHloWrdlo!"; 21 | input = "Hello World!"; 22 | 23 | test(expected, input); 24 | } 25 | 26 | private void test(String expected, String input) { 27 | assertEquals(expected, StringSinusoidal.snakeString(input)); 28 | } 29 | 30 | 31 | } -------------------------------------------------------------------------------- /utils/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | elements-of-programming-interviews 7 | gardncl 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | utils 13 | Utilities 14 | 15 | 16 | 17 | gardncl 18 | datastructures 19 | 1.0 20 | 21 | 22 | junit 23 | junit 24 | ${junit.version} 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /utils/src/main/java/NodeUtil.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.stream.IntStream; 3 | 4 | public class NodeUtil { 5 | 6 | public static ListNode createList(int n) { 7 | ListNode dummyHead = new ListNode(null); 8 | ListNode list = dummyHead; 9 | for (Integer i : IntStream.range(0,n).toArray()) { 10 | list.next = new ListNode<>(i); 11 | list = list.next; 12 | } 13 | return dummyHead.next; 14 | } 15 | 16 | public static ListNode createList(List input) { 17 | ListNode dummyHead = new ListNode(null); 18 | ListNode list = dummyHead; 19 | for (Integer i : input) { 20 | list.next = new ListNode<>(i); 21 | list = list.next; 22 | } 23 | return dummyHead.next; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /utils/src/main/java/StreamUtil.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Collections; 3 | import java.util.List; 4 | import java.util.stream.Collectors; 5 | import java.util.stream.IntStream; 6 | 7 | public class StreamUtil { 8 | 9 | public static IntStream revRange(int from, int to) { 10 | return IntStream.range(from, to).map(i -> to - i + from - 1); 11 | } 12 | 13 | public static List sequence(int n) { 14 | return Arrays.stream(IntStream.range(0, n).toArray()).boxed().collect(Collectors.toList()); 15 | } 16 | 17 | public static List shuffle(List list) { 18 | Collections.shuffle(list); 19 | return list; 20 | } 21 | 22 | 23 | } 24 | --------------------------------------------------------------------------------