0
0
DSA Goprogramming

Kth Smallest Element Using Min Heap in DSA Go

Choose your learning style9 modes available
Mental Model
We use a min heap to always get the smallest number quickly. By removing the smallest number k times, the last one removed is the kth smallest.
Analogy: Imagine a pile of numbered cards. You keep picking the smallest card from the pile one by one. The card you pick on the kth turn is the kth smallest.
Min Heap Array: [2, 3, 5, 7, 8]
Heap structure:
      2
     / \
    3   5
   / \
  7   8
Dry Run Walkthrough
Input: array: [7, 10, 4, 3, 20, 15], k=3
Goal: Find the 3rd smallest element in the array using a min heap
Step 1: Build min heap from array
Min heap array: [3, 7, 4, 10, 20, 15]
Why: Heap organizes elements so smallest is at the root for quick access
Step 2: Extract min (1st smallest): remove 3
Heap after removal: [4, 7, 15, 10, 20]
Why: Remove smallest element to move towards kth smallest
Step 3: Extract min (2nd smallest): remove 4
Heap after removal: [7, 10, 15, 20]
Why: Remove next smallest to approach kth smallest
Step 4: Extract min (3rd smallest): remove 7
Heap after removal: [10, 20, 15]
Why: The 3rd extracted element is the kth smallest
Result:
Heap after 3 extractions: [10, 20, 15]
Kth smallest element: 7
Annotated Code
DSA Go
package main

import (
	"container/heap"
	"fmt"
)

// IntHeap is a min-heap of ints
// Implements heap.Interface
// Len, Less, Swap, Push, Pop

type IntHeap []int

func (h IntHeap) Len() int           { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] }
func (h IntHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
	// add element to heap
	*h = append(*h, x.(int))
}

func (h *IntHeap) Pop() interface{} {
	// remove last element
	n := len(*h)
	x := (*h)[n-1]
	*h = (*h)[:n-1]
	return x
}

// kthSmallest returns the kth smallest element using a min heap
func kthSmallest(arr []int, k int) int {
	h := &IntHeap{}
	heap.Init(h) // organize heap before pushing elements
	// build heap from array
	for _, v := range arr {
		heap.Push(h, v) // add all elements
	}

	var kth int
	// extract min k times
	for i := 0; i < k; i++ {
		kth = heap.Pop(h).(int) // remove smallest
	}
	return kth
}

func main() {
	arr := []int{7, 10, 4, 3, 20, 15}
	k := 3
	result := kthSmallest(arr, k)
	fmt.Printf("%dth smallest element is %d\n", k, result)
}
for _, v := range arr { heap.Push(h, v) // add all elements }
Add all elements to the heap to prepare for extraction
heap.Init(h) // organize heap
Arrange elements so smallest is at the root
for i := 0; i < k; i++ { kth = heap.Pop(h).(int) // remove smallest }
Remove smallest element k times; last removed is kth smallest
OutputSuccess
3th smallest element is 7
Complexity Analysis
Time: O(n + k log n) because building the heap takes O(n) and each of the k extractions takes O(log n)
Space: O(n) because the heap stores all n elements
vs Alternative: Sorting the array takes O(n log n), which is slower than building a heap and extracting k times when k is small
Edge Cases
k is 0 or less
Function returns 0 or invalid result since kth smallest does not exist
DSA Go
for i := 0; i < k; i++ {
k is greater than array length
Heap.Pop will panic or error because heap is empty before k pops
DSA Go
for i := 0; i < k; i++ {
array is empty
Heap is empty, Pop will panic or error
DSA Go
for i := 0; i < k; i++ {
When to Use This Pattern
When you need the kth smallest or largest element efficiently, especially if k is small compared to n, use a heap to avoid full sorting.
Common Mistakes
Mistake: Not building the heap properly before popping elements
Fix: Call heap.Init(h) after pushing all elements to organize the heap
Mistake: Popping fewer or more than k times
Fix: Pop exactly k times to get the kth smallest element
Summary
Finds the kth smallest element by building a min heap and extracting the smallest k times.
Use when you want kth smallest without sorting the entire array.
The smallest element is always at the root of the min heap, so popping k times gives the kth smallest.