diff --git a/datastructures/src/main/java/DuplicateAndMissing.java b/datastructures/src/main/java/DuplicateAndMissing.java new file mode 100644 index 0000000..42fb362 --- /dev/null +++ b/datastructures/src/main/java/DuplicateAndMissing.java @@ -0,0 +1,25 @@ +public class DuplicateAndMissing { + + public Integer duplicate; + public Integer missing; + + public DuplicateAndMissing() { + } + + public DuplicateAndMissing(Integer duplicate, Integer missing) { + this.duplicate = duplicate; + this.missing = missing; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DuplicateAndMissing that = (DuplicateAndMissing) o; + + if (duplicate != null ? !duplicate.equals(that.duplicate) : that.duplicate != null) return false; + return missing != null ? missing.equals(that.missing) : that.missing == null; + } + +} diff --git a/datastructures/src/main/java/MinMax.java b/datastructures/src/main/java/MinMax.java new file mode 100644 index 0000000..d361b3b --- /dev/null +++ b/datastructures/src/main/java/MinMax.java @@ -0,0 +1,20 @@ +public class MinMax { + public Integer min; + public Integer max; + + public MinMax(Integer min, Integer max) { + this.min = min; + this.max = max; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + MinMax minMax = (MinMax) o; + + if (min != null ? !min.equals(minMax.min) : minMax.min != null) return false; + return max != null ? max.equals(minMax.max) : minMax.max == null; + } +} diff --git a/pom.xml b/pom.xml index 40e10b6..c02dfa6 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ stacksandqueues binarytrees heaps + searching pom diff --git a/searching/README.md b/searching/README.md new file mode 100644 index 0000000..ff71a93 --- /dev/null +++ b/searching/README.md @@ -0,0 +1,12 @@ +# Chapter 12: Searching + +* 12.1 SearchSorted +* 12.2 SearchSortedIndex +* 12.3 SearchSortedCyclic +* 12.4 IntegerSquareRoot +* 12.5 RealSquareRoot +* 12.6 SearchSorted2D +* 12.7 FindMinAndMax +* 12.8 FindKthLargest +* 12.9 FindMissingIP +* 12.10 FindDuplicateAndMissing \ No newline at end of file diff --git a/searching/pom.xml b/searching/pom.xml new file mode 100644 index 0000000..28de463 --- /dev/null +++ b/searching/pom.xml @@ -0,0 +1,29 @@ + + + + elements-of-programming-interviews + gardncl + 1.5 + + 4.0.0 + + searching + Chapter 12: Searching + + + gardncl + datastructures + 1.4 + + + gardncl + utils + 1.4 + + + + + + \ No newline at end of file diff --git a/searching/src/main/java/FindDuplicateAndMissing.java b/searching/src/main/java/FindDuplicateAndMissing.java new file mode 100644 index 0000000..7434680 --- /dev/null +++ b/searching/src/main/java/FindDuplicateAndMissing.java @@ -0,0 +1,18 @@ +import java.util.List; + +public class FindDuplicateAndMissing { + + /* + 12.10 + + You are given an array of n integers, each between 0 and n - 1, + inclusive. Exactly one element appears twice, implying exactly + one number between 0 and n - 1 is missing from the array. How + would you compute the duplicate and missing numbers? + */ + + public static DuplicateAndMissing search(List list) { + + return new DuplicateAndMissing(0,0); + } +} diff --git a/searching/src/main/java/FindKthLargest.java b/searching/src/main/java/FindKthLargest.java new file mode 100644 index 0000000..cf3a08e --- /dev/null +++ b/searching/src/main/java/FindKthLargest.java @@ -0,0 +1,16 @@ +import java.util.List; + +public class FindKthLargest { + + /* + 12.8 + + Design an algorithm for computing the kth largest + element in an array. Assume entries are distinct. + */ + + public static int findKth(List list, int k) { + + return 0; + } +} diff --git a/searching/src/main/java/FindMinAndMax.java b/searching/src/main/java/FindMinAndMax.java new file mode 100644 index 0000000..834c9ec --- /dev/null +++ b/searching/src/main/java/FindMinAndMax.java @@ -0,0 +1,16 @@ +import java.util.List; + +public class FindMinAndMax { + + /* + 12.7 + + Design an algorithm to find the min and max elements in an array. + */ + + public static MinMax findMinMax(List list) { + + return new MinMax(0,0); + } + +} diff --git a/searching/src/main/java/FindMissingIP.java b/searching/src/main/java/FindMissingIP.java new file mode 100644 index 0000000..6b4700b --- /dev/null +++ b/searching/src/main/java/FindMissingIP.java @@ -0,0 +1,17 @@ +public class FindMissingIP { + + /* + 12.9 + + Suppose you were given a file containing roughly one billion + IP addresses, each of which is a 32-bit quantity. How would + you programmatically find an IP address that is not in the file? + Assume you have unlimited drive space but only a few megabytes + of RAM at your disposal. + */ + + private static int search(Iterable sequence) { + + return 0; + } +} diff --git a/searching/src/main/java/IntegerSquareRoot.java b/searching/src/main/java/IntegerSquareRoot.java new file mode 100644 index 0000000..79d9978 --- /dev/null +++ b/searching/src/main/java/IntegerSquareRoot.java @@ -0,0 +1,15 @@ +public class IntegerSquareRoot { + + /* + 12.4 + + Write a program which takes a non-negative integer and + returns the largest integer whose square is less that + or equal to the given integer. + */ + + public static int squareRoot(int n) { + + return 0; + } +} diff --git a/searching/src/main/java/RealSquareRoot.java b/searching/src/main/java/RealSquareRoot.java new file mode 100644 index 0000000..8bc8378 --- /dev/null +++ b/searching/src/main/java/RealSquareRoot.java @@ -0,0 +1,14 @@ +public class RealSquareRoot { + + /* + 12.5 + + Implement a function which takes as input a floating point + value and returns the square root. + */ + + public static double squareRoot(double x) { + + return 0; + } +} diff --git a/searching/src/main/java/SearchSorted.java b/searching/src/main/java/SearchSorted.java new file mode 100644 index 0000000..bf8e9cf --- /dev/null +++ b/searching/src/main/java/SearchSorted.java @@ -0,0 +1,17 @@ +import java.util.List; + +public class SearchSorted { + + /* + 12.1 + + Write a method that takes a sorted array and a key and + returns the index of the first occurrence of that key + in the array. + */ + + public static int search(List list, int k) { + + return 0; + } +} diff --git a/searching/src/main/java/SearchSorted2D.java b/searching/src/main/java/SearchSorted2D.java new file mode 100644 index 0000000..67fd52d --- /dev/null +++ b/searching/src/main/java/SearchSorted2D.java @@ -0,0 +1,16 @@ +import java.util.List; + +public class SearchSorted2D { + + /* + 12.6 + + Design an algorithm that takes a 2d sorted array and a + number and checks whether that number appears in the array. + */ + + public static boolean search(List> matrix, int x) { + + return false; + } +} diff --git a/searching/src/main/java/SearchSortedCyclic.java b/searching/src/main/java/SearchSortedCyclic.java new file mode 100644 index 0000000..fb0d319 --- /dev/null +++ b/searching/src/main/java/SearchSortedCyclic.java @@ -0,0 +1,17 @@ +import java.util.List; + +public class SearchSortedCyclic { + + /* + 12.3 + + Design an O(logn) algorithm for finding the position + of the smallest element in a cyclically sorted array. + Assume all the elements are distinct. + */ + + public static int search(List list) { + + return 0; + } +} diff --git a/searching/src/main/java/SearchSortedIndex.java b/searching/src/main/java/SearchSortedIndex.java new file mode 100644 index 0000000..be6647d --- /dev/null +++ b/searching/src/main/java/SearchSortedIndex.java @@ -0,0 +1,17 @@ +import java.util.List; + +public class SearchSortedIndex { + + /* + 12.2 + + Design an efficient algorithm that takes a sorted array + of distinct integers, and returns an index i such that + the element at index i equals i. + */ + + public static int search(List list) { + + return 0; + } +} diff --git a/searching/src/test/java/FindDuplicateAndMissingTest.java b/searching/src/test/java/FindDuplicateAndMissingTest.java new file mode 100644 index 0000000..994c69c --- /dev/null +++ b/searching/src/test/java/FindDuplicateAndMissingTest.java @@ -0,0 +1,47 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class FindDuplicateAndMissingTest { + + private DuplicateAndMissing expected; + private List list; + + @Test + public void search1() throws Exception { + expected = new DuplicateAndMissing(1,7); + list = Arrays.asList(5,4,3,1,6,8,9,1,2); + + test(expected, list); + } + + @Test + public void search2() throws Exception { + expected = new DuplicateAndMissing(); + list = StreamUtil.shuffle(StreamUtil.sequence(100)); + expected.duplicate = list.get(25); + expected.missing = list.get(75); + list.set(75,expected.duplicate); + + test(expected, list); + } + + @Test + public void search3() throws Exception { + expected = new DuplicateAndMissing(); + list = StreamUtil.shuffle(StreamUtil.sequence(1000)); + expected.duplicate = list.get(250); + expected.missing = list.get(750); + list.set(750,expected.duplicate); + + test(expected, list); + } + + private void test(DuplicateAndMissing expected, List list) { + assertEquals(expected, FindDuplicateAndMissing.search(list)); + } + +} \ No newline at end of file diff --git a/searching/src/test/java/FindKthLargestTest.java b/searching/src/test/java/FindKthLargestTest.java new file mode 100644 index 0000000..f2e5d6b --- /dev/null +++ b/searching/src/test/java/FindKthLargestTest.java @@ -0,0 +1,44 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class FindKthLargestTest { + + private int expected; + private List list; + private int k; + + @Test + public void findKth1() throws Exception { + expected = 5; + list = Arrays.asList(3,2,1,5,4); + k = 1; + + test(expected, list, k); + } + + @Test + public void findKth2() throws Exception { + expected = 3; + list = Arrays.asList(3,2,1,5,4); + k = 3; + + test(expected, list, k); + } + + @Test + public void findKth3() throws Exception { + expected = 1; + list = Arrays.asList(3,2,1,5,4); + k = 5; + + test(expected, list, k); + } + + private void test(int expected, List list, int k) { + assertEquals(expected, FindKthLargest.findKth(list, k)); + } +} \ No newline at end of file diff --git a/searching/src/test/java/FindMinAndMaxTest.java b/searching/src/test/java/FindMinAndMaxTest.java new file mode 100644 index 0000000..b93c619 --- /dev/null +++ b/searching/src/test/java/FindMinAndMaxTest.java @@ -0,0 +1,33 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class FindMinAndMaxTest { + + private MinMax expected; + private List list; + + @Test + public void findMinMax1() throws Exception { + expected = new MinMax(1,5); + list = Arrays.asList(3,2,5,1,2,4); + + test(expected, list); + } + + @Test + public void findMinMax2() throws Exception { + expected = new MinMax(0, 9); + list = Arrays.asList(0,1,2,3,4,5,6,7,8,9); + + test(expected, list); + } + + private void test(MinMax expected, List list) { + assertEquals(expected, FindMinAndMax.findMinMax(list)); + } + +} \ No newline at end of file diff --git a/searching/src/test/java/IntegerSquareRootTest.java b/searching/src/test/java/IntegerSquareRootTest.java new file mode 100644 index 0000000..7211d66 --- /dev/null +++ b/searching/src/test/java/IntegerSquareRootTest.java @@ -0,0 +1,38 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class IntegerSquareRootTest { + + private int expected; + private int n; + + @Test + public void squareRoot1() throws Exception { + expected = 4; + n = 4; + + test(expected, n); + } + + @Test + public void squareRoot2() throws Exception { + expected = 10; + n = 108; + + test(expected, n); + } + + @Test + public void squareRoot3() throws Exception { + expected = 13; + n = 185; + + test(expected, n); + } + + private void test(int expected, int n) { + assertEquals(expected, IntegerSquareRoot.squareRoot(n)); + } + +} \ No newline at end of file diff --git a/searching/src/test/java/RealSquareRootTest.java b/searching/src/test/java/RealSquareRootTest.java new file mode 100644 index 0000000..23de524 --- /dev/null +++ b/searching/src/test/java/RealSquareRootTest.java @@ -0,0 +1,36 @@ +import org.junit.Test; + +import static org.junit.Assert.*; + +public class RealSquareRootTest { + + private final double EPSILON = .01; + + private double x; + + @Test + public void squareRoot1() throws Exception { + x = 2.3; + + test(x); + } + + @Test + public void squareRoot3() throws Exception { + x = 534234.948; + + test(x); + } + + @Test + public void squareRoot4() throws Exception { + x = 100000; + + test(x); + } + + private void test(double x) { + assertEquals(Math.sqrt(x), RealSquareRoot.squareRoot(x),EPSILON); + } + +} \ No newline at end of file diff --git a/searching/src/test/java/SearchSorted2DTest.java b/searching/src/test/java/SearchSorted2DTest.java new file mode 100644 index 0000000..8a4707d --- /dev/null +++ b/searching/src/test/java/SearchSorted2DTest.java @@ -0,0 +1,59 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class SearchSorted2DTest { + + private boolean expected; + private List> matrix; + private int x; + + @Test + public void search1() throws Exception { + expected = false; + matrix = Arrays.asList( + Arrays.asList(1,2,3,4), + Arrays.asList(5,6,7,8), + Arrays.asList(7,8,9,10), + Arrays.asList(11,12,13,14) + ); + x = 15; + + test(expected, matrix, x); + } + + @Test + public void search2() throws Exception { + expected = true; + matrix = Arrays.asList( + Arrays.asList(1,2,3,4), + Arrays.asList(5,6,7,8), + Arrays.asList(7,8,9,10), + Arrays.asList(11,12,13,14) + ); + x = 4; + + test(expected, matrix, x); + } + + @Test + public void search3() throws Exception { + expected = true; + matrix = Arrays.asList( + Arrays.asList(1,2,3,4), + Arrays.asList(5,6,7,8), + Arrays.asList(7,8,9,10), + Arrays.asList(11,12,13,14) + ); + x = 9; + + test(expected, matrix, x); + } + + private void test(boolean expected, List> matrix, int x) { + assertEquals(expected, SearchSorted2D.search(matrix, x)); + } +} \ No newline at end of file diff --git a/searching/src/test/java/SearchSortedCyclicTest.java b/searching/src/test/java/SearchSortedCyclicTest.java new file mode 100644 index 0000000..77434a9 --- /dev/null +++ b/searching/src/test/java/SearchSortedCyclicTest.java @@ -0,0 +1,42 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class SearchSortedCyclicTest { + + private int expected; + private List list; + + @Test + public void search1() throws Exception { + expected = 0; + list = Arrays.asList(0,1,2,3,4); + + test(expected, list); + } + + @Test + public void search2() throws Exception { + expected = 2; + list = Arrays.asList(4,5,0,1,2,3); + + test(expected, list); + } + + @Test + public void search3() throws Exception { + expected = 8; + list = Arrays.asList(2,3,4,5,6,7,8,9,0,1); + + test(expected, list); + } + + public void test(int expected, List list) throws Exception { + assertEquals(expected, SearchSortedCyclic.search(list)); + } + + +} \ No newline at end of file diff --git a/searching/src/test/java/SearchSortedIndexTest.java b/searching/src/test/java/SearchSortedIndexTest.java new file mode 100644 index 0000000..0cf470b --- /dev/null +++ b/searching/src/test/java/SearchSortedIndexTest.java @@ -0,0 +1,41 @@ +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class SearchSortedIndexTest { + + private int expected; + private List list; + + @Test + public void search1() throws Exception { + expected = 3; + list = Arrays.asList(3,3,3,3,3); + + test(expected, list); + } + + @Test + public void search2() throws Exception { + expected = 4; + list = Arrays.asList(9,2,6,3,4); + + test(expected, list); + } + + @Test + public void search3() throws Exception { + expected = 0; + list = Arrays.asList(0,6,3,2,5,7,4,3,1); + + test(expected, list); + } + + public void test(int expected, List list) throws Exception { + assertEquals(expected, SearchSortedIndex.search(list)); + } + +} \ No newline at end of file diff --git a/searching/src/test/java/SearchSortedTest.java b/searching/src/test/java/SearchSortedTest.java new file mode 100644 index 0000000..9687ed2 --- /dev/null +++ b/searching/src/test/java/SearchSortedTest.java @@ -0,0 +1,58 @@ +import org.junit.Test; +import org.mockito.internal.util.collections.ArrayUtils; + +import java.util.List; + +import static org.junit.Assert.*; + +public class SearchSortedTest { + + private int n; + private int k; + + @Test + public void search1() throws Exception { + n = 10; + k = 5; + + test(n,k); + } + + @Test + public void search2() throws Exception { + n = 50; + k = 20; + + test(n,k); + } + + @Test + public void search3() throws Exception { + n = 100; + k = 73; + + test(n,k); + } + + @Test + public void search4() throws Exception { + n = 100; + k = 1; + + test(n,k); + } + + @Test + public void search5() throws Exception { + n = 100; + k = 100; + + test(n,k); + } + + public void test(int n, int k) throws Exception { + List list = StreamUtil.sequence(n); + assertEquals(list.indexOf(k), SearchSorted.search(list, k)); + } + +} \ No newline at end of file