Skip to content

Commit 6540379

Browse files
committed
adds problem058
1 parent 42ea383 commit 6540379

File tree

5 files changed

+160
-61
lines changed

5 files changed

+160
-61
lines changed

README.md

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,69 +9,71 @@ Solutions for [Daily Coding Problem](https://www.dailycodingproblem.com/ "Daily
99
## Getting Started
1010

1111
Run `make test` to test the solutions.
12+
1213
```bash
1314
make test # test all
1415
```
1516

1617
## Problems
1718

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

problem057/problem057.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ func SplitLine(fullSentence string, lengthLimit int) []string {
88
lines := make([]string, 0)
99
currentLineWords, currentLineLength := make([]string, 0, lengthLimit), 0
1010
for _, word := range strings.Split(fullSentence, " ") {
11-
if len(word) > lengthLimit {
11+
switch {
12+
case len(word) > lengthLimit:
1213
return nil
13-
} else if lineLengthToBe := currentLineLength + 1 + len(word); lineLengthToBe <= lengthLimit {
14+
case currentLineLength+1+len(word) <= lengthLimit:
1415
currentLineWords, currentLineLength = append(currentLineWords, word), currentLineLength+1+len(word)
15-
} else {
16+
default:
1617
lines = append(lines, strings.Join(currentLineWords, " "))
1718
currentLineWords, currentLineLength = []string{word}, len(word)
1819
}

problem058/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Daily Coding Problem: Problem #58
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Amazon.
6+
7+
An sorted array of integers was rotated an unknown number of times.
8+
9+
Given such an array, find the index of the element in the array in faster than linear time. If the element doesn't exist in the array, return null.
10+
11+
For example, given the array [13, 18, 25, 2, 8, 10] and the element 8, return 4 (the index of 8 in the array).
12+
13+
You can assume all the integers in the array are unique.

problem058/problem058.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package problem058
2+
3+
func Search(numbers []int, targetNumber int) *int {
4+
return modifiedBinarySearch(numbers, targetNumber, 0)
5+
}
6+
7+
func getIntPointer(value int) *int {
8+
return &value
9+
}
10+
11+
func modifiedBinarySearch(numbers []int, targetNumber int, outerIndex int) *int {
12+
if len(numbers) == 0 {
13+
return nil
14+
}
15+
16+
midIndex := (len(numbers) - 1) / 2
17+
if targetNumber == numbers[midIndex] {
18+
return getIntPointer(outerIndex + midIndex)
19+
}
20+
21+
firstNumber, midNumber, lastNumber := numbers[0], numbers[midIndex], numbers[len(numbers)-1]
22+
if firstNumber <= midNumber {
23+
if firstNumber <= targetNumber && targetNumber < midNumber {
24+
return modifiedBinarySearch(numbers[:midIndex], targetNumber, 0)
25+
}
26+
return modifiedBinarySearch(numbers[midIndex+1:], targetNumber, midIndex+1)
27+
} else {
28+
if midNumber < targetNumber && targetNumber <= lastNumber {
29+
return modifiedBinarySearch(numbers[midIndex+1:], targetNumber, midIndex+1)
30+
}
31+
return modifiedBinarySearch(numbers[:midIndex], targetNumber, 0)
32+
}
33+
}

problem058/problem058_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package problem058
2+
3+
import (
4+
"testing"
5+
)
6+
7+
type argumentSet struct {
8+
numbers []int
9+
targetNumber int
10+
result *int
11+
}
12+
13+
func assertValue(t *testing.T, actual *int, expected *int) bool {
14+
if (actual != nil || expected != nil) && (*actual != *expected) {
15+
t.Logf("Actual: %d / Expected: %d", *actual, *expected)
16+
return false
17+
}
18+
return true
19+
}
20+
21+
func TestSearch(t *testing.T) {
22+
testCases := []argumentSet{
23+
{
24+
numbers: []int{13, 18, 25, 2, 8, 10},
25+
targetNumber: 8,
26+
result: getIntPointer(4),
27+
},
28+
{
29+
numbers: []int{13, 18, 25, 2, 8, 10},
30+
targetNumber: 7,
31+
result: nil,
32+
},
33+
{
34+
numbers: []int{2, 8, 10, 11, 13, 18, 25},
35+
targetNumber: 1,
36+
result: nil,
37+
},
38+
{
39+
numbers: []int{},
40+
targetNumber: 8,
41+
result: nil,
42+
},
43+
}
44+
45+
for _, testCase := range testCases {
46+
if !assertValue(t, Search(testCase.numbers, testCase.targetNumber), testCase.result) {
47+
t.FailNow()
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)