Skip to content

Commit 0593fb4

Browse files
committed
adds problem062
1 parent 1268071 commit 0593fb4

File tree

7 files changed

+234
-63
lines changed

7 files changed

+234
-63
lines changed

README.md

+64-63
Original file line numberDiff line numberDiff line change
@@ -20,66 +20,67 @@ make test # test all
2020

2121
## Problems
2222

23-
| Number | Difficulty | Asked By | Title |
24-
|-------------------|------------|-------------|--------------------------------------------------------------|
25-
| [#1](problem001) | EASY | Google | Finding 2 numbers that adds up to k |
26-
| [#2](problem002) | HARD | Uber | Array multiplication |
27-
| [#3](problem003) | MEDIUM | Google | Tree serialization |
28-
| [#4](problem004) | HARD | Stripe | Finding lowest positive number |
29-
| [#5](problem005) | MEDIUM | Jane Street | Lisp pair operations |
30-
| [#6](problem006) | HARD | Google | XOR linked list |
31-
| [#7](problem007) | MEDIUM | Facebook | |
32-
| [#8](problem008) | EASY | Google | |
33-
| [#9](problem009) | HARD | Airbnb | |
34-
| [#10](problem010) | MEDIUM | Apple | |
35-
| [#11](problem011) | MEDIUM | Twitter | |
36-
| [#12](problem012) | HARD | Amazon | |
37-
| [#13](problem013) | HARD | Amazon | |
38-
| [#14](problem014) | MEDIUM | Google | |
39-
| [#15](problem015) | MEDIUM | Facebook | |
40-
| [#16](problem016) | EASY | Twitter | |
41-
| [#17](problem017) | HARD | Google | |
42-
| [#18](problem018) | HARD | Google | |
43-
| [#19](problem019) | MEDIUM | Facebook | |
44-
| [#20](problem020) | EASY | Google | |
45-
| [#21](problem021) | EASY | Snapchat | |
46-
| [#22](problem022) | MEDIUM | Microsoft | |
47-
| [#23](problem023) | EASY | Google | |
48-
| [#24](problem024) | MEDIUM | Google | |
49-
| [#25](problem025) | HARD | Facebook | |
50-
| [#26](problem026) | MEDIUM | Google | |
51-
| [#27](problem027) | EASY | Facebook | |
52-
| [#28](problem028) | MEDIUM | Palantir | |
53-
| [#29](problem029) | EASY | Amazon | |
54-
| [#30](problem030) | MEDIUM | Facebook | |
55-
| [#31](problem031) | EASY | Google | |
56-
| [#32](problem032) | HARD | Jane Street | |
57-
| [#33](problem033) | EASY | Microsoft | |
58-
| [#34](problem034) | MEDIUM | Quora | |
59-
| [#35](problem035) | HARD | Google | Segregating R, G, and Bs in linear time |
60-
| [#36](problem036) | MEDIUM | Dropbox | Finding the second largest node in BST |
61-
| [#37](problem037) | EASY | Google | Generating the power set of a set |
62-
| [#38](problem038) | HARD | Microsoft | Arranging N queens without threatening one another |
63-
| [#39](problem039) | MEDIUM | Dropbox | Conway's Game of Life |
64-
| [#40](problem040) | HARD | Google | Finding the non-duplicated number |
65-
| [#41](problem041) | MEDIUM | Facebook | Finding full itinerary from a list of flights |
66-
| [#42](problem042) | HARD | Google | Finding a subset of S that adds up to k |
67-
| [#43](problem043) | EASY | Amazon | Implementing Stack |
68-
| [#44](problem044) | MEDIUM | Google | Counting inversions in a list of numbers |
69-
| [#45](problem045) | EASY | Two Sigma | Writing rand function |
70-
| [#46](problem046) | HARD | Amazon | Finding the longest palindromic substring |
71-
| [#47](problem047) | EASY | Facebook | Calculating maximum profit with chronological stock prices |
72-
| [#48](problem048) | MEDIUM | Google | Building a tree with given pre-order and in-order traversals |
73-
| [#49](problem049) | MEDIUM | Amazon | Finding the maximum sum of contiguous subarray |
74-
| [#50](problem050) | EASY | Microsoft | Arithmetic binary tree |
75-
| [#51](problem051) | MEDIUM | Facebook | Card shuffle |
76-
| [#52](problem052) | HARD | Google | LRU cache |
77-
| [#53](problem053) | MEDIUM | Apple | Implementing a queue with 2 stacks |
78-
| [#54](problem054) | HARD | Dropbox | Implementing a Sudoku solver |
79-
| [#55](problem055) | EASY | Microsoft | URL shortener |
80-
| [#56](problem056) | MEDIUM | Google | Map coloring |
81-
| [#57](problem057) | MEDIUM | Amazon | Splitting string into lines of limited length |
82-
| [#58](problem058) | MEDIUM | Amazon | Search in rotated sorted array |
83-
| [#59](problem059) | HARD | Google | Low bandwidth file synchronization |
84-
| [#60](problem060) | MEDIUM | Facebook | Partitioning set by subtotals |
85-
23+
| Number | Difficulty | Asked By | Title |
24+
|-------------------|------------|-------------|---------------------------------------------------------------|
25+
| [#1](problem001) | EASY | Google | Finding 2 numbers that adds up to k |
26+
| [#2](problem002) | HARD | Uber | Array multiplication |
27+
| [#3](problem003) | MEDIUM | Google | Tree serialization |
28+
| [#4](problem004) | HARD | Stripe | Finding lowest positive number |
29+
| [#5](problem005) | MEDIUM | Jane Street | Lisp pair operations |
30+
| [#6](problem006) | HARD | Google | XOR linked list |
31+
| [#7](problem007) | MEDIUM | Facebook | |
32+
| [#8](problem008) | EASY | Google | |
33+
| [#9](problem009) | HARD | Airbnb | |
34+
| [#10](problem010) | MEDIUM | Apple | |
35+
| [#11](problem011) | MEDIUM | Twitter | |
36+
| [#12](problem012) | HARD | Amazon | |
37+
| [#13](problem013) | HARD | Amazon | |
38+
| [#14](problem014) | MEDIUM | Google | |
39+
| [#15](problem015) | MEDIUM | Facebook | |
40+
| [#16](problem016) | EASY | Twitter | |
41+
| [#17](problem017) | HARD | Google | |
42+
| [#18](problem018) | HARD | Google | |
43+
| [#19](problem019) | MEDIUM | Facebook | |
44+
| [#20](problem020) | EASY | Google | |
45+
| [#21](problem021) | EASY | Snapchat | |
46+
| [#22](problem022) | MEDIUM | Microsoft | |
47+
| [#23](problem023) | EASY | Google | |
48+
| [#24](problem024) | MEDIUM | Google | |
49+
| [#25](problem025) | HARD | Facebook | |
50+
| [#26](problem026) | MEDIUM | Google | |
51+
| [#27](problem027) | EASY | Facebook | |
52+
| [#28](problem028) | MEDIUM | Palantir | |
53+
| [#29](problem029) | EASY | Amazon | |
54+
| [#30](problem030) | MEDIUM | Facebook | |
55+
| [#31](problem031) | EASY | Google | |
56+
| [#32](problem032) | HARD | Jane Street | |
57+
| [#33](problem033) | EASY | Microsoft | |
58+
| [#34](problem034) | MEDIUM | Quora | |
59+
| [#35](problem035) | HARD | Google | Segregating R, G, and Bs in linear time |
60+
| [#36](problem036) | MEDIUM | Dropbox | Finding the second largest node in BST |
61+
| [#37](problem037) | EASY | Google | Generating the power set of a set |
62+
| [#38](problem038) | HARD | Microsoft | Arranging N queens without threatening one another |
63+
| [#39](problem039) | MEDIUM | Dropbox | Conway's Game of Life |
64+
| [#40](problem040) | HARD | Google | Finding the non-duplicated number |
65+
| [#41](problem041) | MEDIUM | Facebook | Finding full itinerary from a list of flights |
66+
| [#42](problem042) | HARD | Google | Finding a subset of S that adds up to k |
67+
| [#43](problem043) | EASY | Amazon | Implementing Stack |
68+
| [#44](problem044) | MEDIUM | Google | Counting inversions in a list of numbers |
69+
| [#45](problem045) | EASY | Two Sigma | Writing rand function |
70+
| [#46](problem046) | HARD | Amazon | Finding the longest palindromic substring |
71+
| [#47](problem047) | EASY | Facebook | Calculating maximum profit with chronological stock prices |
72+
| [#48](problem048) | MEDIUM | Google | Building a tree with given pre-order and in-order traversals |
73+
| [#49](problem049) | MEDIUM | Amazon | Finding the maximum sum of contiguous subarray |
74+
| [#50](problem050) | EASY | Microsoft | Arithmetic binary tree |
75+
| [#51](problem051) | MEDIUM | Facebook | Card shuffle |
76+
| [#52](problem052) | HARD | Google | LRU cache |
77+
| [#53](problem053) | MEDIUM | Apple | Implementing a queue with 2 stacks |
78+
| [#54](problem054) | HARD | Dropbox | Implementing a Sudoku solver |
79+
| [#55](problem055) | EASY | Microsoft | URL shortener |
80+
| [#56](problem056) | MEDIUM | Google | Map coloring |
81+
| [#57](problem057) | MEDIUM | Amazon | Splitting string into lines of limited length |
82+
| [#58](problem058) | MEDIUM | Amazon | Search in rotated sorted array |
83+
| [#59](problem059) | HARD | Google | Low bandwidth file synchronization |
84+
| [#60](problem060) | MEDIUM | Facebook | Partitioning set by subtotals |
85+
| [#61](problem061) | MEDIUM | Google | Faster pow operation |
86+
| [#62](problem062) | MEDIUM | Facebook | Couting methods to move down |

problem061/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Daily Coding Problem: Problem #61
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Google.
6+
7+
Implement integer exponentiation. That is, implement the pow(x, y) function, where x and y are integers and returns x^y.
8+
9+
Do this faster than the naive method of repeated multiplication.
10+
11+
For example, pow(2, 10) should return 1024.

problem061/problem061.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package problem061
2+
3+
func PowSimple(base int, exponent int) int {
4+
result := 1
5+
for i := 0; i < exponent; i++ {
6+
result *= base
7+
}
8+
return result
9+
}
10+
11+
func PowFaster(base int, exponent int) int {
12+
result := 1
13+
modifiedExponent, modifiedBase := exponent, base
14+
for modifiedExponent > 1 {
15+
if modifiedExponent&1 == 1 {
16+
result *= modifiedBase
17+
}
18+
modifiedExponent >>= 1
19+
modifiedBase *= modifiedBase
20+
}
21+
return result * modifiedBase
22+
}

problem061/problem061_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package problem061
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type argumentSet struct {
8+
base int
9+
exponent int
10+
result int
11+
}
12+
13+
func assert(t *testing.T, actual int, expected int) bool {
14+
t.Logf("Actual: %d / Expected: %d", actual, expected)
15+
if actual != expected {
16+
return false
17+
}
18+
return true
19+
}
20+
21+
func getBenchmark(pow func(int, int) int, base int, exponent int) func(*testing.B) {
22+
return func(b *testing.B) {
23+
for i := 0; i < b.N; i++ {
24+
pow(base, exponent)
25+
}
26+
}
27+
}
28+
29+
func TestPow(t *testing.T) {
30+
testCases := []argumentSet{
31+
{
32+
base: 8,
33+
exponent: 19,
34+
},
35+
}
36+
37+
for _, testCase := range testCases {
38+
if !assert(t, PowFaster(testCase.base, testCase.exponent), PowSimple(testCase.base, testCase.exponent)) {
39+
t.FailNow()
40+
}
41+
resultFaster := testing.Benchmark(getBenchmark(PowFaster, testCase.base, testCase.exponent))
42+
resultSimple := testing.Benchmark(getBenchmark(PowSimple, testCase.base, testCase.exponent))
43+
if resultSimple.T.Milliseconds() < resultFaster.T.Milliseconds() {
44+
t.Logf("repeated multiplication took %d, shift took %d", resultSimple.T.Milliseconds(), resultFaster.T.Milliseconds())
45+
t.FailNow()
46+
}
47+
}
48+
}

problem062/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Daily Coding Problem: Problem #62
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Facebook.
6+
7+
There is an N by M matrix of zeroes. Given N and M, write a function to count the number of ways of starting at the top-left corner and getting to the bottom-right corner. You can only move right or down.
8+
9+
For example, given a 2 by 2 matrix, you should return 2, since there are two ways to get to the bottom-right:
10+
11+
```text
12+
Right, then down
13+
Down, then right
14+
```
15+
16+
Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right.

problem062/problem062.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package problem062
2+
3+
func CountWay(rowCount int, columnCount int) int {
4+
return combination(rowCount+columnCount-2, rowCount-1)
5+
}
6+
7+
func combination(n int, m int) int {
8+
dividendChannel := make(chan int, 1)
9+
divisorChannel := make(chan int, 1)
10+
go func() {
11+
a := 1
12+
for b := n; b > m; b-- {
13+
a *= b
14+
}
15+
dividendChannel <- a
16+
}()
17+
go func() {
18+
a := 1
19+
for b := m; b > 0; b-- {
20+
a *= b
21+
}
22+
divisorChannel <- a
23+
}()
24+
25+
result := 1
26+
select {
27+
case dividend := <-dividendChannel:
28+
result *= dividend
29+
}
30+
select {
31+
case divisor := <-divisorChannel:
32+
result /= divisor
33+
}
34+
return result
35+
}

problem062/problem062_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem062
2+
3+
import "testing"
4+
5+
type argumentSet struct {
6+
rowCount int
7+
columnCount int
8+
result int
9+
}
10+
11+
func assert(t *testing.T, actual int, expected int) bool {
12+
if actual != expected {
13+
t.Logf("Actual: %v / Expected: %v", actual, expected)
14+
return false
15+
}
16+
return true
17+
}
18+
19+
func TestCountWay(t *testing.T) {
20+
testCases := []argumentSet{
21+
{
22+
rowCount: 2,
23+
columnCount: 2,
24+
result: 2,
25+
},
26+
{
27+
rowCount: 5,
28+
columnCount: 5,
29+
result: 70,
30+
},
31+
}
32+
33+
for _, testCase := range testCases {
34+
if !assert(t, CountWay(testCase.rowCount, testCase.columnCount), testCase.result) {
35+
t.FailNow()
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)