Skip to content

Commit

Permalink
release v1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
gardncl committed Apr 29, 2017
2 parents 42d7b83 + 49a1e7d commit ce1c486
Show file tree
Hide file tree
Showing 20 changed files with 530 additions and 5 deletions.
8 changes: 7 additions & 1 deletion datastructures/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

<artifactId>datastructures</artifactId>
<name>Data Structures</name>

<dependencies>
<dependency>
<groupId>gardncl</groupId>
<artifactId>datastructures</artifactId>
<version>1.4</version>
</dependency>
</dependencies>


</project>
28 changes: 28 additions & 0 deletions datastructures/src/main/java/Star.java
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 +
'}';
}
}
9 changes: 9 additions & 0 deletions heaps/README.md
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
28 changes: 28 additions & 0 deletions heaps/pom.xml
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>
20 changes: 20 additions & 0 deletions heaps/src/main/java/ComputeKClosest.java
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();
}
}
18 changes: 18 additions & 0 deletions heaps/src/main/java/ComputeKLargest.java
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();
}
}
18 changes: 18 additions & 0 deletions heaps/src/main/java/ComputeMedianOnline.java
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();
}
}
22 changes: 22 additions & 0 deletions heaps/src/main/java/HeapStack.java
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;
}
}
17 changes: 17 additions & 0 deletions heaps/src/main/java/MergeSortedFiles.java
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();
}
}
20 changes: 20 additions & 0 deletions heaps/src/main/java/SortAlmostSorted.java
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) {

}

}
17 changes: 17 additions & 0 deletions heaps/src/main/java/SortIncDec.java
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();
}
}
92 changes: 92 additions & 0 deletions heaps/src/test/java/ComputeKClosestTest.java
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());
}
}

}
46 changes: 46 additions & 0 deletions heaps/src/test/java/ComputeKLargestTest.java
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());
}
}
}
26 changes: 26 additions & 0 deletions heaps/src/test/java/ComputeMedianOnlineTest.java
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));
}

}
Loading

0 comments on commit ce1c486

Please sign in to comment.