Skip to content

Commit 9a38f5c

Browse files
committed
Initial release
0 parents  commit 9a38f5c

File tree

202 files changed

+17178
-0
lines changed

Some content is hidden

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

202 files changed

+17178
-0
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# EditorConfig helps developers define and maintain consistent
2+
# coding styles between different editors and IDEs
3+
# editorconfig.org
4+
5+
root = true
6+
7+
8+
[*]
9+
10+
indent_style = space
11+
indent_size = 2
12+
end_of_line = lf
13+
charset = utf-8
14+
trim_trailing_whitespace = true
15+
insert_final_newline = true
16+
17+
[*.md]
18+
trim_trailing_whitespace = false
19+
20+
[Makefile]
21+
indent_style = tab
22+
indent_size = 4

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.idea
3+
/out
4+
/core
5+
node_modules
6+
*.o

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests/cpp/gtest"]
2+
path = tests/cpp/gtest
3+
url = [email protected]:tclamb/gtest.git

.jshintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
problems/shortest-fizz-buzz

.jshintrc

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"node": true,
3+
"browser": true,
4+
"jquery": true,
5+
"devel": true,
6+
"nonstandard": true,
7+
"worker": true,
8+
"esnext": true,
9+
"bitwise": false,
10+
"boss": true,
11+
"camelcase": true,
12+
"curly": true,
13+
"eqeqeq": true,
14+
"eqnull": true,
15+
"expr": true,
16+
"forin": true,
17+
"immed": true,
18+
"latedef": true,
19+
"newcap": true,
20+
"noarg": true,
21+
"quotmark": "single",
22+
"regexp": true,
23+
"strict": false,
24+
"undef": true,
25+
"unused": true,
26+
"trailing": true,
27+
"smarttabs": true,
28+
"immed": true,
29+
"globals": {
30+
"it": true,
31+
"describe": true
32+
}
33+
}

CONTRIBUTING.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Contributing
2+
3+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
4+
5+
Indentation should be set to two spaces. If you are adding a new problem, consider adding an entry on the problems list in the readme where applicable.
6+
7+
Please lint and test your code with any means available - currently JavaScript has tests and linting via Mocha and JSHint.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013 Blake Embrey ([email protected])
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Makefile

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
SHELL := /bin/sh
2+
TEST_DIR := tests
3+
GTEST_DIR := $(TEST_DIR)/cpp/gtest
4+
5+
6+
# C++11 tests
7+
CPP_TESTS := stack-machine.cpp once.cpp stack.cpp queue.cpp spiral.cpp
8+
CPP_SOURCES := $(addprefix $(TEST_DIR)/cpp/, $(CPP_TESTS))
9+
10+
11+
# OS specific settings
12+
UNAME_S = $(shell uname -s)
13+
ifeq ($(UNAME_S),Darwin)
14+
CXXFLAGS += -std=c++11 -stdlib=libc++
15+
LDFLAGS += -lc++
16+
CXX := clang++
17+
else ifeq ($(UNAME_S),Linux)
18+
CXXFLAGS += --std=c++11 -g
19+
CXX := g++
20+
endif
21+
22+
23+
# Languages
24+
default:
25+
@echo "Usage: make <language>"
26+
@echo "Available languages: cpp, js"
27+
28+
29+
# Not recommended
30+
.IGNORE: cpp js
31+
all: .IGNORE
32+
33+
34+
# C++ build rules
35+
OTHER_SOURCES := $(TEST_DIR)/cpp/common.cpp
36+
FUSED_GTEST_SOURCES := $(GTEST_DIR)/gtest-all.cc $(GTEST_DIR)/gtest_main.cc
37+
CXXFLAGS += -DGTEST_HAS_PTHREAD=0 -DGTEST_HAS_TR1_TUPLE=0 -g
38+
CPP_OBJECTS := $(CPP_SOURCES:.cpp=.o)
39+
FUSED_GTEST_OBJECTS := $(FUSED_GTEST_SOURCES:.cc=.o)
40+
OTHER_OBJECTS := $(OTHER_SOURCES:.cpp=.o)
41+
cpp: hasGTest $(FUSED_GTEST_OBJECTS) $(CPP_OBJECTS) $(OTHER_OBJECTS)
42+
@$(CXX) $(LDFLAGS) \
43+
$(CPP_OBJECTS) $(FUSED_GTEST_OBJECTS) $(OTHER_OBJECTS)\
44+
-o cpp
45+
@./cpp
46+
@rm -rf cpp
47+
hasGTest:
48+
@if [ ! -f $(GTEST_DIR)/gtest.h ]; then \
49+
git submodule update --init -f $(GTEST_DIR); \
50+
fi
51+
.cpp.o:
52+
@$(CXX) $(CXXFLAGS) -c $< -o $@
53+
.cc.o:
54+
@$(CXX) $(CXXFLAGS) -c $< -o $@
55+
56+
57+
# JS build rules
58+
js:
59+
@npm install 2> /dev/null
60+
@npm test
61+
62+
63+
# Cleanup
64+
clean:
65+
@rm -f $(CPP_OBJECTS) $(FUSED_GTEST_OBJECTS)

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The Code Interview
2+
3+
Over the next few weeks I will try to solve some of the coding problems in this
4+
repository.
5+
6+
Currently, this is a fork of [blakeembrey/code-problems](https://github.com/blakeembrey/code-problems) but I will add more problems as I go along.

package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "code-problems",
3+
"version": null,
4+
"private": true,
5+
"description": "Repository full of code problems and common solutions.",
6+
"main": null,
7+
"directories": {
8+
"test": "tests"
9+
},
10+
"dependencies": {},
11+
"devDependencies": {
12+
"mocha": "~1.11.0",
13+
"jshint": "~2.1.3"
14+
},
15+
"scripts": {
16+
"lint": "./node_modules/.bin/jshint .",
17+
"test": "npm run lint && ./node_modules/.bin/mocha tests/javascript"
18+
},
19+
"repository": {
20+
"type": "git",
21+
"url": "git://github.com/blakeembrey/code-problems.git"
22+
},
23+
"keywords": [
24+
"code",
25+
"problems",
26+
"interview",
27+
"programming"
28+
],
29+
"author": "Blake Embrey",
30+
"license": "MIT",
31+
"readmeFilename": "README.md",
32+
"bugs": {
33+
"url": "https://github.com/blakeembrey/code-problems/issues"
34+
}
35+
}

problems/anagram-detection/Readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Anagram Detection
2+
3+
Write a function that accepts two parameters, a parent and a child string. Determine how many times the child string - or an anagram of the of the child string - appears in the parent string. There is a solution which can be done in near instant time.
4+
5+
```js
6+
f('AdnBndAndBdaBn', 'dAn') // 4 ("Adn", "ndA", "dAn", "And")
7+
f('AbrAcadAbRa', 'cAda') // 2
8+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Simple function that will take a string of latin characters and return a unique hash
2+
hashString = (str) ->
3+
# Map characters to prime numbers to multiply
4+
charMap =
5+
a: 2
6+
b: 3
7+
c: 5
8+
d: 7
9+
e: 11
10+
f: 13
11+
g: 17
12+
h: 19
13+
i: 23
14+
j: 29
15+
k: 31
16+
l: 37
17+
m: 41
18+
n: 43
19+
o: 47
20+
p: 53
21+
q: 59
22+
r: 61
23+
s: 67
24+
t: 71
25+
u: 73
26+
v: 79
27+
w: 83
28+
x: 89
29+
y: 97
30+
z: 101
31+
A: 103
32+
B: 107
33+
C: 109
34+
D: 113
35+
E: 127
36+
F: 131
37+
G: 137
38+
H: 139
39+
I: 149
40+
J: 151
41+
K: 163
42+
L: 167
43+
M: 173
44+
N: 179
45+
O: 181
46+
P: 191
47+
Q: 193
48+
R: 197
49+
S: 199
50+
T: 211
51+
U: 223
52+
V: 227
53+
W: 229
54+
X: 233
55+
Y: 239
56+
Z: 241
57+
58+
str.split("").reduce ((memo, char) -> memo * charMap[char]), 1
59+
60+
anagramDetection = (parent, child) ->
61+
length = child.length
62+
anagram = hashString(child)
63+
total = 0
64+
i = 0
65+
66+
while i < (parent.length - length)
67+
total += 1 if hashString(parent.substr(i, length)) is anagram
68+
i++
69+
total
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Simple function that will take a string of latin characters and return a unique hash
2+
var hashString = function (str) {
3+
// Map characters to prime numbers to multiply
4+
var charMap = {
5+
'a': 2,
6+
'b': 3,
7+
'c': 5,
8+
'd': 7,
9+
'e': 11,
10+
'f': 13,
11+
'g': 17,
12+
'h': 19,
13+
'i': 23,
14+
'j': 29,
15+
'k': 31,
16+
'l': 37,
17+
'm': 41,
18+
'n': 43,
19+
'o': 47,
20+
'p': 53,
21+
'q': 59,
22+
'r': 61,
23+
's': 67,
24+
't': 71,
25+
'u': 73,
26+
'v': 79,
27+
'w': 83,
28+
'x': 89,
29+
'y': 97,
30+
'z': 101,
31+
'A': 103,
32+
'B': 107,
33+
'C': 109,
34+
'D': 113,
35+
'E': 127,
36+
'F': 131,
37+
'G': 137,
38+
'H': 139,
39+
'I': 149,
40+
'J': 151,
41+
'K': 163,
42+
'L': 167,
43+
'M': 173,
44+
'N': 179,
45+
'O': 181,
46+
'P': 191,
47+
'Q': 193,
48+
'R': 197,
49+
'S': 199,
50+
'T': 211,
51+
'U': 223,
52+
'V': 227,
53+
'W': 229,
54+
'X': 233,
55+
'Y': 239,
56+
'Z': 241
57+
};
58+
59+
return str.split('').reduce(function (memo, char) {
60+
return memo * charMap[char];
61+
}, 1);
62+
};
63+
64+
module.exports = function (parent, child) {
65+
var length = child.length,
66+
anagram = hashString(child),
67+
total = 0;
68+
69+
for (var i = 0; i < (parent.length - length); i++) {
70+
if (hashString(parent.substr(i, length)) === anagram) {
71+
total += 1;
72+
}
73+
}
74+
75+
return total;
76+
};

0 commit comments

Comments
 (0)