Skip to content

Commit 84c87f4

Browse files
committed
chapter 13 done
2 parents a72ceff + 2760ddf commit 84c87f4

36 files changed

+1099
-40
lines changed

arrays/src/test/java/GenerateNonuniformRandomNumbersTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import org.junit.Test;
22

33
import java.util.*;
4+
import java.util.concurrent.ConcurrentHashMap;
45
import java.util.concurrent.atomic.AtomicInteger;
56
import java.util.stream.Collectors;
67
import java.util.stream.IntStream;
@@ -41,8 +42,11 @@ public void nonUniformRandomNumberGeneration3() {
4142
}
4243

4344
private void test(List<Integer> values, List<Double> probabilities) {
44-
final Map<Integer, AtomicInteger> results = values.stream().map(integer -> new AbstractMap.SimpleImmutableEntry<>(integer, new AtomicInteger(0)))
45-
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
45+
final Map<Integer, AtomicInteger> results = new ConcurrentHashMap<>(
46+
values.stream()
47+
.map(integer -> new AbstractMap.SimpleImmutableEntry<>(integer, new AtomicInteger(0)))
48+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
49+
);
4650

4751
IntStream.range(0, N)
4852
.parallel()

datastructures/src/main/java/DuplicateAndMissing.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Tuple {
2+
3+
public Integer first;
4+
public Integer second;
5+
6+
public Tuple() {
7+
}
8+
9+
public Tuple(Integer first, Integer second) {
10+
this.first = first;
11+
this.second = second;
12+
}
13+
14+
@Override
15+
public boolean equals(Object o) {
16+
if (this == o) return true;
17+
if (o == null || getClass() != o.getClass()) return false;
18+
19+
Tuple that = (Tuple) o;
20+
21+
if (first != null ? !first.equals(that.first) : that.first != null) return false;
22+
return second != null ? second.equals(that.second) : that.second == null;
23+
}
24+
25+
}

hashtables/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Chapter 13: Hash Tables
2+
3+
* 13.1 PalindromicPermutations
4+
* 13.2 IsLetterConstructable
5+
* 13.3 LRUCache
6+
* 13.4 ComputeLCA
7+
* 13.5 ComputeKMostFrequent
8+
* 13.6 NearestRepeated
9+
* 13.7 SmallestSubarray
10+
* 13.8 SmallestSequentialSubarray
11+
* 13.9 LongestSubarray
12+
* 13.10 LongestContainedInterval
13+
* 13.11 ComputeAverageTopThree
14+
* 13.12 ComputeStringDecompositions
15+
* 13.13 CollatzConjecture
16+
* 13.14 HashFunctionChess

hashtables/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>elements-of-programming-interviews</artifactId>
7+
<groupId>gardncl</groupId>
8+
<version>1.5</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>hashtables</artifactId>
13+
<name>Chapter 13: Hash Tables</name>
14+
<dependencies>
15+
<dependency>
16+
<groupId>gardncl</groupId>
17+
<artifactId>datastructures</artifactId>
18+
<version>1.4</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>gardncl</groupId>
22+
<artifactId>utils</artifactId>
23+
<version>1.4</version>
24+
</dependency>
25+
</dependencies>
26+
27+
28+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class CollatzConjecture {
2+
3+
/*
4+
13.13
5+
6+
Test the Collatz conjecture for the first n positive integers.
7+
*/
8+
9+
public static boolean testCollatzConjecture(int n) {
10+
11+
return false;
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Iterator;
2+
3+
public class ComputeAverageTopThree {
4+
5+
/*
6+
13.11
7+
8+
Write a program which takes as input a file containing test
9+
scores and returns the student who has the maximum score
10+
averaged across his or her top three tests. If the student
11+
has fewer that three test scores, ignore that student.
12+
*/
13+
14+
public static class NameScore {
15+
public String name;
16+
public Integer score;
17+
18+
public NameScore(String name, Integer score) {
19+
this.name = name;
20+
this.score = score;
21+
}
22+
}
23+
24+
public static String findStudent(Iterator<NameScore> iterator) {
25+
26+
return "";
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class ComputeKMostFrequent {
5+
6+
/*
7+
13.5
8+
9+
You are given an array of strings. Compute the
10+
k strings that appear most frequently in the array.
11+
*/
12+
13+
public static List<String> mostFrequent(List<String> list, int k) {
14+
15+
return Collections.emptyList();
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class ComputeLCA {
2+
3+
/*
4+
13.4
5+
6+
Design an algorithm for computing the LCA of two nodes
7+
in a binary tree. The algorithm's time complexity
8+
should depend only on the distance from the nodes to the LCA.
9+
*/
10+
11+
public static BinaryTreeParent<Integer> LCA(BinaryTreeParent<Integer> node0, BinaryTreeParent<Integer> node1) {
12+
13+
return new BinaryTreeParent<>(0);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Collections;
2+
import java.util.List;
3+
4+
public class ComputeStringDecompositions {
5+
6+
/*
7+
13.12
8+
9+
Write a program which takes as input a string (the "sentence")
10+
and an array of strings (the "words"), and returns the starting
11+
indices of substrings of the sentence string which are the
12+
concatenation of all the strings in the words array.
13+
*/
14+
15+
public static List<String> findAllSubstring(String s, List<String> words) {
16+
17+
return Collections.emptyList();
18+
}
19+
}

0 commit comments

Comments
 (0)