0
0
DSA Goprogramming~20 mins

Kth Largest Element Using Max Heap in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Max Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Max Heap after inserting elements
What is the printed state of the max heap after inserting the elements [3, 1, 5, 12, 10] in that order?
DSA Go
package main

import (
	"container/heap"
	"fmt"
)

type MaxHeap []int

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

func (h *MaxHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *MaxHeap) Pop() interface{} {
	n := len(*h)
	x := (*h)[n-1]
	*h = (*h)[:n-1]
	return x
}

func main() {
	h := &MaxHeap{}
	heap.Init(h)
	elements := []int{3, 1, 5, 12, 10}
	for _, v := range elements {
		heap.Push(h, v)
	}
	fmt.Println(*h)
}
A[10 12 5 3 1]
B[12 10 5 1 3]
C[12 10 5 3 1]
D[5 3 1 10 12]
Attempts:
2 left
💡 Hint
Remember that in a max heap, the largest element is at the root and the heap property is maintained after each insertion.
Predict Output
intermediate
2:00remaining
Output after extracting max element from max heap
Given a max heap represented as [20, 15, 18, 8, 10, 5], what is the heap state after one Pop operation?
DSA Go
package main

import (
	"container/heap"
	"fmt"
)

type MaxHeap []int

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

func (h *MaxHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *MaxHeap) Pop() interface{} {
	n := len(*h)
	x := (*h)[n-1]
	*h = (*h)[:n-1]
	return x
}

func main() {
	h := &MaxHeap{20, 15, 18, 8, 10, 5}
	heap.Init(h)
	heap.Pop(h)
	fmt.Println(*h)
}
A[15 10 5 8]
B[18 15 5 8 10]
C[15 18 10 8 5]
D[18 15 10 8 5]
Attempts:
2 left
💡 Hint
Pop removes the root (max) and then heapifies to maintain max heap property.
🧠 Conceptual
advanced
2:00remaining
Understanding Kth Largest Element Extraction
If you want to find the 3rd largest element using a max heap built from an array, which of the following steps is correct?
ABuild a max heap, then pop the max element 3 times; the last popped element is the 3rd largest.
BBuild a max heap, then pop the max element once; the root is the 3rd largest.
CBuild a max heap, then pop the max element 2 times; the next root is the 3rd largest.
DBuild a max heap, then pop the max element 4 times; the last popped element is the 3rd largest.
Attempts:
2 left
💡 Hint
Think about how many times you remove the largest element to reach the kth largest.
🔧 Debug
advanced
2:00remaining
Identify the error in Kth largest element code snippet
What error will this Go code produce when trying to find the 2nd largest element using a max heap?
DSA Go
package main

import (
	"container/heap"
	"fmt"
)

type MaxHeap []int

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

func (h *MaxHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *MaxHeap) Pop() interface{} {
	n := len(*h)
	x := (*h)[n-1]
	*h = (*h)[:n-1]
	return x
}

func main() {
	h := &MaxHeap{10, 5, 20, 8}
	heap.Init(h)
	heap.Pop(h)
	next := (*h)[0]
	fmt.Println(next)
}
APrints 10
BPrints 20
CPrints 5
DRuntime panic: index out of range
Attempts:
2 left
💡 Hint
Check the Less function for max heap property correctness.
🚀 Application
expert
3:00remaining
Find the 4th largest element using max heap operations
Given the array [7, 10, 4, 3, 20, 15], what is the 4th largest element after building a max heap and popping the max element 4 times?
DSA Go
package main

import (
	"container/heap"
	"fmt"
)

type MaxHeap []int

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

func (h *MaxHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *MaxHeap) Pop() interface{} {
	n := len(*h)
	x := (*h)[n-1]
	*h = (*h)[:n-1]
	return x
}

func main() {
	h := &MaxHeap{}
	elements := []int{7, 10, 4, 3, 20, 15}
	for _, v := range elements {
		heap.Push(h, v)
	}
	heap.Init(h)
	var kthLargest int
	for i := 0; i < 4; i++ {
		kthLargest = heap.Pop(h).(int)
	}
	fmt.Println(kthLargest)
}
A15
B7
C4
D10
Attempts:
2 left
💡 Hint
Pop the max element 4 times and track the last popped value.