-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
648 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>elements-of-programming-interviews</artifactId> | ||
<groupId>gardncl</groupId> | ||
<version>1.5</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>searching</artifactId> | ||
<name>Chapter 12: Searching</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>datastructures</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>gardncl</groupId> | ||
<artifactId>utils</artifactId> | ||
<version>1.4</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list) { | ||
|
||
return new DuplicateAndMissing(0,0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list, int k) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list) { | ||
|
||
return new MinMax(0,0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> sequence) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list, int k) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Integer>> matrix, int x) { | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> list) { | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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<Integer> list) { | ||
assertEquals(expected, FindDuplicateAndMissing.search(list)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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<Integer> list, int k) { | ||
assertEquals(expected, FindKthLargest.findKth(list, k)); | ||
} | ||
} |
Oops, something went wrong.