Skip to content

Commit 500f63e

Browse files
committed
Up to #15
1 parent 956c04b commit 500f63e

30 files changed

+98
-207
lines changed

previous/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Daily coding problem
2+
3+
These are my solutions to https://dailycodingproblem.com, a great resource to practice coding interviews.
4+
5+
I have copied problem definitions and examples verbatim from the one I received, but they might occasionally differ
6+
from yours when questions have been updated to improve clarity or provide additional tests.
7+
Notes added after examples are comments of mine.
8+
9+
Pull requests warmly welcomed if you spot issues with any of my solutions!

previous/run_tests.py

Whitespace-only changes.

problems/06/README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
##Problem 6
1+
## Problem 6
22

33
An XOR linked list is a more memory efficient doubly linked list.
44
Instead of each node holding next and prev fields, it holds a field named both, which is a XOR of the next node
55
and the previous node. Implement a XOR linked list; it has an add(element) which adds the element to the end,
66
and a get(index) which returns the node at index.
7+
78
Example:
89

9-
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
11-
... l.add(cnt)
12-
>>> l.get(2) == 2
10+
>>> xll = coding_problem_06()
11+
>>> for cnt in range(0, 4):
12+
... xll.add(cnt)
13+
>>> xll.get(2) == 2
1314
True
14-
15-
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.

problems/06/problem_06.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@ def coding_problem_06():
66
and a get(index) which returns the node at index.
77
Example:
88
9-
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
11-
... l.add(cnt)
12-
>>> l.get(2) == 2
9+
>>> xll = coding_problem_06()
10+
>>> for cnt in range(0, 4):
11+
... xll.add(cnt)
12+
>>> xll.get(2) == 2
1313
True
14-
15-
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.
1814
"""
1915
pass
2016

problems/06/solution_06.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ def coding_problem_06():
77
Example:
88
99
>>> l = coding_problem_06()
10-
>>> for cnt in xrange(0, 4):
10+
>>> for cnt in range(0, 4):
1111
... l.add(cnt)
1212
>>> l.get(2) == 2
1313
True
1414
1515
Note: python does not have actual pointers (id() exists but it is not an actual pointer in all implementations).
16-
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory. This has the
17-
unfortunate consequence that the travel logic needs to reside in the List class rather than the Node one.
16+
For this reason, we use a python list to simulate memory. Indexes are the addresses in memory.
1817
"""
1918
class XORLinkedListNode(object):
2019

@@ -51,7 +50,7 @@ def add(self, val):
5150

5251
def get(self, index):
5352
current_index, previous_index, current_node = self.head()
54-
for cnt in xrange(index + 1):
53+
for cnt in range(index + 1):
5554
previous_index, current_index = current_index, current_node.next_node(previous_index)
5655
current_node = self.memory[current_index]
5756
return current_node.val

problems/07/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
##Problem 7
1+
## Problem 7
22

33
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of ways it can be decoded.
4+
45
Examples:
56

67
>>> coding_problem_07('111') # possible interpretations: 'aaa', 'ka', 'ak'
78
3
9+
810
>>> coding_problem_07('2626') # 'zz', 'zbf', 'bfz', 'bfbf'
9-
4
11+
4

problems/08/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
##Problem 8
1+
## Problem 8
22

33
A unival tree (which stands for "universal value") is a tree where all nodes have the same value.
44
Given the root to a binary tree, count the number of unival subtrees.
55
Example:
66

77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
9-
6
8+
>>> coding_problem_08(btree)
9+
6

problems/08/problem_08.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def coding_problem_08(btree):
55
Example:
66
77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
8+
>>> coding_problem_08(btree)
99
6
1010
"""
1111
pass

problems/08/solution_08.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
def coding_problem_08(btree):
1+
def coding_problem_08(bt):
22
"""
33
A unival tree (which stands for "universal value") is a tree where all nodes have the same value.
44
Given the root to a binary tree, count the number of unival subtrees.
55
Example:
66
77
>>> btree = (0, (0, (0, None, None), (0, (0, None, None), (0, None, None))), (1, None, None))
8-
>>> coding_problem_08(btree)[0]
8+
>>> coding_problem_08(btree)
99
6
1010
"""
11-
val, ln, rn = btree
12-
if ln is None and rn is None: # leaf case
13-
return 1, True, val
11+
def unival_count(btree):
1412

15-
lcount, is_uni_l, lval = coding_problem_08(ln)
16-
rcount, is_uni_r, rval = coding_problem_08(rn)
13+
val, ln, rn = btree
14+
if ln is None and rn is None: # leaf case
15+
return 1, True, val
1716

18-
is_unival = is_uni_l and is_uni_r and val == lval and val == rval
19-
count = lcount + rcount + is_unival
20-
return count, is_unival, val
17+
lcount, is_uni_l, lval = unival_count(ln)
18+
rcount, is_uni_r, rval = unival_count(rn)
19+
20+
is_unival = is_uni_l and is_uni_r and val == lval and val == rval
21+
count = lcount + rcount + is_unival
22+
return count, is_unival, val
23+
24+
return unival_count(bt)[0]
2125

2226

2327
if __name__ == '__main__':

problems/09/README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
##Problem 9
1+
## Problem 9
22

33
Given a list of integers, write a function that returns the largest sum of non-adjacent numbers.
44
The "largest sum of non-adjacent numbers" is the sum of any subset of non-contiguous elements.
5-
Solution courtesy of Kye Jiang (https://github.com/Jedshady).
5+
66
Examples:
77

88
>>> coding_problem_09([2, 4, 6, 8])
99
12
10+
1011
>>> coding_problem_09([5, 1, 1, 5])
1112
10
13+
1214
>>> coding_problem_09([1, 2, 3, 4, 5, 6])
1315
12
16+
1417
>>> coding_problem_09([-8, 4, -3, 2, 3, 4])
1518
10
19+
1620
>>> coding_problem_09([2, 4, 6, 2, 5])
17-
13
21+
13

problems/10/README.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
##Problem 10
1+
## Problem 10
22

33
Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds.
4-
Example:
5-
6-
>>> coding_problem_10()
7-
Before
8-
Hello from thread
9-
After

problems/10/problem_10.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
def coding_problem_10():
22
"""
33
Implement a job scheduler which takes in a function f and an integer n, and calls f after n milliseconds.
4-
Example:
5-
6-
>>> coding_problem_10()
7-
Before
8-
Hello from thread
9-
After
104
"""
11-
pass
5+
pass

problems/10/solution_10.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ def delayed_execution(f, ms):
1616
return f()
1717

1818
def hello(name):
19-
print 'Hello {}'.format(name)
19+
print(f'Hello {name}')
2020

2121
job = Thread(target=delayed_execution, args=(lambda: hello('from thread'), 0.01))
2222
job.start()
2323

24-
print 'Before'
24+
print('Before')
2525
time.sleep(0.02)
26-
print 'After'
26+
print('After')
2727

2828

2929
if __name__ == '__main__':

problems/11/README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
##Problem 11
1+
## Problem 11
22

33
Implement an autocomplete system. That is, given a query string s and a dictionary of all possible query strings,
4-
return all strings in the dictionary that have s as a prefix. Hint: Try pre-processing the dictionary into a more
4+
return all strings in the dictionary that have s as a prefix.
5+
6+
**Hint**: Try pre-processing the dictionary into a more
57
efficient data structure to speed up queries.
8+
69
Example:
710

811
>>> words = ['able', 'abode', 'about', 'above', 'abuse', 'syzygy']
912
>>> coding_problem_11(words, 'abo')
1013
['abode', 'about', 'above']
11-
12-
Note: I have been thinking of lots of potential data structures, like hierarchical dictionaries, or hashing.
13-
Unless otherwise specified, I think space/computation trade-off is not worth doing more than I do below.
14-
The necessity of using more sophisticated solutions depends on the problem to be solved, overkill here.

problems/11/problem_11.py

-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from bisect import bisect_left as bisect
2-
import random
3-
4-
51
def coding_problem_11(strings, prefix):
62
"""
73
Implement an autocomplete system. That is, given a query string s and a dictionary of all possible query strings,
@@ -12,10 +8,6 @@ def coding_problem_11(strings, prefix):
128
>>> words = ['able', 'abode', 'about', 'above', 'abuse', 'syzygy']
139
>>> coding_problem_11(words, 'abo')
1410
['abode', 'about', 'above']
15-
16-
Note: I have been thinking of lots of potential data structures, like hierarchical dictionaries, or hashing.
17-
Unless otherwise specified, I think space/computation trade-off is not worth doing more than I do below.
18-
The necessity of using more sophisticated solutions depends on the problem to be solved, overkill here.
1911
"""
2012
pass
2113

problems/11/solution_11.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from bisect import bisect_left as bisect
2-
import random
32

43

54
def coding_problem_11(strings, prefix):
@@ -13,9 +12,9 @@ def coding_problem_11(strings, prefix):
1312
>>> coding_problem_11(words, 'abo')
1413
['abode', 'about', 'above']
1514
16-
Note: I have been thinking of lots of potential data structures, like hierarchical dictionaries, or hashing.
17-
Unless otherwise specified, I think space/computation trade-off is not worth doing more than I do below.
18-
The necessity of using more sophisticated solutions depends on the problem to be solved, overkill here.
15+
Note: the complexity of the code below is already logarithmic. Faster speed are possible with hierarchical
16+
dictionaries or hashing scheme, if we are willing to pay a space complexity trade-off. Using more sophisticated
17+
approaches must be justified over actual requirements, overkill here.
1918
"""
2019
dictionary = [s.lower() for s in sorted(strings)]
2120
next_prefix = prefix + 'a' if prefix[-1] == 'z' else prefix[:-1] + chr(ord(prefix[-1]) + 1)

problems/12/README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
##Problem 12
1+
## Problem 12
22

33
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a
44
function that returns the number of unique ways you can climb the staircase. The order of the steps matters.
5+
56
For example, if N is 4, then there are 5 unique ways:
67

7-
1, 1, 1, 1
8-
2, 1, 1
9-
1, 2, 1
10-
1, 1, 2
11-
2, 2
8+
1, 1, 1, 1
9+
2, 1, 1
10+
1, 2, 1
11+
1, 1, 2
12+
2, 2
1213

1314
What if, instead of being able to climb 1 or 2 steps at a time, you could climb any number from a set of positive
1415
integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.
16+
1517
Example:
1618

17-
>>> coding_problem_12(4, [1, 2])
19+
>>> coding_problem_12(budget=4, choices=[1, 2])
1820
5

problems/12/problem_12.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from bisect import bisect_left as bisect
2-
import random
3-
4-
51
def coding_problem_12(budget, choices):
62
"""
73
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a
@@ -18,7 +14,7 @@ def coding_problem_12(budget, choices):
1814
integers X? For example, if X = {1, 3, 5}, you could climb 1, 3, or 5 steps at a time.
1915
Example:
2016
21-
>>> coding_problem_12(4, [1, 2])
17+
>>> coding_problem_12(budget=4, choices=[1, 2])
2218
5
2319
"""
2420
pass

problems/12/solution_12.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from bisect import bisect_left as bisect
2-
import random
3-
4-
51
def coding_problem_12(budget, choices):
62
"""
73
There exists a staircase with N steps, and you can climb up either 1 or 2 steps at a time. Given N, write a

problems/13/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
##Problem 13
1+
## Problem 13
22

33
Given an integer k and a string s, find the length of the longest substring that contains at most k distinct
44
characters.
5+
56
Example:
67

78
>>> coding_problem_13('abcba', 2) # longest substring with at most 2 distinct characters is 'bcb'
89
3
10+
911
>>> coding_problem_13('edabccccdccba', 3) # 'bccccdccb'
10-
9
12+
9

problems/13/problem_13.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from bisect import bisect_left as bisect
2-
import random
3-
4-
51
def coding_problem_13(s, k):
62
"""
73
Given an integer k and a string s, find the length of the longest substring that contains at most k distinct

problems/13/solution_13.py

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from bisect import bisect_left as bisect
2-
import random
3-
4-
51
def coding_problem_13(s, k):
62
"""
73
Given an integer k and a string s, find the length of the longest substring that contains at most k distinct

problems/14/README.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
##Problem 14
1+
## Problem 14
2+
3+
The area of a circle is defined as πr². Estimate π to 3 decimal places using a Monte Carlo method.
24

3-
The area of a circle is defined as $\pi r^2$. Estimate $\pi$ to 3 decimal places using a Monte Carlo method.
45
Example:
56

67
>>> import math
@@ -9,7 +10,3 @@ Example:
910
>>> pi_approx = coding_problem_14()
1011
>>> abs(math.pi - pi_approx) < 1e-2
1112
True
12-
13-
Note: the unit test above is not testing for 3 decimal places, but only 2. Getting to 3 significant digits would
14-
require too much time each time, since convergence is exponentially slow. Also priming the random number generator
15-
to avoid random failure for unlucky distributions of samples.

0 commit comments

Comments
 (0)