Go LeetCode Snippets & Templates
Reusable Go code patterns that come up repeatedly in LeetCode problems.
Frequency Map
count := make(map[int]int)
for _, num := range nums {
count[num]++
}Used in: Top-K Elements, Sliding Window
Min / Max Helpers
Go has no built-in generic min/max before 1.21. For older versions:
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}From Go 1.21+, use the built-in min() and max().
Heap Interface Boilerplate
Go’s container/heap requires implementing the heap.Interface:
type MinHeap []int
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x any) { *h = append(*h, x.(any).(int)) }
func (h *MinHeap) Pop() any {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}Common Mistake
PushandPopuse pointer receivers (*MinHeap), butLen,Less, andSwapuse value receivers (MinHeap). Getting this wrong causes subtle bugs.
Used in: Top-K Elements, Two Heaps, K-Way Merge
Bucket Sort by Frequency
freq := make([][]int, len(nums)+1)
for val, cnt := range count {
freq[cnt] = append(freq[cnt], val)
}
for i := len(freq) - 1; i >= 0; i-- {
// freq[i] contains all values that appeared i times
}Used in: Top-K Elements
Slice as Stack
stack := []int{}
stack = append(stack, val) // push
top := stack[len(stack)-1] // peek
stack = stack[:len(stack)-1] // popBracket Matching with Map
For problems like Valid Parentheses, use a map to eliminate if-else chains:
pairs := map[rune]rune{')': '(', '}': '{', ']': '['}
stack := []rune{}
for _, ch := range s {
if ch == '(' || ch == '{' || ch == '[' {
stack = append(stack, ch)
} else {
if len(stack) == 0 || stack[len(stack)-1] != pairs[ch] {
return false
}
stack = stack[:len(stack)-1]
}
}
return len(stack) == 0Used in: Stack-Based Pattern, LC-20 (Valid Parentheses)
Expand from Center (Palindromes)
expand := func(s string, left, right int) (int, int) {
for left >= 0 && right < len(s) && s[left] == s[right] {
left--
right++
}
return left + 1, right - 1
}
// For each index, check both odd and even palindromes
for i := 0; i < len(s); i++ {
lo1, hi1 := expand(s, i, i) // odd-length
lo2, hi2 := expand(s, i, i+1) // even-length
}Used in: Two Pointers (expand from center), LC-5 (Longest Palindromic Substring), LC-647 (Palindromic Substrings)
Queue via Slice
queue := []int{}
queue = append(queue, val) // enqueue
front := queue[0] // peek
queue = queue[1:] // dequeue (note: doesn't free memory)For performance-critical queues, use a ring buffer or
container/list.
Related
- Slices — internals and gotchas
- Go vs Python for DSA — trade-offs
- Hash Tables — Go map patterns