Skip to content

Commit

Permalink
chapter 13 done
Browse files Browse the repository at this point in the history
  • Loading branch information
gardncl committed Apr 30, 2017
2 parents a72ceff + 2760ddf commit 84c87f4
Show file tree
Hide file tree
Showing 36 changed files with 1,099 additions and 40 deletions.
8 changes: 6 additions & 2 deletions arrays/src/test/java/GenerateNonuniformRandomNumbersTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import org.junit.Test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -41,8 +42,11 @@ public void nonUniformRandomNumberGeneration3() {
}

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

IntStream.range(0, N)
.parallel()
Expand Down
25 changes: 0 additions & 25 deletions datastructures/src/main/java/DuplicateAndMissing.java

This file was deleted.

25 changes: 25 additions & 0 deletions datastructures/src/main/java/Tuple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Tuple {

public Integer first;
public Integer second;

public Tuple() {
}

public Tuple(Integer first, Integer second) {
this.first = first;
this.second = second;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Tuple that = (Tuple) o;

if (first != null ? !first.equals(that.first) : that.first != null) return false;
return second != null ? second.equals(that.second) : that.second == null;
}

}
16 changes: 16 additions & 0 deletions hashtables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Chapter 13: Hash Tables

* 13.1 PalindromicPermutations
* 13.2 IsLetterConstructable
* 13.3 LRUCache
* 13.4 ComputeLCA
* 13.5 ComputeKMostFrequent
* 13.6 NearestRepeated
* 13.7 SmallestSubarray
* 13.8 SmallestSequentialSubarray
* 13.9 LongestSubarray
* 13.10 LongestContainedInterval
* 13.11 ComputeAverageTopThree
* 13.12 ComputeStringDecompositions
* 13.13 CollatzConjecture
* 13.14 HashFunctionChess
28 changes: 28 additions & 0 deletions hashtables/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>hashtables</artifactId>
<name>Chapter 13: Hash Tables</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>
13 changes: 13 additions & 0 deletions hashtables/src/main/java/CollatzConjecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class CollatzConjecture {

/*
13.13
Test the Collatz conjecture for the first n positive integers.
*/

public static boolean testCollatzConjecture(int n) {

return false;
}
}
28 changes: 28 additions & 0 deletions hashtables/src/main/java/ComputeAverageTopThree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Iterator;

public class ComputeAverageTopThree {

/*
13.11
Write a program which takes as input a file containing test
scores and returns the student who has the maximum score
averaged across his or her top three tests. If the student
has fewer that three test scores, ignore that student.
*/

public static class NameScore {
public String name;
public Integer score;

public NameScore(String name, Integer score) {
this.name = name;
this.score = score;
}
}

public static String findStudent(Iterator<NameScore> iterator) {

return "";
}
}
17 changes: 17 additions & 0 deletions hashtables/src/main/java/ComputeKMostFrequent.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 ComputeKMostFrequent {

/*
13.5
You are given an array of strings. Compute the
k strings that appear most frequently in the array.
*/

public static List<String> mostFrequent(List<String> list, int k) {

return Collections.emptyList();
}
}
15 changes: 15 additions & 0 deletions hashtables/src/main/java/ComputeLCA.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class ComputeLCA {

/*
13.4
Design an algorithm for computing the LCA of two nodes
in a binary tree. The algorithm's time complexity
should depend only on the distance from the nodes to the LCA.
*/

public static BinaryTreeParent<Integer> LCA(BinaryTreeParent<Integer> node0, BinaryTreeParent<Integer> node1) {

return new BinaryTreeParent<>(0);
}
}
19 changes: 19 additions & 0 deletions hashtables/src/main/java/ComputeStringDecompositions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Collections;
import java.util.List;

public class ComputeStringDecompositions {

/*
13.12
Write a program which takes as input a string (the "sentence")
and an array of strings (the "words"), and returns the starting
indices of substrings of the sentence string which are the
concatenation of all the strings in the words array.
*/

public static List<String> findAllSubstring(String s, List<String> words) {

return Collections.emptyList();
}
}
13 changes: 13 additions & 0 deletions hashtables/src/main/java/HashFunctionChess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class HashFunctionChess {

/*
13.14
Design a hash function for chess game states.
Your function should take a state and the hash
code for that state, and a move, and efficiently
compute the hash code for the updated state.
*/


}
18 changes: 18 additions & 0 deletions hashtables/src/main/java/IsLetterConstructable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class IsLetterConstructable {

/*
13.2
Write a program which takes text for an anonymous letter and text
for a magazine and determines if it is possible to write the
anonymous letter using the magazine. The anonymous letter can be
written using the magazine if for each character in the anonymous
letter, the number of times it appears in the anonymous letter is
no more tha the number of times it appears in the magazine.
*/

public static boolean isConstructable(String letterText, String magazineText) {

return false;
}
}
35 changes: 35 additions & 0 deletions hashtables/src/main/java/LRUCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class LRUCache {

/*
13.3
Create a cache for looking up prices of books identified
by their ISBN. You implement lookup, insert, and remove
methods. Use the Least Recently Used (LRU) policy for
cache eviction. If an ISBN is already present, insert
should not change the price, but it should update that
entry to be the most recently used entry. Lookup should
also update that entry to be the most recently used entry.
*/

private int capacity;

public LRUCache(int capacity) {
this.capacity = capacity;
}

public Integer lookup(Integer key) {

return 0;
}

public Integer insert(Integer key, Integer value) {

return 0;
}

public Integer remove(Integer key) {

return 0;
}
}
18 changes: 18 additions & 0 deletions hashtables/src/main/java/LongestContainedInterval.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.List;

public class LongestContainedInterval {

/*
13.10
Write a program which takes as input a set of integers represented
by an array, and returns the size of a largest subset of integers
in the array having the property that if two integers are in the
subset, then so are all integers between them.
*/

public static int longestInterval(List<Integer> list) {

return 0;
}
}
17 changes: 17 additions & 0 deletions hashtables/src/main/java/LongestSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.List;

public class LongestSubarray {

/*
13.9
Write a program that takes an array and returns the
length of a longest subarray with the property that
all its elements are distinct.
*/

public static int longestSubarray(List<Integer> list) {

return 0;
}
}
16 changes: 16 additions & 0 deletions hashtables/src/main/java/NearestRepeated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.List;

public class NearestRepeated {

/*
13.6
Write a program which takes as input an array and finds the distance
between a closest pair of equal entries.
*/

public static int findNearest(List<String> list) {

return 0;
}
}
15 changes: 15 additions & 0 deletions hashtables/src/main/java/PalindromicPermutations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class PalindromicPermutations {

/*
13.1
Write a program to test whether the letters forming a string
can be permuted to form a palindrome. For example, "edified"
can be permuted to form "deified".
*/

public static boolean canFormPalindrome(String s) {

return false;
}
}
21 changes: 21 additions & 0 deletions hashtables/src/main/java/SmallestSequentialSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.List;
import java.util.Set;

public class SmallestSequentialSubarray {

/*
13.8
Write a program that takes two arrays of strings, and
return the indices of the starting and ending index of
a shortest subarray of the first array that
'sequentially covers' in the order in which they appear
in the keywords array. You can assume all keywords are
distinct.
*/

public static Tuple findSubarray(List<String> paragraph, Set<String> keywords) {

return new Tuple(0,0);
}
}
20 changes: 20 additions & 0 deletions hashtables/src/main/java/SmallestSubarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.List;
import java.util.Set;

public class SmallestSubarray {

/*
13.7
Write a program which takes an array of strings
and a set of strings, and returns the indices of
the starting and ending index of a shortest
subarray of the given array that "covers" the set,
i.e., contains all strings in the set.
*/

public static Tuple findSubarray(List<String> paragraph, Set<String> keywords) {

return new Tuple(0,0);
}
}
Loading

0 comments on commit 84c87f4

Please sign in to comment.