Skip to content

Commit ec30794

Browse files
Tony K. Tandawsbot
authored andcommitted
✨ Code for Chapter 1 - 10 (careercup#12)
* (attempt) 1.1 - 1.4 * (cleanup) modify readme * (cleanup) tidy up checkboxes * (attempt) 1.5 - 1.8 * (attempt) * (cleanup) delete questions * (attempt) 1.9 - stringRotation * (attempt) 2.1 - removeDups * (attempt) return Kth to last * (attempt) deleteMiddleNode * (cleanup) cleanup comments in 1.9 - 2.3 * (attempt) 2.4 * (attempt) 2.5 * (attempt) 2.6 * (attempt) 2.7 * (attempt) 2.8, 3.1, 3.2 * (cleanup) tidy up package.json and readme.md file * (attempt) 3.3 - 3.5 * (attempt) 3.6, 4.01 - 4.03 * (cleanup) tidy up package.json * (revamp) clean out previously existing files * (migrate) move over and tidy up tests in js files. clean up readme * (readme) minor edits * (readme) minor edits * (readme) minor edits * (readme) minor edits * (readme) minor edits * (package.json) amend author, licence, and add eslint * (package.json) add eslint in test script, move eslint to dev dependencies
1 parent 494db2f commit ec30794

File tree

142 files changed

+6220
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+6220
-426
lines changed

README.md

Lines changed: 129 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,142 @@
11
# Cracking the Coding Interview 6th Edition - JavaScript
22

3-
<br>
3+
## Instructions
44

5-
## Question List:
6-
7-
#### Chapter 1
8-
- [x] 1.1 - [Is Unique](lib/data-structures/chapter-1/1_1.js)
9-
- [x] 1.2 - [Check Permutation](lib/data-structures/chapter-1/1_2.js)
10-
- [x] 1.3 - [URLify](lib/data-structures/chapter-1/1_3.js)
11-
- [x] 1.4 - [Palindrome Permutation](lib/data-structures/chapter-1/1_4.js)
12-
- [x] 1.5 - [One Away](lib/data-structures/chapter-1/1_5.js)
13-
- [x] 1.6 - [String Compression](lib/data-structures/chapter-1/1_6.js)
14-
- [x] 1.7 - [Rotate Matrix](lib/data-structures/chapter-1/1_7.js)
15-
- [x] 1.8 - [Zero Matrix](lib/data-structures/chapter-1/1_8.js)
16-
- [ ] 1.9 - String Rotation
17-
18-
#### Chapter 2
19-
- [x] 2.1 - [Remove Duplicate](lib/data-structures/chapter-2/2_1.js)
20-
- [x] 2.2 - [Return Kth to Last](lib/data-structures/chapter-2/2_2.js)
21-
- [x] 2.3 - [Delete Middle Node](lib/data-structures/chapter-2/2_3.js)
22-
- [x] 2.8 - [Detection Loop](lib/data-structures/chapter-2/2_8.js)
23-
24-
25-
#### Chapter 3
26-
- [ ] 3.1 - Three in One
27-
- [ ] 3.2 - Stack Min
28-
- [ ] 3.3 - Stack of Plates
29-
- [ ] 3.4 - Queue via Stacks
30-
- [x] 3.5 - [Sort Stack](lib/data-structures/chapter-3/3_5.js)
31-
- [ ] 3.6 - Animal Shelter
5+
- This repository contains JavaScript attempts on questions listed in the book.
326

33-
<br>
7+
- So far, code or short writeups are available for the first 10 chapters.
348

35-
## Install dependencies:
36-
```bash
37-
npm install
38-
```
9+
- At Gayle's request, the files only contain code, and do not contain the actual questions. For the questions, please support Gayle by purchasing the [book](https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850).
10+
11+
- Under each JavaScript file, there are tests to check that the code is working as expected. For convenience, simple logging is used to test the functions. To run the tests, just cd into the folder containing the .js file and run `node <fileName>.js` to run the tests.
12+
13+
- Contributions welcome
14+
15+
## List of Coding Questions:
16+
17+
#### Chapter 1
18+
- [x] 1.1 - Is Unique
19+
- [x] 1.2 - Check Permutation
20+
- [x] 1.3 - URLify
21+
- [x] 1.4 - Palindrome Permutation
22+
- [x] 1.5 - One Away
23+
- [x] 1.6 - String Compression
24+
- [x] 1.7 - Rotate Matrix
25+
- [x] 1.8 - Zero Matrix
26+
- [x] 1.9 - String Rotation
27+
28+
#### Chapter 2
29+
- [x] 2.1 - Remove Dups
30+
- [x] 2.2 - Return Kth to Last
31+
- [x] 2.3 - Delete Middle Node
32+
- [x] 2.4 - Partition
33+
- [x] 2.5 - Sum Lists
34+
- [x] 2.6 - Palindrome
35+
- [x] 2.7 - Intersection
36+
- [x] 2.8 - Loop Detection
37+
38+
#### Chapter 3
39+
- [x] 3.1 - Three in One
40+
- [x] 3.2 - Stack Min
41+
- [x] 3.3 - Stack of Plates
42+
- [x] 3.4 - Queue via Stacks
43+
- [x] 3.5 - Sort Stack
44+
- [x] 3.6 - Animal Shelter
45+
46+
#### Chapter 4
47+
- [x] 4.1 - Route Between Nodes
48+
- [x] 4.2 - Minimal Tree
49+
- [x] 4.3 - List of Depths
50+
- [x] 4.4 - Check Balanced
51+
- [x] 4.5 - Validate BST
52+
- [x] 4.6 - Successor
53+
- [x] 4.7 - Build Order
54+
- [x] 4.8 - First Common Ancestor
55+
- [x] 4.9 - BST Sequences
56+
- [x] 4.10 - Check Subtree
57+
- [x] 4.11 - Random Node
58+
- [x] 4.12 - Paths with Sum
59+
60+
#### Chapter 5
61+
- [x] 5.1 - Insertion
62+
- [x] 5.2 - Binary to String
63+
- [x] 5.3 - Flip Bit To Win
64+
- [x] 5.4 - Next Number
65+
- [x] 5.5 - Debugger
66+
- [x] 5.6 - Conversion
67+
- [x] 5.7 - Pairwise Swap
68+
- [x] 5.8 - Draw Line
69+
70+
#### Chapter 6
71+
- [x] 6.1 - Heavy Pill
72+
- [x] 6.2 - Basketball
73+
- [x] 6.3 - Dominos
74+
- [x] 6.4 - Ants on a Triangle
75+
- [x] 6.5 - Jugs of Water
76+
- [x] 6.6 - Blue-Eyed Island
77+
- [x] 6.7 - The Apocalypse
78+
- [x] 6.8 - The Egg Drop Problem
79+
- [x] 6.9 - 100 Lockers
80+
- [x] 6.10 - Poison
81+
82+
#### Chapter 7
83+
- [x] 7.1 - Deck of Cards
84+
- [x] 7.2 - Call Center
85+
- [x] 7.3 - Jukebox
86+
- [x] 7.4 - Parking Lot
87+
- [x] 7.5 - Online Book Reader
88+
- [x] 7.6 - Jigsaw
89+
- [x] 7.7 - Chat Server
90+
- [x] 7.8 - Othello
91+
- [x] 7.9 - Circular Array
92+
- [x] 7.10 - Minesweeper
93+
- [x] 7.11 - File System
94+
- [x] 7.12 - Hash Table
95+
96+
#### Chapter 8
97+
- [x] 8.1 - Triple Step
98+
- [x] 8.2 - Robot in a Grid
99+
- [x] 8.3 - Magic Index
100+
- [x] 8.4 - Power Set
101+
- [x] 8.5 - Recursive Multiply
102+
- [x] 8.6 - Towers of Hanoi
103+
- [x] 8.7 - Permutations without Dups
104+
- [x] 8.8 - Permutations with Dups
105+
- [x] 8.9 - Parens
106+
- [x] 8.10 - Paint Fill
107+
- [x] 8.11 - Coins
108+
- [x] 8.12 - Eight Queens
109+
- [x] 8.13 - Stack Boxes
110+
- [x] 8.14 - Boolean Eval
111+
112+
#### Chapter 9
113+
- [x] 9.1 - Stock Data
114+
- [x] 9.2 - Social Network
115+
- [x] 9.3 - Web Crawler
116+
- [x] 9.4 - Duplicate URLs
117+
- [x] 9.5 - Cache
118+
- [x] 9.6 - Sales Rank
119+
- [x] 9.7 - Personal Financial Manager
120+
- [x] 9.8 - Paste Bin
121+
122+
#### Chapter 10
123+
- [x] 10.1 - Sorted Merge
124+
- [x] 10.2 - Group Anagrams
125+
- [x] 10.3 - Search In Rotated Array
126+
- [x] 10.4 - Sorted Search No Size
127+
- [x] 10.5 - Sparse Search
128+
- [x] 10.6 - Sort Big File
129+
- [x] 10.7 - Missing Int
130+
- [x] 10.8 - Find Duplicates
131+
- [x] 10.9 - Sorted Matrix Search
132+
- [x] 10.10 - Rank From Stream
133+
- [x] 10.11 - Peaks And Valleys
39134

40135
<br>
41136

42137
## Test
43138

44-
```bash
139+
```sh
45140
npm test
46141
```
47142

48-
If you just want to lint
49-
```bash
50-
npm run lint
51-
```
52-
53-
<br>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var allUniqueChars = function(string) {
2+
3+
// O(n^2) approach, no additional data structures used
4+
// for each character, check remaining characters for duplicates
5+
for (var i = 0; i < string.length; i++) {
6+
for (var j = i + 1; j < string.length; j++) {
7+
if (string[i] === string[j]) {
8+
return false; // if match, return false
9+
}
10+
}
11+
}
12+
return true; // if no match, return true
13+
};
14+
15+
/* TESTS */
16+
// log some tests here
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var checkPermute = function(stringOne, stringTwo) {
2+
// if different lengths, return false
3+
if (stringOne.length !== stringTwo.length) {
4+
return false;
5+
// else sort and compare
6+
// (doesnt matter how it's sorted, as long as it's sorted the same way)
7+
} else {
8+
var sortedStringOne = stringOne.split('').sort().join('');
9+
var sortedStringTwo = stringTwo.split('').sort().join('');
10+
return sortedStringOne === sortedStringTwo;
11+
}
12+
};
13+
14+
// Tests
15+
console.log(checkPermute('aba', 'aab'), true);
16+
17+
console.log(checkPermute('aba', 'aaba'), false);
18+
19+
console.log(checkPermute('aba', 'aa'), false);

chapter01/1.3 - URLify/urlify.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var urlify = function(str, length) {
2+
// have a pointer to check from start to end
3+
var strArr = str.split('');
4+
var pointer = 0;
5+
while (pointer < str.length) {
6+
if (strArr[pointer] === ' ') {
7+
// *** needs more work here, a little wierd
8+
// not handling trailing spaces properly
9+
for (var i = str.length - 1; i > pointer + 3; i--) {
10+
strArr[i] = str[i - 2];
11+
}
12+
strArr[pointer] = '%';
13+
strArr[pointer+1] = '2';
14+
strArr[pointer+2] = '0';
15+
console.log(strArr, strArr.length);
16+
}
17+
pointer++;
18+
}
19+
// if character is a space, move remainder chars by two
20+
// replace following three chars with '%20'
21+
return strArr.join('');
22+
};
23+
24+
console.log(urlify('Mr John Smith ', 13), 'Mr%20John%20Smith');
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var palinPerm = function(string) {
2+
// create object literal to store charcount
3+
var chars = {};
4+
var currChar;
5+
var mulligan = false;
6+
var isPerm = true;
7+
// pump characters in, spaces not counted, all lowercase
8+
string.split('').forEach((char) => {
9+
if (char !== ' ') {
10+
currChar = char.toLowerCase();
11+
if (chars[currChar] === undefined) {
12+
chars[currChar] = 0;
13+
}
14+
chars[currChar]++;
15+
}
16+
});
17+
// check that all chars are even count, except for one exception
18+
Object.keys(chars).forEach((char) => {
19+
if (chars[char] % 2 > 0) {
20+
// if more than one exception, return false
21+
if (mulligan) {
22+
isPerm = false; // return in a forEach statment doesn't flow out of function scope
23+
} else {
24+
mulligan = true;
25+
}
26+
}
27+
});
28+
// if not return true
29+
return isPerm;
30+
};
31+
32+
// TESTS
33+
console.log(palinPerm('Tact Coa'), 'true');
34+
console.log(palinPerm('Tact boa'), 'false');

chapter01/1.5 - OneAway/oneAway.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var oneAway = function(string1, string2) {
2+
// insert a char for str1 -> remove a char for str2
3+
var checkOneMissing = function(first, second) {
4+
if (first.length !== second.length - 1) {
5+
return false;
6+
} else {
7+
var mulligan = false;
8+
var fP = 0; // first Pointer
9+
var sP = 0; // second Pointer
10+
while (fP < first.length) {
11+
if (first[fP] !== second[sP]) {
12+
if (mulligan) {
13+
return false;
14+
} else {
15+
mulligan = true;
16+
sP++; // second length is longer
17+
}
18+
} else {
19+
fP++;
20+
sP++;
21+
}
22+
}
23+
return true;
24+
}
25+
};
26+
27+
var checkOneDiff = function(first, second) {
28+
if (first.length !== second.length) {
29+
return false;
30+
} else {
31+
var mulligan = false;
32+
var fP = 0; // first Pointer
33+
var sP = 0; // second Pointer
34+
while (fP < first.length) {
35+
if (first[fP] !== second[sP]) {
36+
if (mulligan) {
37+
return false; // more than one mismatch
38+
} else {
39+
mulligan = true; // use up mulligan
40+
}
41+
}
42+
fP++;
43+
sP++;
44+
}
45+
return true;
46+
}
47+
};
48+
// insert a char for str1 -> remove a char for str2
49+
// check one diff
50+
51+
// console log checks
52+
// console.log(string1, string2, 'checkMiss', checkOneMissing(string1, string2));
53+
// console.log(string2, string1, 'checkMiss', checkOneMissing(string2, string1));
54+
// console.log(string1, string2, 'checkDiff', checkOneDiff(string1, string2));
55+
56+
return checkOneMissing(string1, string2) || checkOneMissing(string2, string1) || checkOneDiff(string1, string2);
57+
};
58+
59+
60+
// Test
61+
console.log(oneAway('pale', 'ple'), true);
62+
console.log(oneAway('pales', 'pale'), true);
63+
console.log(oneAway('pale', 'bale'), true);
64+
console.log(oneAway('pale', 'bake'), false);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var strComp = function(string) {
2+
var compressed = '';
3+
var currChar = '';
4+
var currCount = '';
5+
var maxCount = 1;
6+
for (var i = 0; i < string.length; i++) {
7+
if (currChar !== string[i]) {
8+
console.log(currChar, string[i], i);
9+
compressed = compressed + currChar + currCount;
10+
maxCount = Math.max(maxCount, currCount);
11+
currChar = string[i];
12+
currCount = 1;
13+
} else {
14+
currCount++;
15+
}
16+
}
17+
compressed = compressed + currChar + currCount;
18+
maxCount = Math.max(maxCount, currCount);
19+
20+
return maxCount === 1 ? string : compressed;
21+
};
22+
23+
// Test
24+
console.log('aaaaaa', strComp('aaaaaa'), 'a6');
25+
console.log('aabcccccaaa', strComp('aabcccccaaa'), 'a2b1c5a3');

0 commit comments

Comments
 (0)