Skip to content

Commit bd71d25

Browse files
committed
Refactored solutions
1 parent 841655b commit bd71d25

File tree

35 files changed

+147
-114
lines changed

35 files changed

+147
-114
lines changed

Chp. 01 - Arrays and Strings/_1_2_Check_Permutations/CheckPermutations.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
package _1_2_Check_Permutations;
22

3+
// Should ask interviewer if String is ASCII or Unicode (We assume ASCII)
4+
35
// Algorithm:
46
// Count the different characters using an array (as shown below) or a HashMap.
57
// For each String, we can save the count of each character in an array, then compare these 2 arrays.
68
// However, instead of using 2 arrays, we can do it with 1, as shown below.
79

8-
// Time Complexity: O(n)
9-
// Space Complexity: O(1)
1010

1111
public class CheckPermutations {
12+
private static final int NUM_ASCII_CHARS = 256; // number of ASCII characters
1213
public static boolean isPermutation(String s1, String s2) {
1314
if (s1.length() != s2.length()) {
1415
return false;
1516
}
16-
int[] letters = new int[256]; // we assume it's an ASCII string. Should
17-
// ask interviewer if String is ASCII or
18-
// Unicode
17+
int[] letters = new int[NUM_ASCII_CHARS];
1918
for (int i = 0; i < s1.length(); i++) {
2019
char ch = s1.charAt(i);
2120
letters[ch]++;
@@ -29,3 +28,7 @@ public static boolean isPermutation(String s1, String s2) {
2928
return true;
3029
}
3130
}
31+
32+
33+
// Time Complexity: O(n)
34+
// Space Complexity: O(1)

Chp. 02 - Linked Lists/_2_2_Return_Kth_to_Last/ReturnKthToLast.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import common.Node;
44
import common.ListFunctions;
55

6-
// Algorithm: Just calculate the size of SLL, then walk "size - k" elements into list
7-
8-
// Time Complexity: O(n)
9-
// Space Complexity: O(1)
6+
// Algorithm: Just calculate the size of the SLL, then walk "size - k" elements into list
107

118
public class ReturnKthToLast {
129
public static Node kthLast(Node n, int k) {
@@ -20,3 +17,6 @@ public static Node kthLast(Node n, int k) {
2017
return n;
2118
}
2219
}
20+
21+
// Time Complexity: O(n)
22+
// Space Complexity: O(1)

Chp. 02 - Linked Lists/_2_3_Delete_Middle_Node/DeleteMiddleNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import common.Node;
44

5-
// Time Complexity: O(1) - Simply move the data
6-
75
public class DeleteMiddleNode {
86
public static boolean deleteMid(Node n) {
97
if (n == null || n.next == null) { // this algorithm only works if we are deleting an element that's not the last element.
@@ -14,3 +12,5 @@ public static boolean deleteMid(Node n) {
1412
return true;
1513
}
1614
}
15+
16+
// Time Complexity: O(1) - simply move the data

Chp. 02 - Linked Lists/_2_5_Sum_Lists/SumLists.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import common.Node;
44
import common.ListFunctions;
55

6-
// Time Complexity: O(n + m)
7-
// Space Complexity: O(n + m)
8-
96
public class SumLists {
107
/* Reverse Order: Iterative solution */
118
public static Node addReverseOrder(Node n, Node m) {
@@ -88,3 +85,8 @@ private static Pair addForwardOrderHelper(Node n, Node m) {
8885
return new Pair(head, carry);
8986
}
9087
}
88+
89+
// Time/Space Complexity for both solutions
90+
//
91+
// Time Complexity: O(n + m)
92+
// Space Complexity: O(n + m)

Chp. 03 - Stacks and Queues/_3_1_Three_in_One/ThreeInOne.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,8 @@ public void print() {
5555
}
5656
}
5757

58-
// If we want variable stack sizes, these 3 things (from book's complicated
59-
// code) are relevant
60-
// 1) If, for example, stack 1 gets too big, they shift stack 2 over (by
61-
// copying over elements 1 by 1 starting from the end of the stack)
62-
// 2) The array is allowed to wrap around on itself (so will need to use %
63-
// operator)
64-
// 3) They made a new class called "StackData" to keep track of a bunch of
65-
// information about each stack instead of just "head"
58+
// If we want variable stack sizes, these 3 things (from book's complicated code) are relevant
59+
// 1) If, for example, stack 1 gets too big, they shift stack 2 over (by copying over elements 1 by 1 starting from the end of the stack)
60+
// 2) The array is allowed to wrap around on itself (so will need to use % operator)
61+
// 3) They made a new class called "StackData" to keep track of a bunch of information about each stack instead of just "head"
6662
}

Chp. 04 - Trees and Graphs/_4_10_Check_Subtree/CheckSubtree.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44

55
// Tests if one tree is subtree of another
66
//
7-
// Solutions Runtime Preference
7+
// Solution Runtime Preference
88
// ------------------------------------------------------------------
99
// 1) Preorder Traversal See analysis at bottom Worth mentioning
1010
// 2) Recursive search See analysis at bottom Favorite
1111

1212
public class CheckSubtree {
1313
/* Solution 1
14-
* - Book says if T1's preorder traversal is substring of T2's preorder traversal, and same
15-
* is true for inorder traversals, then T2 is substring of T1
16-
* - During implementation, we can insert dummy "0" for nulls. This is necessary
14+
* - Book says if T1's preorder traversal is substring of T2's preorder traversal,
15+
* and same is true for inorder traversals, then T2 is substring of T1
16+
* - During implementation, we can insert dummy "0" for nulls. This is necessary
1717
* to distinguish the 2 trees in book with duplicate values.
1818
*/
1919

2020
/**************/
2121
/* Solution 2 */
2222
/**************/
2323
public static boolean containsTree(TreeNode t1, TreeNode t2) {
24-
if (t2 == null) { // the empty tree is always a subtree. We do this check here to avoid doing it
25-
// every time in subTree
24+
if (t2 == null) { // the empty tree is always a subtree. We do this
25+
// check here to avoid doing it every time in subTree.
2626
return true;
2727
}
2828
return subTree(t1, t2);
@@ -62,8 +62,8 @@ public static boolean matchTree(TreeNode p, TreeNode q) {
6262
//
6363
// ---------- Solution 2
6464
// Time Complexity:
65-
// - O(nm) worst case, but average case is much better since matchTree() usually returns false immediately,
66-
// so it's O(n + km) where k is number of occurrences of T2's root in T1
65+
// - O(nm) worst case, but average case is much better since matchTree() usually returns
66+
// false immediately, so it's O(n + km) where k is number of occurrences of T2's root in T1
6767
// - Also, even when we do call matchTree, it is likely to not match very soon in its search
6868
//
6969
// Space Complexity: O(log(n) + log(m)). Which is great.

Chp. 05 - Bit Manipulation/_5_3_Flip_Bit_to_Win/FlipBitToWin.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package _5_3_Flip_Bit_to_Win;
22

3-
// Time Complexity: O(b) where b is number of bits in int
4-
// Space Complexity: O(1)
5-
63
public class FlipBitToWin {
74
public static int flipBit(int n) {
85
int numBits = Integer.BYTES * 8;
@@ -34,7 +31,10 @@ public static int flipBit(int n) {
3431
return maxLength;
3532
}
3633

37-
public static boolean getBit(int num, int bit) {
34+
private static boolean getBit(int num, int bit) {
3835
return (num & (1 << bit)) != 0;
3936
}
4037
}
38+
39+
// Time Complexity: O(b) where b is number of bits in int.
40+
// Space Complexity: O(1)
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
9.4 - Duplicate URLs
22

3-
- Simple Answer: Use HashMap to detect duplicates
4-
- 10 billion URLS at 100 characters each at 4 bytes each = 4 terabytes of information. Can't save this all in 1 file.
5-
- Create 4000 1GB files called <x>.txt where <x> is hash(url) % 4000.
6-
- (Although mostly uniformly distributed, some files may be bigger or smaller
7-
than 1GB since it's very unlikely we can create a perfect hash function)
8-
- Now all URLs with same hash value are in same file
9-
- (this ensures that 2 of the same key are not in different files, so our next step will successfully remove all duplicates)
10-
- Do a 2nd pass (through each of the 4000 files) and create a HashMap<URL, Boolean> to detect duplicates
11-
- This 2nd pass can be done in parallel on 4000 Machines (Pro: Faster, Con: if 1 machine crashes it affects our final result)
3+
- Simple Answer: Use HashMap to detect duplicates
4+
- 10 billion URLS at 100 characters each at 4 bytes each = 4 terabytes of information. Can't save this all in 1 file.
5+
- Create 4000 1GB files called <x>.txt where <x> is hash(url) % 4000.
6+
- (Although mostly uniformly distributed, some files may be bigger or smaller
7+
than 1GB since it's very unlikely we can create a perfect hash function)
8+
- Now all URLs with same hash value are in same file
9+
- (this ensures that 2 of the same key are not in different files, so our next step will successfully remove all duplicates)
10+
- Do a 2nd pass (through each of the 4000 files) and create a HashMap<URL, Boolean> to detect duplicates
11+
- This 2nd pass can be done in parallel on 4000 Machines (Pro: Faster, Con: if 1 machine crashes it affects our final result)
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
9.5 - Cache
22

3-
- We basically design an LRU cache
4-
- Main idea: Use linked list to keep track of popular pages. Use a HashMap<String, Node> (in parallel w/ the LinkedList)
5-
for fast access to LinkedList nodes (key = the url string, value = node in LinkedList)
6-
- "A linked list would allow easy purging of old data, by moving "fresh" items to the front. We could implement it to remove the
7-
last element of the linked list when the list exceeds a certain size."
8-
- Options for storing the cache
9-
0) Each machine has it's own cache of just it's searches (lame)
10-
1) Each machine has it's own cache of ALL machine's searches
11-
- Pro: Efficient Lookup
12-
- Con: takes up a ton of memory. Updating cache means updating it on every machine)
13-
2) Cache is shared across machines (by hashing keys, which are queries)
14-
3) Rodney method: Each machine has most popular searches cached. Less popular searches are shared among machines
3+
- We basically design an LRU cache
4+
- Main idea: Use linked list to keep track of popular pages. Use a HashMap<String, Node> (in parallel w/ the LinkedList)
5+
for fast access to LinkedList nodes (key = the url string, value = node in LinkedList)
6+
- "A linked list would allow easy purging of old data, by moving "fresh" items to the front. We could implement it to remove the
7+
last element of the linked list when the list exceeds a certain size."
8+
- Options for storing the cache
9+
0) Each machine has it's own cache of just it's searches (lame)
10+
1) Each machine has it's own cache of ALL machine's searches
11+
- Pro: Efficient Lookup
12+
- Con: takes up a ton of memory. Updating cache means updating it on every machine)
13+
2) Cache is shared across machines (by hashing keys, which are queries)
14+
3) Rodney method: Each machine has most popular searches cached. Less popular searches are shared among machines
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4 ways to divide data (Read book's explanation for great tips)
2+
- by order of appearance (Benefit: won't ever need more machines than necessary)
3+
- by hashing it mod number of machines (Benefit: Every machine knows exactly where data is)
4+
- by using what the actual values represent. Can group similar things together, like all people in Mexico.
5+
- arbitrarily (Benefit: better load balancing)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- preprocess the data with a HashMap<String, ArrayList<Document>>
2+
- Somehow divide the HashMap across machines. Can do it alphabetically by keywords.

Chp. 09 - System Design and Scalability/_Intro_Notes

Lines changed: 0 additions & 11 deletions
This file was deleted.

Chp. 10 - Sorting and Searching/_10_01_Sorted_Merge/SortedMerge.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Same concept as "merge" from MergeSort.
44
// Additional Trick: copy to end of array since that's where our empty buffer is.
5+
56
public class SortedMerge {
67
public static void merge(int[] a, int[] b, int lastA, int lastB) { // lastA is index of last element in array. Same with lastB
78
int curr = lastA + lastB + 1;
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
Read book's short answer.
1+
From "Cracking the Coding Interview" book solutions:
2+
3+
Processes and threads are related to each other but are fundamentally different.
4+
5+
A process can be thought of as an instance of a program in execution. A process is an independent entity to
6+
which system resources (e.g., CPU time and memory) are allocated. Each process is executed in a separate
7+
address space, and one process cannot access the variables and data structures of another process. If a
8+
process wishes to access another process' resources, inter-process communications have to be used. These
9+
include pipes, files, sockets, and other forms.
10+
11+
A thread exists within a process and shares the process' resources (including its heap space). Multiple
12+
threads within the same process will share the same heap space. This is very different from processes, which
13+
cannot directly access the memory of another process. Each thread still has its own registers and its own
14+
stack, but other threads can read and write the heap memory.
15+
A thread is a particular execution path of a process. When one thread modifies a process resource, the
16+
change is immediately visible to sibling threads.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Implement a directed graph and use DFS/BFS to detect a cycle.
22

3-
A deadlock can occur if and only if we find a cycle.
3+
A deadlock can occur if and only if we find a cycle.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
From "Cracking the Coding Interview" book solutions:
2+
3+
By applying the word synchronized to a method, we ensure that two threads cannot execute
4+
synchronized methods on the same object instance at the same time.
5+
6+
So, the answer to the first part really depends. If the two threads have the same instance of the object, then
7+
no, they cannot simultaneously execute method A. However, if they have different instances of the object,
8+
then they can.
9+
10+
Conceptually, you can see this by considering locks. A synchronized method applies a "lock" on all
11+
synchronized methods in that instance of the object. This blocks other threads from executing synchronized
12+
methods within that instance.
13+
14+
In the second part, we're asked if threadl can execute synchronized method A while thread2 is
15+
executing non-synchronized method B. Since B is not synchronized, there is nothing to block threadl
16+
from executing A while thread2 is executing B. This is true regardless of whether threadl and thread2
17+
have the same instance of the object.
18+
19+
Ultimately, the key concept to remember is that only one synchronized method can be in execution per
20+
instance of that object. Other threads can execute non-synchronized methods on that instance, or they can
21+
execute any method on a different instance of the object.

Chp. 15 - Threads and Locks/_15_6_Synchronized_Methods/ThreadVsProcess

Lines changed: 0 additions & 1 deletion
This file was deleted.

Chp. 16 - More Problems (Moderate)/_16_24_Pairs_with_Sum/PairsWithSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.HashMap;
44
import java.util.HashSet;
55

6-
// Good question to ask. Given {1,5,1,5}, what does our interview want our output be?
6+
// Good question to ask. Given { 1, 5, 1, 5 }, what do you want our output be?
77
// 1) {1,5}
88
// 2) {1,5}, {5,1}
99
// 3) {1,5}, {1,5}, {5,1}, {5,1}

Chp. 16 - More Problems (Moderate)/_16_25_LRU_Cache/LRUCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedList;
44
import java.util.HashMap;
55

6-
// Rodney: I have to redo this problem to decrease runtime from O(n) to O(1) (like I did in an interview - code is saved)
6+
// I have to redo this problem to decrease runtime from O(n) to O(1) (like I did in an interview - code is saved)
77

88
// To read: http://www.geeksforgeeks.org/implement-lru-cache/
99

Chp. 17 - More Problems (Hard)/_17_01_Add_Without_Plus/AddWithoutPlus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static int add(int a, int b) {
3535
public static int add_version2(int a, int b) {
3636
int anded = a & b;
3737
int xored = a ^ b;
38-
int ored = a | b;
38+
int ored = a | b;
3939

4040
int result = 0;
4141
boolean carry = false;

Chp. 17 - More Problems (Hard)/_17_03_Random_Set/RandomSet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
an array of size n?
77
88
We can first pull a random set of size m from the first n - 1 elements. Then, we just need
9-
to decide if array [n] should be inserted into our subset (which would require pulling
9+
to decide if array[n] should be inserted into our subset (which would require pulling
1010
out a random element from it). An easy way to do this is to pick a random number k
11-
from 0 through n. If k < m, then insert array [n] into subset [k].This will both "fairly"
11+
from 0 through n. If k < m, then insert array[n] into subset [k].This will both "fairly"
1212
(i.e., with proportional probability) insert array[n] into the subset and "fairly"remove
1313
a random element from the subset.
1414

Chp. 17 - More Problems (Hard)/_17_05_Letters_and_Numbers/LettersAndNumbers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import java.util.HashMap;
55

66
// Algorithm:
7-
// - Count the "running" differences (between letters and numbers) at each spot in array. Save in int[]
8-
// - If 2 indices in our int[] have the same difference, that means there's an equal subarray between them
9-
// - To find the max subarray, we find the largest distance between 2 indices with equal differences
10-
11-
// Time Complexity: O(n)
7+
// 1. Count the "running" differences (between letters and numbers) at each spot in array. Save in int[]
8+
// 2. If 2 indices in our int[] have the same difference, that means there's an equal subarray between them
9+
// 3. To find the max subarray, we find the largest distance between 2 indices with equal differences
1210

1311
public class LettersAndNumbers {
1412
public static char[] maxSubarray(char[] array) {
@@ -51,3 +49,5 @@ private static Pair findLongestMatch(int[] deltas) {
5149
return maxPair;
5250
}
5351
}
52+
53+
// Time Complexity: O(n)

Chp. 17 - More Problems (Hard)/_17_07_Baby_Names/BabyNames

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ Solution 1:
1717
Solution 2 (preferred):
1818

1919
- Represent this as a Graph. Names are Nodes. Pairs of names are Edges.
20-
- Generating the desired output is simply doing a BFS/DFS on each connected
20+
- Generating the desired output is simply doing a BFS or DFS on each connected
2121
component, while summing the values at each node.

Chp. 17 - More Problems (Hard)/_17_09_Kth_Multiple/KthMultiple.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
import java.util.ArrayDeque;
44

5-
// Time Complexity: O(n)
6-
// Space Complexity: O(n)
7-
85
public class KthMultiple {
96
public static int getKthMagicNumber(int k) { // assuming "k" counts from 1...
107
if (k < 1) {
@@ -42,3 +39,6 @@ public static int getKthMagicNumber(int k) { // assuming "k" counts from 1...
4239
return val;
4340
}
4441
}
42+
43+
// Time Complexity: O(n)
44+
// Space Complexity: O(n)

Chp. 17 - More Problems (Hard)/_17_10_Majority_Element/MajorityElement.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static Integer getCandidate(int[] array) {
2525
}
2626

2727
private static boolean isMajorityValid(int[] array, int majority) {
28-
long count = Arrays.stream(array).filter(a -> (a == majority)).count(); // here for practice
28+
long count = Arrays.stream(array).filter(a -> (a == majority)).count();
2929
return count * 2 > array.length;
3030
}
3131
}

0 commit comments

Comments
 (0)