Skip to content

Commit d999fae

Browse files
committed
problem 49,50
1 parent 5752190 commit d999fae

File tree

9 files changed

+163
-5
lines changed

9 files changed

+163
-5
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TEST_OPTS=-v -cover -timeout 10s -count 1
1+
TEST_OPTS=-cover -timeout 10s -count 1
22
FIND=find -maxdepth 1 -type d -regextype posix-extended -regex '^\.\/problem[0-9]{1,3}$$'
33
VET_OPTS=
44
FMT_OPTS=-l -s -w

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Number | Difficulty | Asked By | Title
6464
[#45](problem045) | EASY | Two Sigma | Writing rand function
6565
[#46](problem046) | HARD | Amazon | Finding the longest palindromic substring
6666
[#47](problem047) | EASY | Facebook | Calculating maximum profit with chronological stock prices
67-
[#48](problem048) | MEDIUM | Google | Build a tree with given pre-order and in-order traversals
68-
[#49](problem049) | | |
69-
[#50](problem050) | | |
67+
[#48](problem048) | MEDIUM | Google | Building a tree with given pre-order and in-order traversals
68+
[#49](problem049) | MEDIUM | Amazon | Finding the maximum sum of contiguous subarray
69+
[#50](problem050) | EASY | Microsoft | Arithmetic binary tree
7070

problem048/problem048_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestFromValues(t *testing.T) {
4040
},
4141
},
4242
}
43-
for i, _ := range testArgs {
43+
for i := range testArgs {
4444
answer := FromValues(testArgs[i], testArgs2[i])
4545
if !answer.Equal(&correctAnswer) {
4646
t.Log(fmt.Sprintf("failed at %v, %v", testArgs[i], testArgs2[i]))

problem049/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Daily Coding Problem: Problem #49
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Amazon.
6+
7+
Given an array of numbers, find the maximum sum of any contiguous subarray of the array.
8+
9+
For example, given the array `[34, -50, 42, 14, -5, 86]`, the maximum sum would be 137, since we would take elements 42, 14, -5, and 86.
10+
11+
Given the array `[-5, -1, -8, -9]`, the maximum sum would be 0, since we would not take any elements.
12+
13+
Do this in O(N) time.
14+

problem049/problem049.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem049
2+
3+
func MaximizeContiguousSum(values []int) int {
4+
// ex) [10, 20, -50, 24, 1, -10, 20, -6] => [10, 20, -50] [24, 1, -10, 20] [-6]
5+
totalSum := 0
6+
for _, value := range values {
7+
totalSum += value
8+
}
9+
10+
leftExcludedMinSum, leftExcludedSum, leftExcludedEndsAt := 0, 0, 0
11+
rightExcludedMinSum, rightExcludedSum, rightExcludedStartsAt := totalSum, totalSum, 0
12+
for i := range values {
13+
if leftExcludedSum += values[i]; leftExcludedSum < leftExcludedMinSum {
14+
leftExcludedEndsAt = i + 1
15+
}
16+
if rightExcludedSum -= values[i]; rightExcludedSum < rightExcludedMinSum {
17+
rightExcludedStartsAt = i + 1
18+
}
19+
}
20+
21+
if leftExcludedEndsAt >= rightExcludedStartsAt {
22+
return 0
23+
}
24+
maxContiguousSum := 0
25+
for _, value := range values[leftExcludedEndsAt:rightExcludedStartsAt] {
26+
maxContiguousSum += value
27+
}
28+
return maxContiguousSum
29+
}

problem049/problem049_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package problem049
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestMaximizeContiguousSum(t *testing.T) {
9+
testArgs := [][]int{
10+
{34, -50, 42, 14, -5, 86},
11+
{-5, -1, -8, -9},
12+
}
13+
correctResult := []int{137, 0}
14+
for i, arg := range testArgs {
15+
result := MaximizeContiguousSum(arg)
16+
if result != correctResult[i] {
17+
t.Log(fmt.Sprintf("%v: %d != %d", arg, result, correctResult))
18+
t.Fail()
19+
}
20+
}
21+
}

problem050/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Daily Coding Problem: Problem #50
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Microsoft.
6+
7+
Suppose an arithmetic expression is given as a binary tree. Each leaf is an integer and each internal node is one of '+', '−', '∗', or '/'.
8+
9+
Given the root to such a tree, write a function to evaluate it.
10+
11+
For example, given the following tree:
12+
13+
```
14+
*
15+
/ \
16+
+ +
17+
/ \ / \
18+
3 2 4 5
19+
```
20+
21+
You should return 45, as it is (3 + 2) * (4 + 5).

problem050/problem050.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package problem050
2+
3+
import "errors"
4+
5+
type TreeNode interface {
6+
evaluate() (int, error)
7+
}
8+
9+
type OperatorNode struct {
10+
value rune
11+
left TreeNode
12+
right TreeNode
13+
}
14+
15+
func (operatorNode OperatorNode) evaluate() (int, error) {
16+
leftValue, err := operatorNode.left.evaluate()
17+
if err != nil {
18+
return 0, err
19+
}
20+
rightValue, err := operatorNode.right.evaluate()
21+
if err != nil {
22+
return 0, err
23+
}
24+
switch operatorNode.value {
25+
case '+':
26+
return leftValue + rightValue, nil
27+
case '-':
28+
return leftValue - rightValue, nil
29+
case '*':
30+
return leftValue * rightValue, nil
31+
case '/':
32+
return leftValue / rightValue, nil
33+
default:
34+
return 0, errors.New("invalid operator")
35+
}
36+
}
37+
38+
type NumberNode struct {
39+
value int
40+
}
41+
42+
func (numberNode NumberNode) evaluate() (int, error) {
43+
return numberNode.value, nil
44+
}

problem050/problem050_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package problem050
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestEvaluate(t *testing.T) {
9+
tree := OperatorNode{
10+
value: '*',
11+
left: OperatorNode{
12+
value: '+',
13+
left: TreeNode(&NumberNode{3}),
14+
right: TreeNode(&NumberNode{2}),
15+
},
16+
right: OperatorNode{
17+
value: '+',
18+
left: TreeNode(&NumberNode{4}),
19+
right: TreeNode(&NumberNode{5}),
20+
},
21+
}
22+
result, err := tree.evaluate()
23+
correctResult := (3 + 2) * (4 + 5)
24+
if err != nil || result != correctResult {
25+
t.Error(err)
26+
t.Error(fmt.Sprintf("%d != %d", result, correctResult))
27+
t.Fail()
28+
}
29+
}

0 commit comments

Comments
 (0)