Challenge - 5 Problems
Heap Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the heap array after inserting 15?
Given a max-heap represented as an array, what is the array after inserting the value 15 and performing bubble up?
DSA Java
int[] heap = {20, 18, 10, 12, 9, 8}; // Insert 15 and bubble up // Show final heap array
Attempts:
2 left
💡 Hint
Remember to bubble up the inserted element until the max-heap property is restored.
✗ Incorrect
After inserting 15 at the end, it bubbles up swapping with 10 to maintain max-heap property. Final array is [20, 18, 15, 12, 9, 8, 10].
❓ Predict Output
intermediateWhat is the heap array after inserting 5?
Given a max-heap array, what is the array after inserting 5 and performing bubble up?
DSA Java
int[] heap = {30, 20, 15, 10, 8, 12}; // Insert 5 and bubble up // Show final heap array
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no bubble up occurs.
✗ Incorrect
5 is inserted at the end. Its parent is 15 which is larger, so no swaps happen. Final array is [30, 20, 15, 10, 8, 12, 5].
🔧 Debug
advancedWhy does this bubble up code cause an infinite loop?
Consider this bubble up code snippet for a max-heap insert. Why does it cause an infinite loop?
DSA Java
int i = heapSize - 1; while (i > 0 && heap[i] > heap[(i - 1) / 2]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; // Missing update of i }
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
The variable i is never updated inside the while loop, so the condition remains true forever causing infinite loop.
❓ Predict Output
advancedWhat is the heap array after inserting 25?
Given a max-heap array, what is the array after inserting 25 and performing bubble up?
DSA Java
int[] heap = {40, 30, 20, 15, 10, 18}; // Insert 25 and bubble up // Show final heap array
Attempts:
2 left
💡 Hint
Bubble up swaps with parent if inserted value is larger.
✗ Incorrect
Insert 25 at index 6. Parent index is 2 with value 20. 25 > 20, swap. Result: [40, 30, 25, 15, 10, 18, 20]. No further swaps needed.
🧠 Conceptual
expertWhat is the time complexity of the bubble up operation in a heap insert?
Choose the correct time complexity for the bubble up operation when inserting an element into a heap of size n.
Attempts:
2 left
💡 Hint
Consider the height of the heap and how many swaps bubble up can perform.
✗ Incorrect
Bubble up moves the inserted element up the tree at most the height of the heap, which is log n for a heap of size n.
