Heap•Pattern: Min-Heap•Medium

Kth largest element in a stream

Problem Intuition & Objective

Design a class that finds the kth largest element after every add().

Time ComplexityO(log k) per add
Optimal
Space ComplexityO(k)
Memory Bounds
Optimal Algorithmic Approach

Min-heap of size k: insert and possibly evict O(log k) per add.

Core Invariants & Edge Cases

  • Maintain a min-heap of size k
  • The heap root is always the kth largest element
  • If heap grows beyond k, pop the smallest
Previous ProblemWord Search in a 2D gridNext Problem Find K closest points to the origin

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.