forked from RezaSi/go-interview-practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution-template.go
More file actions
133 lines (112 loc) · 3.98 KB
/
solution-template.go
File metadata and controls
133 lines (112 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"strings"
"time"
)
// SlowSort sorts a slice of integers using a very inefficient algorithm (bubble sort)
// TODO: Optimize this function to be more efficient
func SlowSort(data []int) []int {
// Make a copy to avoid modifying the original
result := make([]int, len(data))
copy(result, data)
// Bubble sort implementation
for i := 0; i < len(result); i++ {
for j := 0; j < len(result)-1; j++ {
if result[j] > result[j+1] {
result[j], result[j+1] = result[j+1], result[j]
}
}
}
return result
}
// OptimizedSort is your optimized version of SlowSort
// It should produce identical results but perform better
func OptimizedSort(data []int) []int {
// TODO: Implement a more efficient sorting algorithm
// Hint: Consider using sort package or a more efficient algorithm
return SlowSort(data) // Replace this with your optimized implementation
}
// InefficientStringBuilder builds a string by repeatedly concatenating
// TODO: Optimize this function to be more efficient
func InefficientStringBuilder(parts []string, repeatCount int) string {
result := ""
for i := 0; i < repeatCount; i++ {
for _, part := range parts {
result += part
}
}
return result
}
// OptimizedStringBuilder is your optimized version of InefficientStringBuilder
// It should produce identical results but perform better
func OptimizedStringBuilder(parts []string, repeatCount int) string {
// TODO: Implement a more efficient string building method
// Hint: Consider using strings.Builder or bytes.Buffer
return InefficientStringBuilder(parts, repeatCount) // Replace this with your optimized implementation
}
// ExpensiveCalculation performs a computation with redundant work
// It computes the sum of all fibonacci numbers up to n
// TODO: Optimize this function to be more efficient
func ExpensiveCalculation(n int) int {
if n <= 0 {
return 0
}
sum := 0
for i := 1; i <= n; i++ {
sum += fibonacci(i)
}
return sum
}
// Helper function that computes the fibonacci number at position n
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
// OptimizedCalculation is your optimized version of ExpensiveCalculation
// It should produce identical results but perform better
func OptimizedCalculation(n int) int {
// TODO: Implement a more efficient calculation method
// Hint: Consider memoization or avoiding redundant calculations
return ExpensiveCalculation(n) // Replace this with your optimized implementation
}
// HighAllocationSearch searches for all occurrences of a substring and creates a map with their positions
// TODO: Optimize this function to reduce allocations
func HighAllocationSearch(text, substr string) map[int]string {
result := make(map[int]string)
// Convert to lowercase for case-insensitive search
lowerText := strings.ToLower(text)
lowerSubstr := strings.ToLower(substr)
for i := 0; i < len(lowerText); i++ {
// Check if we can fit the substring starting at position i
if i+len(lowerSubstr) <= len(lowerText) {
// Extract the potential match
potentialMatch := lowerText[i : i+len(lowerSubstr)]
// Check if it matches
if potentialMatch == lowerSubstr {
// Store the original case version
result[i] = text[i : i+len(substr)]
}
}
}
return result
}
// OptimizedSearch is your optimized version of HighAllocationSearch
// It should produce identical results but perform better with fewer allocations
func OptimizedSearch(text, substr string) map[int]string {
// TODO: Implement a more efficient search method with fewer allocations
// Hint: Consider avoiding temporary string allocations and reusing memory
return HighAllocationSearch(text, substr) // Replace this with your optimized implementation
}
// A function to simulate CPU-intensive work for benchmarking
// You don't need to optimize this; it's just used for testing
func SimulateCPUWork(duration time.Duration) {
start := time.Now()
for time.Since(start) < duration {
// Just waste CPU cycles
for i := 0; i < 1000000; i++ {
_ = i
}
}
}