Heap / Priority Queue

Keep a heap so the smallest or largest item is always ready to take in logarithmic time. The tell is 'the k largest', 'the k most frequent', or repeatedly taking the best next item, as in Kth Largest Element in an Array. It avoids sorting everything.

15 problems · 4 with a video · 3 to practice in an AI round

Heap / Priority Queue problems
#ProblemDifficultyVideoAI round
1Top K Frequent ElementsMediumAI round
2Kth Largest Element in an ArrayMedium—AI round
3Task SchedulerMedium—
4Sort Characters By FrequencyMedium—
5Reorganize StringMediumAI round
6K Closest Points to OriginMedium—
7Sort an ArrayMedium—
8Find K Pairs with Smallest SumsMedium—
9Last Stone WeightEasy
10Furthest Building You Can ReachMedium—
11Car PoolingMedium—
12Maximum Number of Events That Can Be AttendedMedium—
13Single-Threaded CPUMedium—
14Rearrange String k Distance ApartHard—
15Maximum Product After K IncrementsMedium

Practice in an AI round

Three of these are AI rounds: 30 minutes, an AI agent beside your code, then a scored report.

Practice Top K Frequent Elements in an AI round