Skip to content

Commit

Permalink
release v1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
gardncl committed Apr 29, 2017
2 parents ce1c486 + bd84fc4 commit a72ceff
Show file tree
Hide file tree
Showing 24 changed files with 648 additions and 0 deletions.
25 changes: 25 additions & 0 deletions datastructures/src/main/java/DuplicateAndMissing.java
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;
}

}
20 changes: 20 additions & 0 deletions datastructures/src/main/java/MinMax.java
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;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>stacksandqueues</module>
<module>binarytrees</module>
<module>heaps</module>
<module>searching</module>
</modules>
<packaging>pom</packaging>

Expand Down
12 changes: 12 additions & 0 deletions searching/README.md
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
29 changes: 29 additions & 0 deletions searching/pom.xml
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>
18 changes: 18 additions & 0 deletions searching/src/main/java/FindDuplicateAndMissing.java
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);
}
}
16 changes: 16 additions & 0 deletions searching/src/main/java/FindKthLargest.java
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;
}
}
16 changes: 16 additions & 0 deletions searching/src/main/java/FindMinAndMax.java
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);
}

}
17 changes: 17 additions & 0 deletions searching/src/main/java/FindMissingIP.java
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;
}
}
15 changes: 15 additions & 0 deletions searching/src/main/java/IntegerSquareRoot.java
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;
}
}
14 changes: 14 additions & 0 deletions searching/src/main/java/RealSquareRoot.java
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;
}
}
17 changes: 17 additions & 0 deletions searching/src/main/java/SearchSorted.java
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;
}
}
16 changes: 16 additions & 0 deletions searching/src/main/java/SearchSorted2D.java
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;
}
}
17 changes: 17 additions & 0 deletions searching/src/main/java/SearchSortedCyclic.java
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;
}
}
17 changes: 17 additions & 0 deletions searching/src/main/java/SearchSortedIndex.java
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;
}
}
47 changes: 47 additions & 0 deletions searching/src/test/java/FindDuplicateAndMissingTest.java
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));
}

}
44 changes: 44 additions & 0 deletions searching/src/test/java/FindKthLargestTest.java
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));
}
}
Loading

0 comments on commit a72ceff

Please sign in to comment.