Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 052fc91

Browse files
committed
cracking conding interview solutions
1 parent f4c37ad commit 052fc91

9 files changed

+75364
-0
lines changed

src/main/java/BinaryTree.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
/*
9+
* Given the root of a tree node, return the max height of a Binary Tree
10+
*/
11+
12+
public class BinaryTree {
13+
14+
public int solve(TreeNode node) {
15+
16+
if (node == null)
17+
return 0;
18+
19+
Queue<TreeNode> nodes = new LinkedList<>();
20+
nodes.add(node);
21+
22+
int height = 0;
23+
24+
while (true) {
25+
int c = nodes.size();
26+
if (c == 0)
27+
return height;
28+
height++;
29+
30+
while (c > 0) {
31+
TreeNode next = nodes.peek();
32+
nodes.remove();
33+
34+
if (next.left != null)
35+
nodes.add(next.left);
36+
if (next.right != null)
37+
nodes.add(next.right);
38+
39+
c--;
40+
}
41+
}
42+
}
43+
44+
public int solveRecursive(TreeNode node) {
45+
if (node == null)
46+
return 0;
47+
else {
48+
/* compute the depth of each subtree */
49+
int lDepth = solveRecursive(node.left);
50+
int rDepth = solveRecursive(node.right);
51+
52+
/* use the larger one */
53+
if (lDepth > rDepth)
54+
return (lDepth + 1);
55+
else
56+
return (rDepth + 1);
57+
}
58+
}
59+
}

src/main/java/BinaryTree2.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
/*
6+
* Given the root node of a binary tree, can you determine if it's also a binary
7+
* search tree? Constraints : 0 <= data <= 1000
8+
*/
9+
10+
public class BinaryTree2 {
11+
12+
boolean solve(TreeNode root) {
13+
return solve(root, -1, 10001);
14+
}
15+
16+
private boolean solve(TreeNode root, int min, int max) {
17+
if (root == null)
18+
return true;
19+
if (root.data <= min || root.data >= max)
20+
return false;
21+
return (solve(root.left, min, root.data) && solve(root.right, root.data, max));
22+
23+
}
24+
}

src/main/java/Contacts.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
import java.util.TreeMap;
6+
7+
/**
8+
* @author medany
9+
*/
10+
11+
public class Contacts {
12+
13+
public Integer[] solve(String[] lines, int f) {
14+
ContactsManager contacts = new ContactsManager();
15+
String operation, contact, pattern;
16+
Integer[] result = new Integer[f];
17+
int r = 0;
18+
for (String line : lines) {
19+
20+
operation = line.split(" ")[0];
21+
22+
if (operation.equals("add")) {
23+
contact = line.split(" ")[1];
24+
contacts.add(contact);
25+
} else if (operation.equals("find")) {
26+
pattern = line.split(" ")[1];
27+
result[r] = contacts.find(pattern);
28+
r++;
29+
}
30+
}
31+
return result;
32+
}
33+
}
34+
35+
class ContactsManager {
36+
private Map<Character, Map<Character, List<String>>> book;
37+
38+
public ContactsManager() {
39+
this.book = new TreeMap<>();
40+
}
41+
42+
public void add(String name) {
43+
if (book.containsKey(name.charAt(0))) {
44+
if (book.get(name.charAt(0)).containsKey(name.charAt(1))) {
45+
book.get(name.charAt(0)).get(name.charAt(1)).add(name);
46+
} else {
47+
book.get(name.charAt(0)).put(name.charAt(1), new ArrayList<String>());
48+
book.get(name.charAt(0)).get(name.charAt(1)).add(name);
49+
}
50+
} else {
51+
book.put(name.charAt(0), new HashMap<Character, List<String>>());
52+
if (name.length() > 1) {
53+
book.get(name.charAt(0)).put(name.charAt(1), new ArrayList<String>());
54+
book.get(name.charAt(0)).get(name.charAt(1)).add(name);
55+
} else {
56+
book.get(name.charAt(0)).put(name.charAt(0), new ArrayList<String>());
57+
book.get(name.charAt(0)).get(name.charAt(0)).add(name);
58+
}
59+
}
60+
}
61+
62+
public int find(String pattern) {
63+
int count = 0;
64+
if (pattern.length() < 2) {
65+
if (book.containsKey(pattern.charAt(0))) {
66+
for (List<String> list : book.get(pattern.charAt(0)).values())
67+
count += list.size();
68+
}
69+
} else {
70+
if (book.containsKey(pattern.charAt(0))) {
71+
if (book.get(pattern.charAt(0)).containsKey(pattern.charAt(1)))
72+
for (String contact : book.get(pattern.charAt(0)).get(pattern.charAt(1))) {
73+
if (contact.startsWith(pattern)) {
74+
count++;
75+
}
76+
}
77+
}
78+
}
79+
return count;
80+
81+
}
82+
}

src/main/java/TreeNode.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class TreeNode {
7+
int data;
8+
TreeNode left, right;
9+
10+
TreeNode(int data) {
11+
this.data = data;
12+
left = right = null;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return data + ", ";
18+
}
19+
}

test/main/java/BinaryTree2Test.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class BinaryTree2Test {
9+
10+
private BinaryTree2 alg = new BinaryTree2();
11+
private boolean actual, expected;
12+
13+
@Test
14+
public void Test_Recursive_1() {
15+
TreeNode root = new TreeNode(4);
16+
root.left = new TreeNode(2);
17+
root.right = new TreeNode(6);
18+
root.left.left = new TreeNode(1);
19+
root.left.right = new TreeNode(3);
20+
root.right.left = new TreeNode(5);
21+
root.right.right = new TreeNode(7);
22+
23+
actual = alg.solve(root);
24+
expected = true;
25+
26+
Assert.assertEquals(expected, actual);
27+
}
28+
29+
@Test
30+
public void Test_Recursive_2() { // 1 2 4 3 5 6 7
31+
TreeNode root = new TreeNode(3);
32+
root.left = new TreeNode(2);
33+
root.right = new TreeNode(6);
34+
root.left.left = new TreeNode(1);
35+
root.left.right = new TreeNode(4);
36+
root.right.left = new TreeNode(5);
37+
root.right.right = new TreeNode(7);
38+
39+
actual = alg.solve(root);
40+
expected = false;
41+
42+
Assert.assertEquals(expected, actual);
43+
}
44+
}

test/main/java/BinaryTreeTest.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class BinaryTreeTest {
9+
10+
private BinaryTree alg = new BinaryTree();
11+
private int actual, expected;
12+
13+
@Test
14+
public void Test_Iterative() {
15+
TreeNode root = new TreeNode(1);
16+
root.left = new TreeNode(2);
17+
root.right = new TreeNode(3);
18+
root.left.left = new TreeNode(4);
19+
root.left.right = new TreeNode(5);
20+
21+
actual = alg.solve(root);
22+
expected = 3;
23+
24+
Assert.assertEquals(expected, actual);
25+
}
26+
27+
@Test
28+
public void Test_Recursive() {
29+
TreeNode root = new TreeNode(1);
30+
root.left = new TreeNode(2);
31+
root.right = new TreeNode(3);
32+
root.left.left = new TreeNode(4);
33+
root.left.right = new TreeNode(5);
34+
35+
actual = alg.solve(root);
36+
expected = 3;
37+
38+
Assert.assertEquals(expected, actual);
39+
}
40+
41+
}

test/main/java/ContactsTest.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import java.io.BufferedReader;
2+
import java.io.File;
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
/**
10+
* @author medany
11+
*/
12+
13+
public class ContactsTest {
14+
15+
private Contacts alg = new Contacts();
16+
Integer[] actual = null, expected = null;
17+
18+
@Test
19+
public void Test_1() {
20+
21+
actual = alg.solve(new String[] { "add hack", "add hackerrank", "find hac", "find hak" }, 2);
22+
expected = new Integer[] { 2, 0 };
23+
24+
Assert.assertArrayEquals(expected, actual);
25+
26+
}
27+
28+
@Test
29+
public void Test_2() {
30+
31+
BufferedReader ir = null, or = null;
32+
FileReader i = null, o = null;
33+
34+
try {
35+
i = new FileReader(new File(this.getClass().getClassLoader().getResource("contacts_input").getPath()));
36+
o = new FileReader(new File(this.getClass().getClassLoader().getResource("contacts_output").getPath()));
37+
ir = new BufferedReader(i);
38+
or = new BufferedReader(o);
39+
int n = 0;
40+
String[] lines;
41+
String line = ir.readLine();
42+
43+
n = Integer.parseInt(line);
44+
lines = new String[n];
45+
46+
int l = 0, f = 0;
47+
while ((line = ir.readLine()) != null) {
48+
lines[l] = line;
49+
if (line.split(" ")[0].equals("find"))
50+
f++;
51+
l++;
52+
}
53+
54+
expected = new Integer[f];
55+
l = 0;
56+
while ((line = or.readLine()) != null) {
57+
expected[l] = Integer.parseInt(line);
58+
l++;
59+
}
60+
61+
actual = alg.solve(lines, f);
62+
Assert.assertArrayEquals(expected, actual);
63+
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
} finally {
67+
try {
68+
if (ir != null)
69+
ir.close();
70+
if (or != null)
71+
or.close();
72+
if (i != null)
73+
i.close();
74+
if (o != null)
75+
o.close();
76+
} catch (IOException ex) {
77+
ex.printStackTrace();
78+
}
79+
}
80+
81+
}
82+
83+
@Test
84+
public void Test_3() {
85+
86+
actual = alg.solve(new String[] { "add s", "add ss", "add sss", "add ssss", "add sssss", "find s", "find ss",
87+
"find sss", "find ssss", "find sssss", "find ssssss" }, 6);
88+
expected = new Integer[] { 5, 4, 3, 2, 1, 0 };
89+
90+
Assert.assertArrayEquals(expected, actual);
91+
92+
}
93+
94+
}

0 commit comments

Comments
 (0)