Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 4a818c6

Browse files
committed
Somewhere here the git history has been mangled, clean up some issues until I go through and start adding tests for them all.
1 parent 42ab94a commit 4a818c6

File tree

19 files changed

+114
-104
lines changed

19 files changed

+114
-104
lines changed

Readme.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
# Code Problems
22

3-
This repo is full of code problems that I have received prior or during an interview. I'll present all my solutions here in JavaScript, but feel free to add your own solutions in another language and even optimise my solutions. If you have your own code problems you would like to contribute, I would love to see them as well.
3+
This is my repo full of code problems that I have completed prior to or during an interview. I hope that all these problems (and solutions) are useful to practice against and review. Feel free to contribute any solutions and optimisations, given they are algorithmic optimisations. Please add your own problems that you find as well, as I would love to see them.
4+
5+
## Contributing
6+
7+
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.
8+
9+
Indentation should be set to two spaces. Please lint and test your code with any means available - currently only JavaScript has tests and linting via Mocha and JSHint.
10+
11+
## Testing
12+
13+
```sh
14+
npm install # Installs `mocha` and any other dependencies needed to run
15+
npm test # Runs the testing scripts
16+
```

anagram-detection/Readme.md

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
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.
44

5-
65
```js
76
f('AdnBndAndBdaBn', 'dAn') // 4 ("Adn", "ndA", "dAn", "And")
87
f('AbrAcadAbRa', 'cAda') // 2
File renamed without changes.

integer-difference/integer-difference.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var findUniquePairs = function (n, array) {
1+
var integerDifference = function (n, array) {
22
var hash = {},
33
total = 0;
44

integer-length/integer-length.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
function IntegerLength($num){
4-
return strlen((string) $num);
3+
function IntegerLength ($num) {
4+
return strlen((string) $num);
55
}
66

7-
?>
7+
?>
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
var kthElementInArray = function (k, array) {
1+
var kthLargestElementInArray = function (k, array) {
22
// I believe we can store it in a hash to achieve an O(n) complexity
33
var hash = {},
44
count = 0;
5+
56
// Loop through each of the array items putting the values as keys in the hash
67
array.forEach(function (num) {
78
hash[num] = hash[num] + 1 || 1;
89
});
10+
911
// Loop through each of the keys in the hash and keep track of the total count
1012
for (var i in hash) {
1113
// Check if `k` is smaller or equal to the current count plus the current
1214
// hash index, but also greater than the previous count (this will mean it
1315
// is stored in this integer key)
1416
if (k <= count + hash[i] && k > count) {
1517
// Coerce the output back to a number, since that is expected
16-
return Number(i);
18+
return +i;
1719
}
1820
// Increment the total count
1921
count += hash[i];
2022
}
23+
2124
return -1;
2225
};

linked-list/linked-list.js

-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,3 @@ LinkedList.prototype.containsNode = function (value) {
4545

4646
return false;
4747
};
48-
49-
// Maker function to return instances of linked lists
50-
var makeLinkedList = function (value) {
51-
return new LinkedList(value);
52-
};

longest-compound-word/longest-compound-word.js

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ module.exports = function (list) {
88
// Inserts a word into the prefix tree structure
99
insertWord = function (word) {
1010
var index = 0,
11-
active = prefixes,
12-
char;
11+
active = prefixes,
12+
char;
1313

1414
while (char = word[index++]) {
1515
active = (active[char] = active[char] || {});
@@ -21,10 +21,10 @@ module.exports = function (list) {
2121
// Finds the longest prefix we can make using the word
2222
findPrefixes = function (word) {
2323
var prefix = '',
24-
found = [],
25-
index = 0,
26-
active = prefixes,
27-
char;
24+
found = [],
25+
index = 0,
26+
active = prefixes,
27+
char;
2828

2929
while (char = word[index++]) {
3030
if (!active[char]) { break; }
@@ -52,22 +52,22 @@ module.exports = function (list) {
5252

5353
possibleWords.forEach(function (possible) {
5454
var word = possible[0],
55-
prefixes = possible[1],
56-
found = false,
57-
findCompoundWord, loopPrefixes;
55+
prefixes = possible[1],
56+
found = false,
57+
findCompoundWord, loopPrefixes;
5858

5959
findCompoundWord = function (suffix) {
6060
// Find all future prefixes and continue search
6161
if (suffix) {
62-
return findPrefixes(suffix).forEach(function (prefix) {
63-
!found && loopPrefixes(prefix, suffix);
64-
});
62+
return findPrefixes(suffix).forEach(function (prefix) {
63+
!found && loopPrefixes(prefix, suffix);
64+
});
6565
}
6666
// If the suffix doesn't exist, it must be because we have found an
6767
// exact compound word
6868
if (word.length > longestLength) {
69-
longestWords = [];
70-
longestLength = word.length;
69+
longestWords = [];
70+
longestLength = word.length;
7171
}
7272
// If the word is equal to the length of the current longest word, push it
7373
// into the result array, then set found to be true so we can break the

longest-words/LongestWords.cs

+28-29
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
2-
char[] delimiter = new char[] { ' ' };
3-
public List<string> FindLongestWords(string sentence)
1+
char[] delimiter = new char[] { ' ' };
2+
public List<string> FindLongestWords(string sentence)
3+
{
4+
List<string> longestWords = new List<string>();
5+
int currentLongestLength = 0;
6+
string[] words = sentence.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
7+
if (words != null && words.Length > 0)
8+
{
9+
foreach (string word in words)
10+
{
11+
// Duplicate check.
12+
if (!longestWords.Contains(word.ToLower()))
13+
{
14+
// If word is longer than the current longest. We clear our word list and add only this one.
15+
if (word.Length > currentLongestLength)
416
{
5-
List<string> longestWords = new List<string>();
6-
int currentLongestLength = 0;
7-
string[] words = sentence.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
8-
if (words != null && words.Length > 0)
9-
{
10-
foreach (string word in words)
11-
{
12-
// Duplicate check.
13-
if (!longestWords.Contains(word.ToLower()))
14-
{
15-
// If word is longer than the current longest. We clear our word list and add only this one.
16-
if (word.Length > currentLongestLength)
17-
{
18-
longestWords.Clear();
19-
longestWords.Add(word.ToLower());
20-
currentLongestLength = word.Length;
21-
}
22-
// If word's length equals currentLongest, we just add it to the list.
23-
else if (word.Length == currentLongestLength)
24-
{
25-
longestWords.Add(word);
26-
}
27-
}
28-
}
29-
}
30-
return longestWords;
17+
longestWords.Clear();
18+
longestWords.Add(word.ToLower());
19+
currentLongestLength = word.Length;
3120
}
21+
// If word's length equals currentLongest, we just add it to the list.
22+
else if (word.Length == currentLongestLength)
23+
{
24+
longestWords.Add(word);
25+
}
26+
}
27+
}
28+
}
29+
return longestWords;
30+
}

median-integer-stream/median-integer-stream.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,22 @@ var medianStream = {
3131
if (!this._median) { this._median = new LinkedList(number); return; }
3232

3333
var node = this._median,
34-
prevNode;
34+
prevNode;
3535

3636
// If the number is greater than the median value, need to insert somewhere
3737
// after the current median node
3838
if (number > this._median.value) {
3939
while (node && node.value < number) {
40-
prevNode = node;
41-
node = node.next;
40+
prevNode = node;
41+
node = node.next;
4242
}
4343
(node || prevNode).append(number);
4444
// Increment the counter of right nodes
4545
++this._right;
4646
} else {
4747
while (node && node.value > number) {
48-
prevNode = node;
49-
node = node.prev;
48+
prevNode = node;
49+
node = node.prev;
5050
}
5151
(node || prevNode).prepend(number);
5252
// Increment the counter of left nodes

number-format/number-format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports.iterative = function (number) {
1414
return string.join('.');
1515
};
1616

17-
exports.regex = function (number) {
17+
exports.regexp = function (number) {
1818
var string = ('' + number).split('.');
1919

2020
string[0] = string[0].replace(/(\d)(?=(?:\d{3})+(?:\.|$))/g, '$1,');

prime-number/prime-number.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var primeNumber = function (n) {
2-
if (n < 2) { return false; }
2+
if (n === 2) { return true; }
3+
if (n < 2 || !(n&1)) { return false; }
34

4-
for (var i = 2, l = Math.floor(Math.pow(n, 0.5)); i <= l; i++) {
5+
for (var i = 3, l = Math.floor(Math.pow(n, 0.5)); i <= l; i += 2) {
56
if (n % i === 0) {
67
return false;
78
}

queue/queue.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Queue = function () {
44
this.length = 0;
55
}
66

7-
Queue.prototype.add = function (value) {
7+
Queue.prototype.enqueue = function (value) {
88
var node = {
99
value: value,
1010
next: null
@@ -26,7 +26,7 @@ Queue.prototype.add = function (value) {
2626
return this.length += 1;
2727
};
2828

29-
Queue.prototype.remove = function () {
29+
Queue.prototype.dequeue = function () {
3030
if (!this.head) { return; }
3131

3232
var node = this.head;
File renamed without changes.

stack/stack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var Stack = function () {
33
this.length = 0;
44
}
55

6-
Stack.prototype.add = function (value) {
6+
Stack.prototype.push = function (value) {
77
var node = {
88
value: value,
99
next: null
@@ -19,7 +19,7 @@ Stack.prototype.add = function (value) {
1919
return this.length += 1;
2020
};
2121

22-
Stack.prototype.remove = function () {
22+
Stack.prototype.pop = function () {
2323
// If there is no head node, return `undefined`
2424
if (!this.head) { return; }
2525

string-format/string-format.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var stringFormat = function (string /* , args */) {
2-
var args = Array.prototype.slice.call(arguments, 1);
2+
var args = Array.prototype.slice.call(arguments, 1);
33

4-
return string.replace(/\{(\d+)\}/g, function (_, arg) {
5-
return arg in args ? args[arg] : _;
6-
});
4+
return string.replace(/\{(\d+)\}/g, function (_, arg) {
5+
return arg in args ? args[arg] : _;
6+
});
77
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
public int SumOfIntegers(int[] integerArray)
2-
{
3-
int sum = 0;
4-
foreach (int item in integerArray)
5-
{
6-
sum += item;
7-
}
8-
return sum + integerArray.Length;
9-
}
1+
public int SumOfIntegers(int[] integerArray)
2+
{
3+
int sum = 0;
4+
foreach (int item in integerArray)
5+
{
6+
sum += item;
7+
}
8+
return sum + integerArray.Length;
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

3-
function SumOfIntegers($integerArray){
4-
$sum = 0;
5-
foreach($integerArray as $valor){
6-
$sum += $valor;
7-
}
8-
return $sum + count($integerArray);
3+
function SumOfIntegers ($integerArray) {
4+
$sum = 0;
5+
foreach ($integerArray as $valor) {
6+
$sum += $valor;
7+
}
8+
return $sum + count($integerArray);
99
}
1010

1111
?>

transform-word/transform-word.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ var transformWord = function (dictionary, start, end) {
88
var isOneCharDifference = function (word1, word2) {
99
// If the second word is larger than the first word, reverse the arguments and run again
1010
if (word2.length > word1.length) {
11-
return isOneCharDifference(word2, word1);
11+
return isOneCharDifference(word2, word1);
1212
}
1313

1414
// If the difference in length between the words is greater than one,
1515
// or the words are identical anyway, it's impossible
1616
if (word1 === word2 || word2.length < word1.length - 1) {
17-
return false;
17+
return false;
1818
}
1919

2020
for (var i = 0; i < word1.length; i++) {
21-
// First we check whether replacing the character with the equivelent from
22-
// the second word with make the second word
23-
if (word1.substr(0, i) + word2[i] + word1.substr(i + 1) === word2) {
24-
return true;
25-
}
26-
// Next we check if removing a single letter from the first word will
27-
// result in the second word
28-
if (word1.substr(0, i) + word1.substr(i + 1) === word2) {
29-
return true;
30-
}
21+
// First we check whether replacing the character with the equivelent from
22+
// the second word with make the second word
23+
if (word1.substr(0, i) + word2[i] + word1.substr(i + 1) === word2) {
24+
return true;
25+
}
26+
// Next we check if removing a single letter from the first word will
27+
// result in the second word
28+
if (word1.substr(0, i) + word1.substr(i + 1) === word2) {
29+
return true;
30+
}
3131
}
3232

3333
return false;
@@ -38,11 +38,11 @@ var transformWord = function (dictionary, start, end) {
3838
graph[word] = [];
3939
// Check all the other words in the graph so far and see if they are connections
4040
Object.keys(graph).forEach(function (connection) {
41-
if (isOneCharDifference(word, connection)) {
42-
graph[word].push(connection);
43-
// Push the word into the connection if it's been created
44-
graph[connection] && graph[connection].push(word);
45-
}
41+
if (isOneCharDifference(word, connection)) {
42+
graph[word].push(connection);
43+
// Push the word into the connection if it's been created
44+
graph[connection] && graph[connection].push(word);
45+
}
4646
});
4747
});
4848

0 commit comments

Comments
 (0)