-
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
20 changed files
with
530 additions
and
5 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
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,28 @@ | ||
public class Star implements Comparable<Star> { | ||
|
||
private double x, y, z; | ||
|
||
public Star(double x, double y, double z) { | ||
this.x = x; | ||
this.y = y; | ||
this.z = z; | ||
} | ||
|
||
public double distance() { | ||
return Math.sqrt(x * x + y * y + z * z); | ||
} | ||
|
||
@Override | ||
public int compareTo(Star rhs) { | ||
return Double.compare(this.distance(), rhs.distance()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Star{" + | ||
"x=" + x + | ||
", y=" + y + | ||
", z=" + z + | ||
'}'; | ||
} | ||
} |
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,9 @@ | ||
# Chapter 11: Heaps | ||
|
||
* 11.1 MergeSortedFiles | ||
* 11.2 SortIncDec | ||
* 11.3 SortAlmostSorted | ||
* 11.4 ComputeKClosest | ||
* 11.5 ComputeMedianOnline | ||
* 11.6 ComputeKLargest | ||
* 11.7 HeapStack |
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,28 @@ | ||
<?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>heaps</artifactId> | ||
<name>Chapter 11: Heaps</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,20 @@ | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class ComputeKClosest { | ||
|
||
/* | ||
11.4 | ||
How would you compute the k stars which | ||
are closest to Earth? | ||
*/ | ||
|
||
private static final Star EARTH_COORDINATES = new Star(0,0,0); | ||
|
||
public static List<Star> getKClosest(int k, Iterator<Star> stars) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
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.Collections; | ||
import java.util.List; | ||
|
||
public class ComputeKLargest { | ||
|
||
/* | ||
11.6 | ||
Given a max-heap, represented as an array A, design an | ||
algorithm that computes the k largest elements stored | ||
in the max-heap. You cannot modify the heap. | ||
*/ | ||
|
||
public static List<Integer> kLargestInBinaryHeap(final List<Integer> list, int k) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
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.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class ComputeMedianOnline { | ||
|
||
/* | ||
11.5 | ||
Design an algorithm for computing the | ||
running median of a sequence. | ||
*/ | ||
|
||
public static List<Double> onlineMedian(Iterator<Integer> sequence) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
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,22 @@ | ||
public class HeapStack { | ||
|
||
/* | ||
11.7 | ||
How would you implement a stack API using a heap? | ||
*/ | ||
|
||
public void push(Integer x) { | ||
|
||
} | ||
|
||
public Integer pop() { | ||
|
||
return 0; | ||
} | ||
|
||
public Integer peek() { | ||
|
||
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.Collections; | ||
import java.util.List; | ||
|
||
public class MergeSortedFiles { | ||
|
||
/* | ||
11.1 | ||
Write a program that takes as input a set of sorted sequences | ||
and computes the union of these sequences as a sorted sequence. | ||
*/ | ||
|
||
public static List<Integer> mergeSorted(List<List<Integer>> sortedArrays) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
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 @@ | ||
import java.util.Iterator; | ||
|
||
public class SortAlmostSorted { | ||
|
||
/* | ||
11.3 | ||
Write a program which takes as input a very long sequence | ||
of numbers and prints the numbers in sorted order. Each | ||
number is at most k away from its correctly sorted position. | ||
(Such an array is sometimes referred to as being k-sorted.) | ||
For example, no number in the sequence [3,-1,2,6,4,5,8] | ||
is more than two away from its final sorted position. | ||
*/ | ||
|
||
public static void sort(Iterator<Integer> sequence, int k) { | ||
|
||
} | ||
|
||
} |
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.Collections; | ||
import java.util.List; | ||
|
||
public class SortIncDec { | ||
|
||
/* | ||
11.2 | ||
Design an efficient algorithm for sorting a | ||
k-increasing-decreasing array. | ||
*/ | ||
|
||
public static List<Integer> sort(List<Integer> list) { | ||
|
||
return Collections.emptyList(); | ||
} | ||
} |
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,92 @@ | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeKClosestTest { | ||
|
||
private List<Star> expected; | ||
private int k; | ||
private Iterator<Star> stars; | ||
|
||
@Test | ||
public void getKClosest1() throws Exception { | ||
expected = Arrays.asList( | ||
new Star(0,0,1), | ||
new Star(0,0,2) | ||
); | ||
k = 1; | ||
stars = Arrays.asList( | ||
new Star(0,0,1) | ||
).iterator(); | ||
|
||
test(expected, k, stars); | ||
} | ||
|
||
@Test | ||
public void getKClosest2() throws Exception { | ||
expected = Arrays.asList( | ||
new Star(0,0,1), | ||
new Star(0,0,4), | ||
new Star(0,0,2), | ||
new Star(0,0,5), | ||
new Star(0,0,3) | ||
); | ||
k = 3; | ||
stars = Arrays.asList( | ||
new Star(0,0,1), | ||
new Star(0,0,2), | ||
new Star(0,0,3) | ||
).iterator(); | ||
|
||
test(expected, k, stars); | ||
} | ||
|
||
@Test | ||
public void getKClosest3() throws Exception { | ||
expected = Arrays.asList( | ||
new Star(0,0,75), | ||
new Star(0,0,23), | ||
new Star(0,0,131), | ||
new Star(0,0,99), | ||
new Star(0,0,67), | ||
new Star(0,0,70), | ||
new Star(0,0,99), | ||
new Star(0,0,3), | ||
new Star(0,0,23), | ||
new Star(0,0,100), | ||
new Star(0,0,32), | ||
new Star(0,0,43) | ||
); | ||
k = 5; | ||
stars = Arrays.asList( | ||
new Star(0,0,3), | ||
new Star(0,0,23), | ||
new Star(0,0,32), | ||
new Star(0,0,43), | ||
new Star(0,0,70) | ||
).iterator(); | ||
|
||
test(expected, k, stars); | ||
} | ||
|
||
private void test(List<Star> expected, int k, Iterator<Star> stars) { | ||
List<Star> result = ComputeKClosest.getKClosest(k, stars); | ||
try { | ||
for (Star star : expected) { | ||
if (!result.contains(star)) | ||
throw new AssertionError(); | ||
} | ||
} catch (AssertionError e) { | ||
StringBuilder errorMessage = new StringBuilder(); | ||
errorMessage.append("\nExpected: "+expected.toString()); | ||
errorMessage.append("\nActual: "+result.toString()+"\n"); | ||
fail(errorMessage.toString()); | ||
} | ||
} | ||
|
||
} |
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,46 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeKLargestTest { | ||
|
||
private List<Integer> expected; | ||
private List<Integer> list; | ||
private int k; | ||
|
||
@Test | ||
public void kLargestInBinaryHeap1() throws Exception { | ||
expected = Arrays.asList(7,8,9); | ||
list = Arrays.asList(8,3,4,2,6,5,7,9,1); | ||
k = 3; | ||
|
||
test(expected,list,k); | ||
} | ||
|
||
@Test | ||
public void kLargestInBinaryHeap2() throws Exception { | ||
expected = Arrays.asList(5,6,7,8,9); | ||
list = Arrays.asList(8,3,4,2,6,5,7,9,1); | ||
k = 5; | ||
|
||
test(expected,list,k); | ||
} | ||
|
||
private void test(List<Integer> expected, List<Integer> list, int k) { | ||
List<Integer> result = ComputeKLargest.kLargestInBinaryHeap(list, k); | ||
try { | ||
for (Integer i : expected) { | ||
if (!result.contains(i)) | ||
throw new AssertionError(); | ||
} | ||
} catch (AssertionError e) { | ||
StringBuilder errorMessage = new StringBuilder(); | ||
errorMessage.append("\nExpected: "+expected.toString()); | ||
errorMessage.append("\nActual: "+result.toString()+"\n"); | ||
fail(errorMessage.toString()); | ||
} | ||
} | ||
} |
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,26 @@ | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ComputeMedianOnlineTest { | ||
|
||
private List<Double> expected; | ||
private Iterator<Integer> input; | ||
|
||
@Test | ||
public void onlineMedian1() throws Exception { | ||
expected = Arrays.asList(1.,.5,1.,2.,2.,1.5,1.); | ||
input = Arrays.asList(1,0,3,5,2,0,1).iterator(); | ||
|
||
test(expected, input); | ||
} | ||
|
||
private void test(List<Double> expected, Iterator<Integer> input) { | ||
assertEquals(expected, ComputeMedianOnline.onlineMedian(input)); | ||
} | ||
|
||
} |
Oops, something went wrong.