BeFreed
    Categories>Technology>Real-Time Medians with Two Heaps: Efficient Streaming Data

    Real-Time Medians with Two Heaps: Efficient Streaming Data

    23 min
    |
    |
    May 13, 2026
    TechnologyEducationCareer

    Learn how to calculate real-time medians for streaming data using the two heaps method. Achieve O(1) time complexity for instant results in ad tech and analytics.

    Real-Time Medians with Two Heaps: Efficient Streaming Data

    Best quote from Real-Time Medians with Two Heaps: Efficient Streaming Data

    “

    By using two heaps working in tandem—a Max-Heap for the lower half and a Min-Heap for the upper half—you can achieve $O(\log n)$ for adding or deleting numbers and a staggering $O(1)$ for grabbing the median.

    ”

    This audio lesson was created by a BeFreed community member

    Input question

    Add, delete, get median int values methods in python 3. This is for a CodeSignal assessment.

    Host voices
    Niaplay
    Jacksonplay
    Learning style
    Deep
    Knowledge sources
    Loading...
    link
    https://codesignal.com/learn/courses/understanding-and-using-trees-in-python/lessons/solving-real-world-problems-with-heaps-in-python?courseSlug=understanding-and-using-trees-in-python
    Running Median in O(log n) time in Python
    link
    https://stackoverflow.com/questions/68949041/running-median-in-olog-n-time-in-python
    Median of K Sliding Windows (Extended Practice) | Webcoderspeed
    link
    https://webcoderspeed.com/blog/dsa-heaps/35-find-median-sliding-window-variant
    How can I delete element in log(n) from priority queue implemented through heapq in python?
    link
    https://stackoverflow.com/questions/25828788/how-can-i-delete-element-in-logn-from-priority-queue-implemented-through-heapq

    Frequently Asked Questions

    The most efficient approach for calculating real-time medians involves using two heaps working in tandem. This data structure strategy allows you to maintain a perfectly balanced center point as new data arrives. Unlike naive sorting methods that crawl to a stop as datasets grow, the two heaps method provides a way to handle high-frequency data streams, such as click-stream info in ad tech, with significantly better performance and algorithm optimization.

    Using two heaps transforms the performance of your application by offering O(log n) time complexity for adding or deleting numbers and a staggering O(1) complexity for retrieving the median. While a standard list requires an O(n log n) sorting cost that creates a performance wall, the two heaps approach ensures the answer is essentially ready instantly. This makes it an essential technique for acing a CodeSignal assessment or managing real-world streaming analytics.

    In an ad tech environment where data hits servers every millisecond, a naive approach of tossing numbers into a list and sorting them is a nightmare. As the volume of click-stream data increases, the computational cost of sorting causes the code to crawl to a stop. To handle a literal firehose of data effectively, developers must move away from constant sorting and utilize specialized data structures like heaps to maintain real-time responsiveness.

    Discover more

    Study Nate Silver's statistical methods
    LEARNING PLAN

    Study Nate Silver's statistical methods

    In an era of data overload, the ability to filter out noise is a critical competitive advantage. This plan is ideal for data analysts, political junkies, and sports bettors who want to adopt the rigorous, Bayesian-inspired framework that made Nate Silver a household name.

    3 h 49 m•4 Sections
    多维效能:全场景时间管理系统
    LEARNING PLAN

    多维效能:全场景时间管理系统

    在碎片化时代,传统时间管理已难以应对复杂的多任务挑战。本课程专为追求高产出、管理多重身份的职场精英设计,旨在通过精力管理与数字工具重塑个人的效能系统。

    1 h 36 m•4 Sections
    To upgrade speed reading and comprehension.
    LEARNING PLAN

    To upgrade speed reading and comprehension.

    In an age of information overload, the ability to process and retain complex data quickly is a critical competitive advantage. This plan is ideal for students, researchers, and professionals who need to master dense material without sacrificing comprehension.

    3 h 32 m•4 Sections
    Read more in less time
    LEARNING PLAN

    Read more in less time

    In an era of information overload, the ability to process and retain knowledge quickly is a competitive necessity. This plan is designed for professionals and lifelong learners who want to master speed reading, effective note-taking, and analytical strategies to maximize their intellectual ROI.

    4 h 6 m•4 Sections
    The Real-Time Human Decoder
    LEARNING PLAN

    The Real-Time Human Decoder

    In a world of filtered communication, the ability to read unspoken signals is a critical competitive advantage. This plan is designed for leaders, negotiators, and professionals who need to master micro-expressions and behavioral psychology to navigate complex social landscapes.

    1 h 24 m•4 Sections
    大腦的後台清理術
    LEARNING PLAN

    大腦的後台清理術

    在資訊超載的時代,許多人的大腦因背景程式過多而陷入慢性疲勞。本課程專為高壓工作者與容易焦慮的人士設計,旨在透過科學方法重整腦部資源分配。

    1 h 46 m•3 Sections
    Master Math & Fast Calculation Tricks
    LEARNING PLAN

    Master Math & Fast Calculation Tricks

    In a world driven by data, the ability to process numbers quickly and accurately is a vital competitive advantage. This plan is designed for students, professionals, and lifelong learners who want to eliminate math anxiety and master the art of rapid mental calculation.

    4 h 29 m•4 Sections
    To read faster in a time limit of 10 minutes
    LEARNING PLAN

    To read faster in a time limit of 10 minutes

    In an age of information overload, the ability to process text quickly is a vital competitive advantage for professionals and students alike. This plan is designed for anyone looking to save hours of study or work time by mastering the mechanics of rapid comprehension and memory retention.

    3 h 54 m•4 Sections

    From Columbia University alumni built in San Francisco

    BeFreed Brings Together A Global Community Of 1,000,000 Curious Minds
    See more on how BeFreed is discussed across the web

    "Instead of endless scrolling, I just hit play on BeFreed. It saves me so much time."

    @Moemenn
    platform
    star
    star
    star
    star
    star

    "I never knew where to start with nonfiction—BeFreed’s book lists turned into podcasts gave me a clear path."

    @Chloe, Solo founder, LA
    platform
    comments
    12
    likes
    117

    "Perfect balance between learning and entertainment. Finished ‘Thinking, Fast and Slow’ on my commute this week."

    @Raaaaaachelw
    platform
    star
    star
    star
    star
    star

    "Crazy how much I learned while walking the dog. BeFreed = small habits → big gains."

    @Matt, YC alum
    platform
    comments
    12
    likes
    108

    "Reading used to feel like a chore. Now it’s just part of my lifestyle."

    @Erin, Investment Banking Associate , NYC
    platform
    comments
    254
    likes
    17

    "Feels effortless compared to reading. I’ve finished 6 books this month already."

    @djmikemoore
    platform
    star
    star
    star
    star
    star

    "BeFreed turned my guilty doomscrolling into something that feels productive and inspiring."

    @Pitiful
    platform
    comments
    96
    likes
    4.5K

    "BeFreed turned my commute into learning time. 20-min podcasts are perfect for finishing books I never had time for."

    @SofiaP
    platform
    star
    star
    star
    star
    star

    "BeFreed replaced my podcast queue. Imagine Spotify for books — that’s it. 🙌"

    @Jaded_Falcon
    platform
    comments
    201
    thumbsUp
    16

    "It is great for me to learn something from the book without reading it."

    @OojasSalunke
    platform
    star
    star
    star
    star
    star

    "The themed book list podcasts help me connect ideas across authors—like a guided audio journey."

    @Leo, Law Student, UPenn
    platform
    comments
    37
    likes
    483

    "Makes me feel smarter every time before going to work"

    @Cashflowbubu
    platform
    star
    star
    star
    star
    star

    From Columbia University alumni built in San Francisco

    BeFreed Brings Together A Global Community Of 1,000,000 Curious Minds
    See more on how BeFreed is discussed across the web

    "Instead of endless scrolling, I just hit play on BeFreed. It saves me so much time."

    @Moemenn
    platform
    star
    star
    star
    star
    star

    "I never knew where to start with nonfiction—BeFreed’s book lists turned into podcasts gave me a clear path."

    @Chloe, Solo founder, LA
    platform
    comments
    12
    likes
    117

    "Perfect balance between learning and entertainment. Finished ‘Thinking, Fast and Slow’ on my commute this week."

    @Raaaaaachelw
    platform
    star
    star
    star
    star
    star

    "Crazy how much I learned while walking the dog. BeFreed = small habits → big gains."

    @Matt, YC alum
    platform
    comments
    12
    likes
    108

    "Reading used to feel like a chore. Now it’s just part of my lifestyle."

    @Erin, Investment Banking Associate , NYC
    platform
    comments
    254
    likes
    17

    "Feels effortless compared to reading. I’ve finished 6 books this month already."

    @djmikemoore
    platform
    star
    star
    star
    star
    star

    "BeFreed turned my guilty doomscrolling into something that feels productive and inspiring."

    @Pitiful
    platform
    comments
    96
    likes
    4.5K

    "BeFreed turned my commute into learning time. 20-min podcasts are perfect for finishing books I never had time for."

    @SofiaP
    platform
    star
    star
    star
    star
    star

    "BeFreed replaced my podcast queue. Imagine Spotify for books — that’s it. 🙌"

    @Jaded_Falcon
    platform
    comments
    201
    thumbsUp
    16

    "It is great for me to learn something from the book without reading it."

    @OojasSalunke
    platform
    star
    star
    star
    star
    star

    "The themed book list podcasts help me connect ideas across authors—like a guided audio journey."

    @Leo, Law Student, UPenn
    platform
    comments
    37
    likes
    483

    "Makes me feel smarter every time before going to work"

    @Cashflowbubu
    platform
    star
    star
    star
    star
    star

    "Instead of endless scrolling, I just hit play on BeFreed. It saves me so much time."

    @Moemenn
    platform
    star
    star
    star
    star
    star

    "I never knew where to start with nonfiction—BeFreed’s book lists turned into podcasts gave me a clear path."

    @Chloe, Solo founder, LA
    platform
    comments
    12
    likes
    117

    "Perfect balance between learning and entertainment. Finished ‘Thinking, Fast and Slow’ on my commute this week."

    @Raaaaaachelw
    platform
    star
    star
    star
    star
    star

    "Crazy how much I learned while walking the dog. BeFreed = small habits → big gains."

    @Matt, YC alum
    platform
    comments
    12
    likes
    108

    "Reading used to feel like a chore. Now it’s just part of my lifestyle."

    @Erin, Investment Banking Associate , NYC
    platform
    comments
    254
    likes
    17

    "Feels effortless compared to reading. I’ve finished 6 books this month already."

    @djmikemoore
    platform
    star
    star
    star
    star
    star

    "BeFreed turned my guilty doomscrolling into something that feels productive and inspiring."

    @Pitiful
    platform
    comments
    96
    likes
    4.5K

    "BeFreed turned my commute into learning time. 20-min podcasts are perfect for finishing books I never had time for."

    @SofiaP
    platform
    star
    star
    star
    star
    star

    "BeFreed replaced my podcast queue. Imagine Spotify for books — that’s it. 🙌"

    @Jaded_Falcon
    platform
    comments
    201
    thumbsUp
    16

    "It is great for me to learn something from the book without reading it."

    @OojasSalunke
    platform
    star
    star
    star
    star
    star

    "The themed book list podcasts help me connect ideas across authors—like a guided audio journey."

    @Leo, Law Student, UPenn
    platform
    comments
    37
    likes
    483

    "Makes me feel smarter every time before going to work"

    @Cashflowbubu
    platform
    star
    star
    star
    star
    star

    "Instead of endless scrolling, I just hit play on BeFreed. It saves me so much time."

    @Moemenn
    platform
    star
    star
    star
    star
    star

    "I never knew where to start with nonfiction—BeFreed’s book lists turned into podcasts gave me a clear path."

    @Chloe, Solo founder, LA
    platform
    comments
    12
    likes
    117

    "Perfect balance between learning and entertainment. Finished ‘Thinking, Fast and Slow’ on my commute this week."

    @Raaaaaachelw
    platform
    star
    star
    star
    star
    star

    "Crazy how much I learned while walking the dog. BeFreed = small habits → big gains."

    @Matt, YC alum
    platform
    comments
    12
    likes
    108

    "Reading used to feel like a chore. Now it’s just part of my lifestyle."

    @Erin, Investment Banking Associate , NYC
    platform
    comments
    254
    likes
    17

    "Feels effortless compared to reading. I’ve finished 6 books this month already."

    @djmikemoore
    platform
    star
    star
    star
    star
    star

    "BeFreed turned my guilty doomscrolling into something that feels productive and inspiring."

    @Pitiful
    platform
    comments
    96
    likes
    4.5K

    "BeFreed turned my commute into learning time. 20-min podcasts are perfect for finishing books I never had time for."

    @SofiaP
    platform
    star
    star
    star
    star
    star

    "BeFreed replaced my podcast queue. Imagine Spotify for books — that’s it. 🙌"

    @Jaded_Falcon
    platform
    comments
    201
    thumbsUp
    16

    "It is great for me to learn something from the book without reading it."

    @OojasSalunke
    platform
    star
    star
    star
    star
    star

    "The themed book list podcasts help me connect ideas across authors—like a guided audio journey."

    @Leo, Law Student, UPenn
    platform
    comments
    37
    likes
    483

    "Makes me feel smarter every time before going to work"

    @Cashflowbubu
    platform
    star
    star
    star
    star
    star
    1.5K Ratings4.7
    Start your learning journey, now
    BeFreed App
    BeFreed

    Learn Anything, Personalized

    DiscordLinkedIn
    Featured book summaries
    Crucial ConversationsThe Perfect MarriageInto the WildNever Split the DifferenceAttachedGood to GreatSay Nothing
    Trending categories
    Self HelpCommunication SkillRelationshipMindfulnessPhilosophyInspirationProductivity
    Celebrities' reading list
    Elon MuskCharlie KirkBill GatesSteve JobsAndrew HubermanJoe RoganJordan Peterson
    Award winning collection
    Pulitzer PrizeNational Book AwardGoodreads Choice AwardsNobel Prize in LiteratureNew York TimesCaldecott MedalNebula Award
    Featured Topics
    ManagementAmerican HistoryWarTradingStoicismAnxietySex
    Best books by Year
    2025 Best Non Fiction Books2024 Best Non Fiction Books2023 Best Non Fiction Books
    Featured authors
    Chimamanda Ngozi AdichieGeorge OrwellO. J. SimpsonBarbara O'NeillWinston ChurchillCharlie Kirk
    BeFreed vs other apps
    BeFreed vs. Other Book Summary AppsBeFreed vs. ElevenReaderBeFreed vs. ReadwiseBeFreed vs. Anki
    Learning tools
    Knowledge VisualizerAI Podcast Generator
    Information
    About Usarrow
    Pricingarrow
    FAQarrow
    Blogarrow
    Careerarrow
    Partnershipsarrow
    Ambassador Programarrow
    Directoryarrow
    BeFreed
    Try now
    © 2026 BeFreed
    Term of UsePrivacy Policy
    BeFreed

    Learn Anything, Personalized

    DiscordLinkedIn
    Featured book summaries
    Crucial ConversationsThe Perfect MarriageInto the WildNever Split the DifferenceAttachedGood to GreatSay Nothing
    Trending categories
    Self HelpCommunication SkillRelationshipMindfulnessPhilosophyInspirationProductivity
    Celebrities' reading list
    Elon MuskCharlie KirkBill GatesSteve JobsAndrew HubermanJoe RoganJordan Peterson
    Award winning collection
    Pulitzer PrizeNational Book AwardGoodreads Choice AwardsNobel Prize in LiteratureNew York TimesCaldecott MedalNebula Award
    Featured Topics
    ManagementAmerican HistoryWarTradingStoicismAnxietySex
    Best books by Year
    2025 Best Non Fiction Books2024 Best Non Fiction Books2023 Best Non Fiction Books
    Learning tools
    Knowledge VisualizerAI Podcast Generator
    Featured authors
    Chimamanda Ngozi AdichieGeorge OrwellO. J. SimpsonBarbara O'NeillWinston ChurchillCharlie Kirk
    BeFreed vs other apps
    BeFreed vs. Other Book Summary AppsBeFreed vs. ElevenReaderBeFreed vs. ReadwiseBeFreed vs. Anki
    Information
    About Usarrow
    Pricingarrow
    FAQarrow
    Blogarrow
    Careerarrow
    Partnershipsarrow
    Ambassador Programarrow
    Directoryarrow
    BeFreed
    Try now
    © 2026 BeFreed
    Term of UsePrivacy Policy

    Key Takeaways

    1

    Finding the Middle Ground in Real-Time

    0:00
    0:31
    1:03
    1:18
    1:32
    1:39
    1:50
    2:06
    2

    The Two-Heap Balancing Act

    2:38
    2:54
    3:06
    3:16
    3:31
    3:47
    4:06
    4:10
    4:33
    4:48
    4:58
    5:15
    3

    The Challenge of Direct Deletion

    5:34
    5:50
    6:09
    3:16
    6:32
    6:45
    7:04
    7:21
    7:32
    7:41
    7:53
    3:16
    8:14
    8:26
    4

    Mastering Lazy Deletion

    8:47
    9:05
    9:30
    9:39
    10:01
    10:16
    10:35
    3:16
    11:00
    11:14
    11:37
    11:51
    5

    The Python Implementation Drill

    12:08
    12:21
    12:39
    12:46
    13:02
    13:05
    10:35
    13:39
    13:56
    13:58
    10:01
    3:16
    14:41
    14:47
    6

    Avoiding Common Pitfalls

    15:07
    15:22
    15:38
    16:00
    16:20
    16:32
    16:50
    3:16
    17:18
    17:32
    17:46
    7

    Real-World and Interview Context

    17:54
    18:07
    18:25
    18:36
    18:53
    19:09
    19:22
    19:31
    9:30
    20:12
    8

    Practical Playbook for the Assessment

    20:19
    20:30
    20:39
    20:49
    21:03
    21:15
    21:28
    21:40
    21:52
    22:04
    22:13
    22:22
    9

    Closing Reflection

    22:36
    22:55
    23:09
    23:26
    23:34
    23:41

    More like this

    Optimizing Massive Cashflow Files book cover
    Optimizing Massive Cashflow Files
    Clunky spreadsheets slow down every team. Learn how to use change summaries to audit automated cleanups and reduce file size without losing data integrity.
    15 min
    Finding Bottlenecks With Data You Already Have book cover
    The goalLean Six SigmaToyota Production SystemGemba kaizen
    20 sources
    Finding Bottlenecks With Data You Already Have
    Stop buying new sensors to find production delays. Learn how to use existing ERP data and the Drum-Buffer-Rope method to clear WIP and fix your workflow.
    28 min
    Data Topology: Central vs. Edge Databases book cover
    SQLite at the Edge: When libSQL Beats Postgres (2026) | TechPlainedDesigning your SaaS Database for Scale with Postgres — Citus Docs 8.2 documentationMulti-Tenant Architecture: Database Per Tenant vs Shared Schema — Pros and Cons (2026) - DEV CommunityThe Multi-Tenant SaaS Architecture Decision: Cost & Engineering Hours Across 4 Patterns | Appycodes
    5 sources
    Data Topology: Central vs. Edge Databases
    Struggling with global latency? Explore how shifting from central databases to per-account edge files can collapse read paths and improve performance.
    24 min
    FIFO Queues: First In, First Out Explained book cover
    System Design Interview – An Insider's GuideBuilding MicroservicesDesigning Data-Intensive ApplicationsClean Architecture
    23 sources
    FIFO Queues: First In, First Out Explained
    Miles and Nia explore FIFO queues, from basic first-come-first-served principles to advanced topics like message durability, scaling patterns, and choosing the right queue technology for your system.
    27 min
    Mastering useMemo: Smart Caching Without the Overhead book cover
    Optimal IllusionsWriting High-Performance .NET CodeHow to Pass ExamsTwo Scoops of Django
    24 sources
    Mastering useMemo: Smart Caching Without the Overhead
    Learn to optimize React apps by caching expensive calculations like large list filters. Discover why overusing useMemo backfires and get a 3-step checklist to decide when to memoize.
    20 min
    Complejidad Algorítmica: El Coste Real de tu Código book cover
    Google Drive PDF
    1 source
    Complejidad Algorítmica: El Coste Real de tu Código
    Descubre por qué un programa rápido no siempre es eficiente y aprende a medir el uso de tiempo y memoria para diseñar software de alto nivel.
    27 min
    Getting Things Done book cover
    Getting Things Done
    David Allen
    Systematic approach to managing tasks and increasing productivity.
    9 min
    The Ultimate Retroactive Jealousy Cure book cover
    The Ultimate Retroactive Jealousy Cure
    Jeff Billings
    Overcome obsessive thoughts about your partner's past with a 12-step plan to rewire your mind and reclaim relationship peace.
    8 min