Challenge - 5 Problems
Kth Smallest Heap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Kth Smallest Element Extraction
What is the output of the following Go code that finds the 3rd smallest element using a min heap?
DSA Go
package main import ( "container/heap" "fmt" ) 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{}) { *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func kthSmallest(nums []int, k int) int { h := &IntHeap{} heap.Init(h) for _, num := range nums { heap.Push(h, num) } var kth int for i := 0; i < k; i++ { kth = heap.Pop(h).(int) } return kth } func main() { nums := []int{7, 10, 4, 3, 20, 15} k := 3 fmt.Println(kthSmallest(nums, k)) }
Attempts:
2 left
💡 Hint
Remember the min heap always pops the smallest element first.
✗ Incorrect
The sorted order of the array is [3, 4, 7, 10, 15, 20]. The 3rd smallest element is 7.
❓ Predict Output
intermediate2:00remaining
Result of Kth Smallest Element with Duplicate Values
What will be the output of this Go code finding the 4th smallest element using a min heap with duplicates?
DSA Go
package main import ( "container/heap" "fmt" ) 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{}) { *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func kthSmallest(nums []int, k int) int { h := &IntHeap{} heap.Init(h) for _, num := range nums { heap.Push(h, num) } var kth int for i := 0; i < k; i++ { kth = heap.Pop(h).(int) } return kth } func main() { nums := []int{5, 3, 3, 1, 2, 2, 4} k := 4 fmt.Println(kthSmallest(nums, k)) }
Attempts:
2 left
💡 Hint
Duplicates are counted separately in the heap pops.
✗ Incorrect
Sorted array with duplicates: [1, 2, 2, 3, 3, 4, 5]. The 4th smallest is 3.
🔧 Debug
advanced2:00remaining
Identify the Error in Kth Smallest Element Code
What error will this Go code produce when trying to find the 2nd smallest element using a min heap?
DSA Go
package main import ( "container/heap" "fmt" ) 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{}) { *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func kthSmallest(nums []int, k int) int { h := &IntHeap{} heap.Init(h) for _, num := range nums { heap.Push(h, num) } var kth int for i := 0; i < k; i++ { kth = heap.Pop(h).(int) } return kth } func main() { nums := []int{8, 3, 5, 1} k := 2 fmt.Println(kthSmallest(nums, k)) }
Attempts:
2 left
💡 Hint
Check the Less function in the heap interface.
✗ Incorrect
The Less function is reversed, so the heap acts as a max heap. The 2nd pop returns 5, which is the 2nd smallest in a max heap of all elements.
❓ Predict Output
advanced2:00remaining
Output of Kth Smallest Element with Partial Heap Push
What is the output of this Go code that pushes only first k elements into the min heap and then processes the rest?
DSA Go
package main import ( "container/heap" "fmt" ) 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{}) { *h = append(*h, x.(int)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func kthSmallest(nums []int, k int) int { h := &IntHeap{} for i := 0; i < k; i++ { heap.Push(h, nums[i]) } heap.Init(h) for i := k; i < len(nums); i++ { if nums[i] < (*h)[0] { (*h)[0] = nums[i] heap.Fix(h, 0) } } return (*h)[0] } func main() { nums := []int{12, 3, 5, 7, 19} k := 2 fmt.Println(kthSmallest(nums, k)) }
Attempts:
2 left
💡 Hint
Trace the heap state after each operation.
✗ Incorrect
Pushes first 2: 12 then 3, heap [3,12]. Init no change. Then 5<3? No; 7<3 No; 19<3 No. Root remains 3.
🚀 Application
expert3:00remaining
Kth Smallest Element in a Stream Using Min Heap
You want to find the kth smallest element in a stream of integers arriving one by one. Which approach using a min heap is correct to maintain the kth smallest element efficiently?
Attempts:
2 left
💡 Hint
To efficiently track kth smallest, keep only k elements in the heap.
✗ Incorrect
Maintaining a max heap of size k with the k smallest elements allows efficient updates and retrieval of kth smallest element as the root.