Bird
Raised Fist0
DSA Javascriptprogramming~10 mins

Why Heap Exists and What Sorted Array Cannot Do Efficiently in DSA Javascript - Test Your Knowledge

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a new element into a max heap.

DSA Javascript
function insert(heap, value) {
  heap.push(value);
  let index = heap.length - 1;
  while (index > 0) {
    let parent = Math.floor((index - 1) / [1]);
    if (heap[parent] >= heap[index]) break;
    [heap[parent], heap[index]] = [heap[index], heap[parent]];
    index = parent;
  }
}
Drag options to blanks, or click blank then click option'
A3
B2
C1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect divisor for parent calculation.
Not using Math.floor which causes wrong parent index.
2fill in blank
medium

Complete the code to extract the maximum element from a max heap.

DSA Javascript
function extractMax(heap) {
  if (heap.length === 0) return null;
  const max = heap[0];
  heap[0] = heap.pop();
  let index = 0;
  while (true) {
    let left = 2 * index + 1;
    let right = 2 * index + 2;
    let largest = index;
    if (left < heap.length && heap[left] > heap[[1]]) largest = left;
    if (right < heap.length && heap[right] > heap[largest]) largest = right;
    if (largest === index) break;
    [heap[index], heap[largest]] = [heap[largest], heap[index]];
    index = largest;
  }
  return max;
}
Drag options to blanks, or click blank then click option'
Alargest
Bleft
Cright
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing left child with right child instead of current node.
Using wrong variable for comparison.
3fill in blank
hard

Fix the error in the code that checks if a sorted array can efficiently insert a new element.

DSA Javascript
function insertSorted(arr, value) {
  let i = arr.length - 1;
  while (i >= 0 && arr[i] > [1]) {
    arr[i + 1] = arr[i];
    i--;
  }
  arr[i + 1] = value;
}
Drag options to blanks, or click blank then click option'
Avalue
Barr[i]
Ci
Darr[i + 1]
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[i] with itself or wrong variable.
Using index variable instead of value.
4fill in blank
hard

Fill both blanks to create a function that checks if a sorted array contains a value using binary search.

DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let mid = Math.floor((left + right) [1] 2);
    if (arr[mid] === target) return true;
    else if (arr[mid] [2] target) left = mid + 1;
    else right = mid - 1;
  }
  return false;
}
Drag options to blanks, or click blank then click option'
A+
B<
C/
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of division for mid calculation.
Incorrect comparison operators.
5fill in blank
hard

Fill all three blanks to create a function that builds a max heap from an unsorted array.

DSA Javascript
function heapify(arr, n, i) {
  let largest = i;
  let left = 2 * i + 1;
  let right = 2 * i + 2;

  if (left < n && arr[left] [1] arr[largest]) largest = left;
  if (right < n && arr[right] [2] arr[largest]) largest = right;

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, [3]);
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
Ci
Dlargest
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for max heap comparison.
Recursive call with wrong index.

Practice

(1/5)
1. Why do we use a heap instead of just a sorted array when we want to repeatedly access the smallest element?
easy
A. Because heaps allow faster insertion and deletion of the smallest element than sorted arrays.
B. Because heaps store elements in sorted order all the time.
C. Because sorted arrays use less memory than heaps.
D. Because heaps are easier to implement than sorted arrays.

Solution

  1. Step 1: Understand operations on sorted arrays

    Inserting or deleting elements in a sorted array requires shifting elements, which takes linear time (O(n)).
  2. Step 2: Compare with heap operations

    Heaps allow insertion and deletion of the smallest element in logarithmic time (O(log n)) by maintaining a special tree structure.
  3. Final Answer:

    Because heaps allow faster insertion and deletion of the smallest element than sorted arrays. -> Option A
  4. Quick Check:

    Heap operations faster than sorted array for insert/delete [OK]
Hint: Heaps speed up insert/delete of min element vs sorted arrays [OK]
Common Mistakes:
  • Thinking heaps keep all elements fully sorted
  • Assuming sorted arrays have faster insertions
  • Confusing memory usage with operation speed
2. Which of the following is the correct way to insert an element into a min-heap represented as an array in JavaScript?
easy
A. Replace the root with the new element and heapify down.
B. Insert element at the start and sort the array.
C. Add element at the end and do nothing else.
D. Add element at the end, then bubble it up to maintain heap property.

Solution

  1. Step 1: Recall min-heap insertion process

    Insert the new element at the end of the array to keep the complete tree shape.
  2. Step 2: Restore heap property by bubbling up

    Compare the new element with its parent and swap if smaller, repeat until heap property is restored.
  3. Final Answer:

    Add element at the end, then bubble it up to maintain heap property. -> Option D
  4. Quick Check:

    Insert by adding end + bubble up [OK]
Hint: Insert at end, then bubble up to fix heap [OK]
Common Mistakes:
  • Sorting the whole array after insertion
  • Not bubbling up after insertion
  • Replacing root directly without insertion
3. Consider this JavaScript code using a min-heap array representation:
const heap = [2, 5, 3, 10, 7];
// Insert 1 into heap
heap.push(1);
// Bubble up process
let i = heap.length - 1;
while (i > 0) {
  let parent = Math.floor((i - 1) / 2);
  if (heap[i] < heap[parent]) {
    [heap[i], heap[parent]] = [heap[parent], heap[i]];
    i = parent;
  } else {
    break;
  }
}
console.log(heap);
What is the output after insertion and bubbling up?
medium
A. [1, 2, 3, 10, 7, 5]
B. [1, 5, 3, 10, 7, 2]
C. [1, 5, 2, 10, 7, 3]
D. [1, 3, 2, 10, 7, 5]

Solution

  1. Step 1: Insert 1 at the end

    Initial heap: [2, 5, 3, 10, 7] After push: [2, 5, 3, 10, 7, 1]
  2. Step 2: Bubble up 1 to restore heap

    Index i=5, parent = 2 heap[5]=1 < heap[2]=3, swap -> [2, 5, 1, 10, 7, 3] Now i=2, parent=0 heap[2]=1 < heap[0]=2, swap -> [1, 5, 2, 10, 7, 3] Now i=0, stop.
  3. Final Answer:

    [1, 5, 2, 10, 7, 3] -> Option C
  4. Quick Check:

    Bubble up swaps 1 up to root [OK]
Hint: Bubble up swaps until parent smaller or root reached [OK]
Common Mistakes:
  • Swapping with wrong parent index
  • Stopping bubble up too early
  • Incorrect array after swaps
4. The following code tries to remove the smallest element from a min-heap represented as an array. What is the bug?
function removeMin(heap) {
  if (heap.length === 0) return null;
  const min = heap[0];
  heap[0] = heap.pop();
  let i = 0;
  while (true) {
    let left = 2 * i + 1;
    let right = 2 * i + 2;
    let smallest = i;
    if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
    if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
    if (smallest === i) break;
    [heap[i], heap[smallest]] = [heap[smallest], heap[i]];
    i = smallest;
  }
  return min;
}
medium
A. It does not handle the case when heap has only one element.
B. It does not handle empty heap correctly.
C. It does not check if heap.pop() returns undefined.
D. It does not update the index i correctly after swapping.

Solution

  1. Step 1: Analyze removal when heap has one element

    If heap has one element, heap.pop() removes it, then heap[0] = undefined sets root to undefined, causing invalid heap state.
  2. Step 2: Check code for single element case

    Code does not handle this special case; it should return the popped element directly without reassigning heap[0].
  3. Final Answer:

    It does not handle the case when heap has only one element. -> Option A
  4. Quick Check:

    Single element removal needs special handling [OK]
Hint: Check single-element heap removal edge case [OK]
Common Mistakes:
  • Ignoring single-element heap case
  • Assuming pop always returns a value
  • Not updating index after swap (actually correct here)
5. You want to maintain a collection of numbers where you frequently insert new numbers and also frequently remove the smallest number. Why is a heap better than a sorted array for this use case, and what is the time complexity difference?
hard
A. Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for insert and O(1) for remove-min.
B. Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for both insert and remove-min.
C. Heap allows insert and remove-min in O(1), while sorted array requires O(n) for insert and remove-min.
D. Heap allows insert and remove-min in O(n), while sorted array requires O(log n) for insert and remove-min.

Solution

  1. Step 1: Analyze heap operation times

    Insertion and removal of smallest element in a heap both take O(log n) time due to bubbling up/down.
  2. Step 2: Analyze sorted array operation times

    Insertion requires shifting elements to keep sorted order, O(n). Removing smallest (first element) requires shifting all remaining elements left, O(n).
  3. Final Answer:

    Heap allows insert and remove-min in O(log n), while sorted array requires O(n) for both insert and remove-min. -> Option B
  4. Quick Check:

    Heap: O(log n) insert/remove, Sorted array: O(n) insert/remove [OK]
Hint: Heap: O(log n) insert/remove; sorted array O(n) both [OK]
Common Mistakes:
  • Thinking sorted array remove-min is O(1)
  • Assuming heap insert/remove is O(1)
  • Confusing insert and remove complexities
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard