📖 Scenario: Imagine you are organizing a small event where you need to quickly find the person with the earliest arrival time or the person with the latest arrival time. You want to use a data structure that helps you find these people efficiently.
🎯 Goal: Build a simple program that uses a min heap to find the earliest arrival time and a max heap to find the latest arrival time from a list of arrival times.
📋 What You'll Learn
Create an array of arrival times as integers
Create a variable to store the number of arrival times
Implement a function to build a min heap from the array
Implement a function to build a max heap from the array
Print the earliest arrival time using the min heap
Print the latest arrival time using the max heap
💡 Why This Matters
🌍 Real World
Heaps are used in scheduling tasks, priority queues, and finding minimum or maximum values quickly in many applications like event management and resource allocation.
💼 Career
Understanding heaps is important for software engineers working on performance-critical applications, algorithms, and systems that require efficient data retrieval.
Progress0 / 4 steps
1
Create the arrival times array
Create an integer array called arrival_times with these exact values: 15, 3, 9, 8, 5, 2, 7. Also create an integer variable called n and set it to the number of elements in arrival_times.
DSA C
Hint
Use int arrival_times[] = {15, 3, 9, 8, 5, 2, 7}; and calculate n using sizeof.
2
Create helper functions for heap operations
Create two functions: void build_min_heap(int arr[], int n) and void build_max_heap(int arr[], int n). For now, just declare these functions without implementation.
DSA C
Hint
Write function declarations for build_min_heap and build_max_heap with parameters int arr[] and int n.
3
Implement min heap and max heap building logic
Implement the functions build_min_heap and build_max_heap to rearrange the array into a min heap and max heap respectively. Use a simple heapify approach starting from the last parent node. Use helper functions min_heapify and max_heapify inside these functions.
DSA C
Hint
Use heapify functions to build min and max heaps by adjusting the array in place.
4
Print earliest and latest arrival times
Call build_min_heap(arrival_times, n) and print the first element of arrival_times as the earliest arrival time. Then call build_max_heap(arrival_times, n) and print the first element of arrival_times as the latest arrival time.
DSA C
Hint
After building the min heap, the first element is the smallest (earliest). After building the max heap, the first element is the largest (latest).
Practice
(1/5)
1. What is the main difference between a Min Heap and a Max Heap?
easy
A. Min Heap always has the smallest element at the root, Max Heap has the largest at the root
B. Min Heap stores only positive numbers, Max Heap stores only negative numbers
C. Min Heap is a sorted array, Max Heap is a sorted linked list
D. Min Heap and Max Heap are the same data structure
Solution
Step 1: Understand heap root property
Min Heap keeps the smallest element at the root node, Max Heap keeps the largest element at the root node.
Step 2: Compare heap types
Since Min Heap root is smallest and Max Heap root is largest, they differ in root element priority.
Final Answer:
Min Heap always has the smallest element at the root, Max Heap has the largest at the root -> Option A
Quick Check:
Root element priority = smallest for Min Heap, largest for Max Heap [OK]
Hint: Min Heap root is smallest, Max Heap root is largest [OK]
Common Mistakes:
Confusing Min Heap with sorted array
Thinking heaps store only positive or negative numbers
Believing Min Heap and Max Heap are identical
2. Which of the following is the correct way to insert an element into a Min Heap in C?
easy
A. Add element at the end, then bubble down while parent is smaller
B. Add element at the root, then bubble down while children are smaller
C. Add element at the end, then bubble up while parent is greater
D. Add element at the root, then bubble up while children are greater
Solution
Step 1: Recall Min Heap insertion process
Insert element at the end of the heap array to maintain complete tree property.
Step 2: Restore heap property by bubbling up
Compare inserted element with parent; if parent is greater, swap and continue bubbling up.
Final Answer:
Add element at the end, then bubble up while parent is greater -> Option C
Quick Check:
Insert at end + bubble up if parent > element [OK]
Hint: Insert at end, bubble up if parent bigger in Min Heap [OK]
Common Mistakes:
Bubbling down instead of up after insertion
Inserting at root instead of end
Swapping when parent is smaller
3. Given the following Max Heap array: [50, 30, 40, 10, 20], what will be the array after inserting 60?
medium
A. [60, 50, 40, 10, 20, 30]
B. [50, 30, 40, 10, 20, 60]
C. [50, 60, 40, 10, 20, 30]
D. [60, 30, 50, 10, 20, 40]
Solution
Step 1: Insert 60 at the end
Array becomes [50, 30, 40, 10, 20, 60].
Step 2: Bubble up 60 to restore Max Heap
60 compares with parent 40; since 60 > 40, swap. New array: [50, 30, 60, 10, 20, 40]. Then 60 compares with parent 50; 60 > 50, swap again. Final array: [60, 30, 50, 10, 20, 40].
Final Answer:
[60, 30, 50, 10, 20, 40] -> Option D
Quick Check:
Insert at end + bubble up swaps = [60,30,50,10,20,40] [OK]
Hint: Insert at end, bubble up swaps until parent bigger in Max Heap [OK]
Common Mistakes:
Not bubbling up after insertion
Swapping with wrong parent index
Confusing Min Heap bubbling rules
4. The following code snippet tries to remove the root from a Min Heap but has a bug. What is the bug?
void removeRoot(int heap[], int *size) {
heap[0] = heap[*size - 1];
(*size)--;
int i = 0;
while (i < *size) {
int left = 2*i + 1;
int right = 2*i + 2;
int smallest = i;
if (left < *size && heap[left] < heap[smallest]) smallest = left;
if (right < *size && heap[right] < heap[smallest]) smallest = right;
if (smallest == i) break;
int temp = heap[i];
heap[i] = heap[smallest];
heap[smallest] = temp;
i = smallest;
}
}
medium
A. The code does not bubble down after replacing root
B. The loop condition should be while (left < *size) instead of i < *size
C. The size is not decremented before replacing root
D. The code swaps with the largest child instead of the smallest
Solution
Step 1: Analyze loop condition for bubbling down
The loop uses i < *size, but bubbling down requires checking if left child exists to continue.
Step 2: Correct loop condition
Loop should continue while left child index is within size, i.e., while (left < *size), to avoid invalid child access.
Final Answer:
The loop condition should be while (left < *size) instead of i < *size -> Option B
Quick Check:
Bubble down loop must check child existence [OK]
Hint: Bubble down loops check child index, not current index [OK]
Common Mistakes:
Using wrong loop condition causing out-of-bounds access
Not decrementing size before replacing root
Swapping with wrong child node
5. You want to efficiently find the median of a stream of numbers. Which heap combination is best and why?
hard
A. Use two heaps: Max Heap for lower half, Min Heap for upper half to balance values
B. Use one Max Heap only, because it keeps largest elements at root
C. Use one Min Heap only, because it keeps smallest elements at root
D. Use two Min Heaps to store all elements
Solution
Step 1: Understand median finding in a stream
Median splits data into two halves: lower and upper. We need quick access to max of lower half and min of upper half.
Step 2: Use two heaps for balance
Max Heap stores lower half (max at root), Min Heap stores upper half (min at root). Balancing sizes allows median calculation efficiently.
Final Answer:
Use two heaps: Max Heap for lower half, Min Heap for upper half to balance values -> Option A
Quick Check:
Median = balanced Max and Min Heaps [OK]
Hint: Median needs Max Heap lower half + Min Heap upper half [OK]
Common Mistakes:
Using only one heap losing half data info
Using two Min Heaps losing max info of lower half
Not balancing heap sizes for median
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.