Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
out
56 changes: 56 additions & 0 deletions src/techinterview/Anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package techinterview;

import java.util.*;
import java.util.ArrayList;

/**
* Anagram Interview Question:
*
* Typically posed as follows:
*
* Given a word, and a "dictionary" find all anagrams of the given word (i.e. words which use all the same letters
* as the given word but in a different order.)
*/
public class Anagram {
private Map<String, Collection<String>> anagramsByKey = new HashMap<>();

public Collection<String> findAll(String word, Collection<String> dictionary) {
preprocess(word, dictionary);

Collection<String> anagrams = anagramsByKey.get(generateKey(word));

if (anagrams == null) {
anagrams = new HashSet<>();
}

return anagrams;
}

private void preprocess(String word, Collection<String> dictionary) {
dictionary.stream().filter(entry -> !word.equals(entry)).forEach(entry -> {
String key = generateKey(entry);

if (!anagramsByKey.containsKey(key)) {
anagramsByKey.put(key, new HashSet<>());
}

anagramsByKey.get(key).add(entry);
});
}

private String generateKey(String entry) {
List<Character> characters = new ArrayList<>();

for (char c : entry.toCharArray()) {
characters.add(c);
}

Collections.sort(characters);

StringBuilder keySb = new StringBuilder();
characters.forEach(keySb::append);

return keySb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class BinarySearch {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package TechInterview;
package techinterview;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinarySearchTree {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class DoublyLinkedList<E> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class HashTable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

import java.util.Arrays;

Expand Down
22 changes: 22 additions & 0 deletions src/techinterview/Palindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package techinterview;

public class Palindrome {
public static boolean is(String word) {
if (word == null) {
throw new IllegalArgumentException("Invalid argument 'null'.");
}

int i = 0, j = word.length() - 1;

while (i <= j) {
if (word.charAt(i) != word.charAt(j)) {
return false;
}

++i;
--j;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class Queue<E> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class SingletonDesign {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class SinglyLinkedList<E> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class StackArray {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class StackLinkedList<E> {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

public class Test {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

import java.util.HashMap;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package TechInterview;
package techinterview;

import java.util.Arrays;

Expand Down
24 changes: 24 additions & 0 deletions test/src/techinterview/AnagramTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package techinterview;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class AnagramTest {

@Test
public void testFindsAnagrams() throws Exception {
Collection<String> anagrams = new Anagram().findAll("baste", Arrays.asList("abets", "baste", "betas", "beast", "beats"));
assertEquals(4, anagrams.size());
}

@Test
public void testEmptyDictionaryFindsNoAnagrams() throws Exception {
Collection<String> anagrams = new Anagram().findAll("baste", Collections.emptyList());
assertEquals(0, anagrams.size());
}
}
31 changes: 31 additions & 0 deletions test/src/techinterview/PalindromeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package techinterview;

import org.junit.Test;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PalindromeTest {

@Test
public void testIsPalindrome() throws Exception {
assertTrue(Palindrome.is("racecar"));
assertFalse(Palindrome.is("race"));
assertTrue(Palindrome.is("mom"));
}

@Test
public void testEmptyStringIsPalindrome() throws Exception {
assertTrue(Palindrome.is(""));
}

@Test
public void testSingleLetterIsPalindrome() throws Exception {
assertTrue(Palindrome.is("a"));
}

@Test(expected = IllegalArgumentException.class)
public void testNullThrowsIllegalArgumentException() throws Exception {
Palindrome.is(null);
}
}