Go for Python Devs: LeetCode One-Page Cheat Sheet
Go for Python Devs: LeetCode One-Page Cheat Sheet
Section titled “Go for Python Devs: LeetCode One-Page Cheat Sheet”60-Second Pre-Interview Version
Section titled “60-Second Pre-Interview Version”// Most-used importsimport ("container/heap"; "sort")
// Min helpersfunc 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 }
// Binary search (first true in [0,n))l, r := 0, nfor l < r { m := l + (r-l)/2 if check(m) { r = m } else { l = m + 1 }}
// BFS queue (O(1) amortized pop-left)q, head := []int{start}, 0seen := map[int]bool{start:true}for head < len(q) { x := q[head]; head++ for _, y := range g[x] { if seen[y] { continue } seen[y] = true q = append(q, y) }}
// DFS recursivevar dfs func(int)dfs = func(u int) { vis[u] = true for _, v := range g[u] { if !vis[v] { dfs(v) } }}
// Sliding windowl := 0for r := 0; r < len(s); r++ { add(s[r]) for bad() { remove(s[l]); l++ } ans = max(ans, r-l+1)}
// Prefix sum + hash (subarray sum == k)cnt := map[int]int{0:1}sum := 0for _, x := range nums { sum += x ans += cnt[sum-k] cnt[sum]++}
// Monotonic stack skeletonst := []int{} // usually indicesfor i := 0; i < n; i++ { for len(st) > 0 && bad(st[len(st)-1], i) { st = st[:len(st)-1] } st = append(st, i)}
// DSU findfunc (d *DSU) Find(x int) int { if d.p[x] != x { d.p[x] = d.Find(d.p[x]) } return d.p[x]}Rapid gotchas:
len(string)is bytes; use[]runefor unicode index.for rangecopies values; mutate via index.- map iteration order is random.
- use
int64for large sums/products.
LeetCode Signatures (Copy/Paste)
Section titled “LeetCode Signatures (Copy/Paste)”// Arrays / stringsfunc twoSum(nums []int, target int) []int {}func lengthOfLongestSubstring(s string) int {}
// Linked listtype ListNode struct { Val int Next *ListNode}func reverseList(head *ListNode) *ListNode {}
// Binary treetype TreeNode struct { Val int Left *TreeNode Right *TreeNode}func maxDepth(root *TreeNode) int {}Python -> Go Mental Map
Section titled “Python -> Go Mental Map”| Python | Go |
|---|---|
list[int] | []int |
dict[k]v | map[K]V |
set[int] | map[int]struct{} |
tuple(a,b) | [2]int or struct{a,b int} |
None | nil (for ptr/map/slice/interface/func/channel) |
heapq | container/heap (interface impl) |
deque | []T + head index |
Core Syntax You Actually Use
Section titled “Core Syntax You Actually Use”package main
import ( "container/heap" "sort" "strconv" "strings")
func solve(nums []int) int { ans := 0 for i := 0; i < len(nums); i++ { if nums[i] > 0 { ans += nums[i] } } return ans}Containers and Common Ops
Section titled “Containers and Common Ops”a := []int{1, 2, 3}a = append(a, 4) // pushx := a[len(a)-1]; a = a[:len(a)-1] // popa = append(a[:i], a[i+1:]...) // delete index ib := make([]int, n) // zeroed len=nc := make([]int, n, 2*n) // len=n cap=2ncopy(b, a) // copy min(len(b),len(a))
// map/dictm := map[string]int{}m["a"]++v, ok := m["x"] // if !ok, v is zero valuedelete(m, "a")
// setset := map[int]struct{}{}set[5] = struct{}{}_, ok = set[5]delete(set, 5)Strings, Bytes, Unicode
Section titled “Strings, Bytes, Unicode”s := "abc"n := len(s) // bytes, not runessub := s[l:r] // byte slice
bs := []byte(s) // mutable bytesbs[0] = 'z's2 := string(bs)
rs := []rune(s) // unicode-safe indexing_ = rs
parts := strings.Split("a,b,c", ",")joined := strings.Join(parts, "|")num, _ := strconv.Atoi("123")str := strconv.Itoa(123)Patterns (Drop-in Templates)
Section titled “Patterns (Drop-in Templates)”Two Pointers
Section titled “Two Pointers”l, r := 0, len(nums)-1for l < r { if nums[l]+nums[r] < target { l++ } else { r-- }}Binary Search (First True)
Section titled “Binary Search (First True)”l, r := 0, n // [l, r)for l < r { mid := l + (r-l)/2 if check(mid) { r = mid } else { l = mid + 1 }}ans := l_ = ansPrefix Sum
Section titled “Prefix Sum”pre := make([]int, len(nums)+1)for i := 0; i < len(nums); i++ { pre[i+1] = pre[i] + nums[i]}sumLR := pre[r+1] - pre[l] // inclusive [l..r]_ = sumLR
// Prefix hash map: count subarrays with sum == kcnt := map[int]int{0: 1}sum, ans := 0, 0for _, x := range nums { sum += x ans += cnt[sum-k] cnt[sum]++}Monotonic Stack (Next Greater Example)
Section titled “Monotonic Stack (Next Greater Example)”res := make([]int, len(nums))st := []int{} // store indicesfor i := len(nums) - 1; i >= 0; i-- { for len(st) > 0 && nums[st[len(st)-1]] <= nums[i] { st = st[:len(st)-1] } if len(st) == 0 { res[i] = -1 } else { res[i] = nums[st[len(st)-1]] } st = append(st, i)}Sliding Window (Variable Length)
Section titled “Sliding Window (Variable Length)”freq := map[byte]int{}best := 0l := 0for r := 0; r < len(s); r++ { c := s[r] freq[c]++ for /* invalid window */ false { d := s[l] freq[d]-- if freq[d] == 0 { delete(freq, d) } l++ } if r-l+1 > best { best = r - l + 1 }}BFS (Queue with Head Index)
Section titled “BFS (Queue with Head Index)”q := []int{start}head := 0seen := map[int]bool{start: true}for head < len(q) { x := q[head] head++ for _, y := range g[x] { if seen[y] { continue } seen[y] = true q = append(q, y) }}DFS (Recursive)
Section titled “DFS (Recursive)”func dfs(u int, g [][]int, vis []bool) { vis[u] = true for _, v := range g[u] { if !vis[v] { dfs(v, g, vis) } }}Backtracking
Section titled “Backtracking”path := []int{}var ans [][]intvar dfs func(int)dfs = func(i int) { if i == n { t := append([]int(nil), path...) // clone ans = append(ans, t) return } // choose path = append(path, nums[i]) dfs(i + 1) path = path[:len(path)-1] // skip dfs(i + 1)}dfs(0)Topological Sort (Kahn)
Section titled “Topological Sort (Kahn)”indeg := make([]int, n)g := make([][]int, n)for _, e := range edges { u, v := e[0], e[1] g[u] = append(g[u], v) indeg[v]++}q := []int{}for i := 0; i < n; i++ { if indeg[i] == 0 { q = append(q, i) }}head, order := 0, []int{}for head < len(q) { u := q[head] head++ order = append(order, u) for _, v := range g[u] { indeg[v]-- if indeg[v] == 0 { q = append(q, v) } }}isDAG := len(order) == n_ = isDAGIntervals
Section titled “Intervals”sort.Slice(intervals, func(i, j int) bool { return intervals[i][0] < intervals[j][0] })out := [][]int{}for _, it := range intervals { if len(out) == 0 || out[len(out)-1][1] < it[0] { out = append(out, []int{it[0], it[1]}) } else if it[1] > out[len(out)-1][1] { out[len(out)-1][1] = it[1] }}Heap (Min Heap of Int)
Section titled “Heap (Min Heap of Int)”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 interface{}) { *h = append(*h, x.(int)) }func (h *MinHeap) Pop() interface{} { old := *h x := old[len(old)-1] *h = old[:len(old)-1] return x}
h := &MinHeap{}heap.Init(h)heap.Push(h, 3)heap.Push(h, 1)mn := heap.Pop(h).(int)_ = mnUnion Find (DSU)
Section titled “Union Find (DSU)”type DSU struct { p []int r []int}
func NewDSU(n int) *DSU { p, r := make([]int, n), make([]int, n) for i := range p { p[i] = i } return &DSU{p: p, r: r}}
func (d *DSU) Find(x int) int { if d.p[x] != x { d.p[x] = d.Find(d.p[x]) } return d.p[x]}
func (d *DSU) Union(a, b int) bool { ra, rb := d.Find(a), d.Find(b) if ra == rb { return false } if d.r[ra] < d.r[rb] { ra, rb = rb, ra } d.p[rb] = ra if d.r[ra] == d.r[rb] { d.r[ra]++ } return true}DP Skeletons
Section titled “DP Skeletons”// 1D DP: min stepsdp := make([]int, n+1)const INF = int(1e9)for i := range dp { dp[i] = INF}dp[0] = 0for i := 1; i <= n; i++ { // dp[i] = min(dp[i], dp[i-k]+cost)}
// 2D DP griddp2 := make([][]int, m)for i := range dp2 { dp2[i] = make([]int, n)}Linked List (In-Place Reverse)
Section titled “Linked List (In-Place Reverse)”var prev *ListNodecur := headfor cur != nil { nxt := cur.Next cur.Next = prev prev = cur cur = nxt}head = prevTree Traversal (Iterative Inorder)
Section titled “Tree Traversal (Iterative Inorder)”st := []*TreeNode{}cur := rootfor cur != nil || len(st) > 0 { for cur != nil { st = append(st, cur) cur = cur.Left } cur = st[len(st)-1] st = st[:len(st)-1] // visit cur.Val cur = cur.Right}Grid Helpers
Section titled “Grid Helpers”dirs := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}in := func(r, c int) bool { return r >= 0 && r < m && c >= 0 && c < n }Interview Gotchas (High Value)
Section titled “Interview Gotchas (High Value)”for rangecopies element value; mutate slice via index (nums[i] = ...).- No
while; usefor condition {}. - No ternary operator.
- Map iteration order is random.
len(string)is bytes; convert to[]runefor unicode indexing.- Use
int64for large sums/products. - Recursive DFS can stack overflow on deep graphs; switch to iterative if needed.
Tiny Helpers
Section titled “Tiny Helpers”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 }func abs(x int) int { if x < 0 { return -x }; return x }Bit + Math Micro-Patterns
Section titled “Bit + Math Micro-Patterns”isOdd := x&1 == 1pow2 := x > 0 && (x&(x-1)) == 0lowbit := x & -xbits := 0for y := x; y > 0; y &= y - 1 { bits++ } // popcount
gcd := func(a, b int) int { for b != 0 { a, b = b, a%b } return a}Big-O Memory Hooks
Section titled “Big-O Memory Hooks”- Sort:
O(n log n) - Hash lookup: average
O(1) - Heap push/pop:
O(log n) - BFS/DFS graph:
O(V+E) - Union-Find: almost
O(1)amortized