Skip to content

Commit ac1cd4d

Browse files
committed
problem 42, 43
1 parent f8e283d commit ac1cd4d

File tree

8 files changed

+193
-1
lines changed

8 files changed

+193
-1
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ Number | Difficulty | Asked By | Title
5858
[#39](problem039) | MEDIUM | Dropbox | Conway's Game of Life
5959
[#40](problem040) | HARD | Google | Finding the non-duplicated number
6060
[#41](problem041) | MEDIUM | Facebook | Finding full itinerary from a list of flights
61+
[#42](problem042) | HARD | Google | Finding a subset of S that adds up to k
62+
[#43](problem043) | EASY | Amazon | Implementing Stack
63+

problem041/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ For example, given the list of flights `[('SFO', 'HKO'), ('YYZ', 'SFO'), ('YUL',
1111
Given the list of flights `[('SFO', 'COM'), ('COM', 'YYZ')]` and starting airport 'COM', you should return null.
1212

1313
Given the list of flights `[('A', 'B'), ('A', 'C'), ('B', 'C'), ('C', 'A')]` and starting airport 'A', ~~you should return the list `['A', 'B', 'C', 'A', 'C']` even though `['A', 'C', 'A', 'B', 'C']` is also a valid itinerary. However, the first one is lexicographically smaller.~~
14-
**you should return nil.**
14+
**you should return null.**

problem042/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Daily Coding Problem: Problem #42 [HARD]
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Google.
6+
7+
Given a list of integers S and a target number k, write a function that returns a subset of S that adds up to k. If such a subset cannot be made, then return null.
8+
9+
Integers can appear more than once in the list. You may assume all numbers in the list are positive.
10+
11+
For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24.

problem042/problem042.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package problem042
2+
3+
func GetSubset(values []int, sum int) []int {
4+
if len(values) == 0 {
5+
if sum == 0 {
6+
return []int{}
7+
} else {
8+
return nil
9+
}
10+
}
11+
if subsetWithoutFirstItem := GetSubset(values[1:], sum); subsetWithoutFirstItem != nil {
12+
return subsetWithoutFirstItem
13+
}
14+
if subsetWithFirstItem := GetSubset(values[1:], sum-values[0]); subsetWithFirstItem != nil {
15+
return append(values[0:1], subsetWithFirstItem...)
16+
}
17+
return nil
18+
}

problem042/problem042_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package problem042
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestGetSubset(t *testing.T) {
10+
values := []int{12, 1, 61, 5, 9, 2}
11+
k := 24
12+
answer, correctAnswer := GetSubset(values, k), []int{12, 1, 9, 2}
13+
if !reflect.DeepEqual(answer, correctAnswer) {
14+
t.Log(fmt.Sprintf("%v, %d: %v != %v", values, k, answer, correctAnswer))
15+
t.FailNow()
16+
}
17+
j := 61
18+
answer2, correctAnswer2 := GetSubset(values, j), []int{61}
19+
if !reflect.DeepEqual(answer, correctAnswer) {
20+
t.Log(fmt.Sprintf("%v, %d: %v != %v", values, j, answer2, correctAnswer2))
21+
t.FailNow()
22+
}
23+
}

problem043/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## Daily Coding Problem: Problem #43 [EASY]
2+
3+
4+
Good morning! Here's your coding interview problem for today.
5+
6+
This problem was asked by Amazon.
7+
8+
Implement a stack that has the following methods:
9+
10+
---
11+
* push(val), which pushes an element onto the stack
12+
* pop(), which pops off and returns the topmost element of the stack. If there are no elements in the stack, then it should throw an error or return null.
13+
* max(), which returns the maximum value in the stack currently. If there are no elements in the stack, then it should throw an error or return null.
14+
---
15+
16+
Each method should run in constant time.

problem043/problem043.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package problem043
2+
3+
type stack struct {
4+
values []int
5+
maxValueStack *stack
6+
}
7+
8+
type StackEmptyError struct{}
9+
10+
func (stackEmptyError StackEmptyError) Error() string {
11+
return "StackEmptyError"
12+
}
13+
14+
func NewStack(initialCapacity uint) *stack {
15+
return &stack{
16+
values: make([]int, 0, initialCapacity),
17+
maxValueStack: &stack{
18+
values: make([]int, 0, initialCapacity),
19+
maxValueStack: nil,
20+
},
21+
}
22+
}
23+
24+
func (thisStack *stack) peek() (int, error) {
25+
if len(thisStack.values) == 0 {
26+
return 0, StackEmptyError{}
27+
}
28+
return thisStack.values[len(thisStack.values)-1], nil
29+
}
30+
31+
func (thisStack *stack) Push(value int) *stack {
32+
if thisStack.maxValueStack != nil {
33+
currentMaxValue, err := thisStack.maxValueStack.peek()
34+
if (err != nil && err.Error() == "StackEmptyError") || currentMaxValue <= value {
35+
thisStack.maxValueStack.Push(value)
36+
}
37+
}
38+
thisStack.values = append(thisStack.values, value)
39+
return thisStack
40+
}
41+
42+
func (thisStack *stack) Pop() (int, error) {
43+
if len(thisStack.values) == 0 {
44+
return 0, StackEmptyError{}
45+
}
46+
value := thisStack.values[len(thisStack.values)-1]
47+
thisStack.values = thisStack.values[:len(thisStack.values)-1]
48+
49+
if thisStack.maxValueStack != nil {
50+
currentMaxValue, _ := thisStack.maxValueStack.peek()
51+
if currentMaxValue == value {
52+
_, _ = thisStack.maxValueStack.Pop()
53+
}
54+
}
55+
return value, nil
56+
}
57+
58+
func (thisStack *stack) Max() (int, error) {
59+
currentMaxValue, err := thisStack.maxValueStack.peek()
60+
if err != nil {
61+
return 0, err
62+
}
63+
return currentMaxValue, nil
64+
}

problem043/problem043_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package problem043
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestStack(t *testing.T) {
9+
stack := NewStack(32).Push(4).Push(10).Push(12).Push(4).Push(12)
10+
11+
correctAnswer := 12
12+
if maxValue, err := stack.Max(); err != nil || maxValue != correctAnswer {
13+
t.Log(fmt.Sprintf("Max %d != %d", maxValue, correctAnswer), err)
14+
t.Fail()
15+
}
16+
17+
correctAnswer = 12
18+
if poppedValue, err := stack.Pop(); err != nil || poppedValue != correctAnswer {
19+
t.Log(fmt.Sprintf("Pop %d != %d", poppedValue, correctAnswer), err)
20+
t.Fail()
21+
}
22+
23+
correctAnswer = 12
24+
if maxValue, err := stack.Max(); err != nil || maxValue != correctAnswer {
25+
t.Log(fmt.Sprintf("Max %d != %d", maxValue, correctAnswer), err)
26+
t.Fail()
27+
}
28+
29+
correctAnswer = 4
30+
if poppedValue, err := stack.Pop(); err != nil || poppedValue != correctAnswer {
31+
t.Log(fmt.Sprintf("Pop %d != %d", poppedValue, correctAnswer), err)
32+
t.Fail()
33+
}
34+
35+
_, _ = stack.Pop()
36+
37+
correctAnswer = 10
38+
if maxValue, err := stack.Max(); err != nil || maxValue != correctAnswer {
39+
t.Log(fmt.Sprintf("Max %d != %d", maxValue, correctAnswer), err)
40+
t.Fail()
41+
}
42+
43+
_, _ = stack.Pop()
44+
45+
correctAnswer = 4
46+
if maxValue, err := stack.Max(); err != nil || maxValue != correctAnswer {
47+
t.Log(fmt.Sprintf("Max %d != %d", maxValue, correctAnswer), err)
48+
t.Fail()
49+
}
50+
51+
_, _ = stack.Pop()
52+
53+
if _, err := stack.Pop(); err == nil {
54+
t.Log("Pop without error")
55+
t.Fail()
56+
}
57+
}

0 commit comments

Comments
 (0)