📖 Scenario: Imagine you are organizing a priority queue for tasks in a to-do app. Each task has a priority number. You want to store these tasks so that the highest priority task is always easy to find.
🎯 Goal: You will create a simple heap structure using an array to understand how tasks are stored and how the heap properties keep the highest priority task at the top.
📋 What You'll Learn
Create an array called tasks with specific priority numbers
Create a variable called heapSize to store the number of tasks
Write a function called isHeapPropertyValid to check if the array follows max-heap property
Print the result of the heap property check
💡 Why This Matters
🌍 Real World
Heaps are used in task scheduling, priority queues, and efficient sorting algorithms like heap sort.
💼 Career
Understanding heap structure helps in roles involving algorithm optimization, system design, and software development.
Progress0 / 4 steps
1
Create the tasks array
Create an array called tasks with these exact priority numbers in order: 40, 30, 20, 15, 10, 12, 8.
DSA Javascript
Hint
Use square brackets [] to create an array and separate numbers with commas.
2
Set the heap size
Create a variable called heapSize and set it to the length of the tasks array.
DSA Javascript
Hint
Use array.length to get the number of items in an array.
3
Check the max-heap property
Write a function called isHeapPropertyValid that takes the tasks array and heapSize as parameters. It should return true if every parent node is greater than or equal to its children (max-heap property), otherwise false. Use a for loop with variable i from 0 to Math.floor(heapSize / 2) - 1 to check each parent node.
DSA Javascript
Hint
Remember, for each parent at index i, left child is at 2*i + 1 and right child is at 2*i + 2.
4
Print the heap property check result
Use console.log to print the result of calling isHeapPropertyValid(tasks, heapSize).
DSA Javascript
Hint
Use console.log to print the boolean result.
Practice
(1/5)
1. What is a key property of a max heap data structure?
easy
A. Every parent node is greater than or equal to its child nodes
B. Every parent node is less than or equal to its child nodes
C. All leaf nodes are greater than the root node
D. The tree is always a complete binary tree but unordered
Solution
Step 1: Understand max heap property
A max heap requires that each parent node is greater than or equal to its children.
Step 2: Compare options with max heap definition
Every parent node is greater than or equal to its child nodes matches the max heap property exactly, others describe min heap or incorrect properties.
Final Answer:
Every parent node is greater than or equal to its child nodes -> Option A
Quick Check:
Max heap = parent ≥ children [OK]
Hint: Max heap means parent is always bigger or equal [OK]
Common Mistakes:
Confusing max heap with min heap property
Thinking leaf nodes must be bigger than root
Ignoring the complete binary tree requirement
2. Which of the following is the correct way to represent a heap in JavaScript using an array?
easy
A. Parent at index i, left child at i + 1, right child at i + 2
B. Parent at index i, left child at 2*i + 1, right child at 2*i + 2
C. Parent at index i, left child at 2*i, right child at 2*i + 1
D. Parent at index i, left child at i - 1, right child at i - 2
Solution
Step 1: Recall array indexing for heap
In zero-based arrays, left child index = 2*i + 1, right child index = 2*i + 2.
Step 2: Match options with formula
Parent at index i, left child at 2*i + 1, right child at 2*i + 2 matches the correct formula for zero-based indexing heaps.
Final Answer:
Parent at index i, left child at 2*i + 1, right child at 2*i + 2 -> Option B
3. Consider the following JavaScript code that builds a max heap from an array:
const arr = [3, 9, 2, 1, 4, 5];
function maxHeapify(arr, i, n) {
let largest = i;
let left = 2 * i + 1;
let right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) largest = left;
if (right < n && arr[right] > arr[largest]) largest = right;
if (largest !== i) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
maxHeapify(arr, largest, n);
}
}
function buildMaxHeap(arr) {
let n = arr.length;
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
maxHeapify(arr, i, n);
}
}
buildMaxHeap(arr);
console.log(arr);
What is the output printed?
medium
A. [3, 9, 2, 1, 4, 5]
B. [9, 3, 5, 1, 4, 2]
C. [5, 4, 3, 1, 9, 2]
D. [9, 4, 5, 1, 3, 2]
Solution
Step 1: Understand buildMaxHeap process
Start heapifying from last non-leaf node at index 2 (value 2), then index 1 (value 9), then index 0 (value 3).
Parent index calculation should be Math.floor((i - 1) / 2) -> Option A
Quick Check:
Parent index = (i-1)//2 in zero-based heap [OK]
Hint: Parent index in zero-based heap = (i-1)//2 [OK]
Common Mistakes:
Using i/2 instead of (i-1)/2 for parent
Loop condition allowing i=0 to cause errors
Swapping when parent is larger, not smaller
5. You have a min heap represented as an array: [2, 5, 8, 10, 15, 20]. You want to insert the value 1 and maintain the min heap property. Which sequence of array states correctly shows the heap after each step of insertion and heapify up?
Compare 1 with parent at index 2 (value 8), swap: [2, 5, 1, 10, 15, 20, 8] Compare 1 with new parent at index 0 (value 2), swap: [1, 5, 2, 10, 15, 20, 8]