├── .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