0
0
DSA Javascriptprogramming~20 mins

Median of Data Stream Using Two Heaps in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Median Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Median After Adding Numbers
What is the median after adding the numbers 5, 15, 1, 3 in order using two heaps?
DSA Javascript
class MedianFinder {
  constructor() {
    this.small = [] // max heap
    this.large = [] // min heap
  }

  addNum(num) {
    if (this.small.length === 0 || num <= -this.small[0]) {
      this.small.push(-num)
      this.small.sort((a, b) => a - b)
    } else {
      this.large.push(num)
      this.large.sort((a, b) => a - b)
    }

    if (this.small.length > this.large.length + 1) {
      this.large.push(-this.small.shift())
      this.large.sort((a, b) => a - b)
    } else if (this.large.length > this.small.length) {
      this.small.push(-this.large.shift())
      this.small.sort((a, b) => a - b)
    }
  }

  findMedian() {
    if (this.small.length > this.large.length) {
      return -this.small[0]
    } else {
      return (-this.small[0] + this.large[0]) / 2
    }
  }
}

const mf = new MedianFinder()
mf.addNum(5)
mf.addNum(15)
mf.addNum(1)
mf.addNum(3)
const median = mf.findMedian()
console.log(median)
A4
B3
C5
D6
Attempts:
2 left
💡 Hint
Think about how the two heaps balance the numbers and how median is calculated from their tops.
🧠 Conceptual
intermediate
1:30remaining
Why Use Two Heaps for Median of Data Stream?
Why do we use two heaps (a max heap and a min heap) to find the median in a data stream?
ATo store all numbers twice for redundancy.
BTo sort all numbers in ascending order efficiently.
CTo use one heap for odd numbers and one for even numbers.
DTo keep track of the lower half and upper half of numbers separately for quick median calculation.
Attempts:
2 left
💡 Hint
Think about how median relates to the middle values in a sorted list.
🔧 Debug
advanced
2:00remaining
Identify the Bug in MedianFinder Implementation
What error will this code produce when adding numbers and finding median?
DSA Javascript
class MedianFinder {
  constructor() {
    this.small = [] // max heap
    this.large = [] // min heap
  }

  addNum(num) {
    if (this.small.length === 0 || num <= -this.small[0]) {
      this.small.push(-num)
      this.small.sort((a, b) => a - b)
    } else {
      this.large.push(num)
      this.large.sort((a, b) => a - b)
    }

    if (this.small.length > this.large.length + 1) {
      this.large.push(-this.small.shift())
      this.large.sort((a, b) => a - b)
    } else if (this.large.length > this.small.length) {
      this.small.push(-this.large.shift())
      this.small.sort((a, b) => a - b)
    }
  }

  findMedian() {
    if (this.small.length > this.large.length) {
      return -this.small[0]
    } else {
      return (-this.small[0] + this.large[0]) / 2
    }
  }
}

const mf = new MedianFinder()
mf.addNum(1)
mf.addNum(2)
mf.addNum(3)
console.log(mf.findMedian())
AThe code runs correctly and outputs 2
BThe code outputs 1.5 instead of 2
CThe code throws a TypeError because shift() is used on a sorted array without heap structure
DThe code throws a SyntaxError due to missing semicolons
Attempts:
2 left
💡 Hint
Think about how shift() affects the heap property after sorting.
📝 Syntax
advanced
2:00remaining
Which Option Fixes the Heap Balancing Logic?
Given the buggy code below, which option correctly fixes the balancing logic to maintain heaps properly? class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort() } else { this.large.push(num) this.large.sort() } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort() } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort() } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } }
AReplace shift() with pop() and use a proper heap data structure instead of sorting arrays.
BRemove all sorting calls and rely on push() only.
CUse unshift() instead of push() to add elements to arrays.
DReplace push() with concat() to add elements.
Attempts:
2 left
💡 Hint
Think about how heaps work and how to maintain their properties.
🚀 Application
expert
2:30remaining
Calculate Median After Complex Stream Operations
Given the following sequence of numbers added to the MedianFinder: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, what is the median after all insertions?
DSA Javascript
class MedianFinder {
  constructor() {
    this.small = [] // max heap
    this.large = [] // min heap
  }

  addNum(num) {
    if (this.small.length === 0 || num <= -this.small[0]) {
      this.small.push(-num)
      this.small.sort((a, b) => a - b)
    } else {
      this.large.push(num)
      this.large.sort((a, b) => a - b)
    }

    if (this.small.length > this.large.length + 1) {
      this.large.push(-this.small.shift())
      this.large.sort((a, b) => a - b)
    } else if (this.large.length > this.small.length) {
      this.small.push(-this.large.shift())
      this.small.sort((a, b) => a - b)
    }
  }

  findMedian() {
    if (this.small.length > this.large.length) {
      return -this.small[0]
    } else {
      return (-this.small[0] + this.large[0]) / 2
    }
  }
}

const mf = new MedianFinder()
[10,20,30,40,50,60,70,80,90,100].forEach(n => mf.addNum(n))
console.log(mf.findMedian())
A60
B55
C50
D65
Attempts:
2 left
💡 Hint
Count the numbers and find the middle two values for even count.