Queue data structure supporting fast k-th largest element finding
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The queue is a fundamental data structure in computer science, often used to store data in a First-In-First-Out (FIFO) manner. While queues are traditionally used for sequential processing, there are scenarios where you need to access elements differently, such as finding the k-th largest element. In this article, we delve into how a queue can be augmented or combined with other data structures to efficiently retrieve the k-th largest element, which is particularly useful in priority-based applications.
Basic Concepts
What is a Queue?
A queue is a linear data structure that follows the FIFO principle. Key operations associated with a queue include:
- Enqueue: Add an element to the end of the queue.
- Dequeue: Remove the element from the front of the queue.
- Peek: View the front element without removing it.
Despite its simplicity, a standard queue does not support efficient retrieval of the k-th largest element, necessitating enhancements or the use of additional data structures.
K-th Largest Element Problem
Given a data stream of elements, you may want to find the k-th largest element in this data structure efficiently. This is a common problem in areas like real-time analytics, where quick decision-making based on data streaming is essential.
Enhanced Data Structures for K-th Largest Element
To efficiently find the k-th largest element within a queue, one can use various advanced data structures or algorithms. Below are some methods shown with examples.
Using a Min-Heap
One practical solution is to use a min-heap to store the k largest elements observed. Here’s a breakdown of how this works:
- Complexity: Both inserting into and removing from a min-heap of size k operate in time.
- Process:
- As elements are enqueued, each element is inserted into the min-heap.
- If the heap size exceeds k, the smallest element (root of the min-heap) is removed.
- At any point, the root of the min-heap represents the k-th largest element.
Example
Suppose you have a queue and you want to find the 3rd largest element as elements are enqueued:
- Initialize an empty min-heap with a maximum size of 3.
- As you enqueue each element:
- Add the element to the heap: `[5]`, `[5, 15]`, `[5, 10, 15]`.
- Upon adding 8, replace the root: `[8, 10, 15]`.
- Add 25: `[10, 15, 25]`.
- Add 2 and discard it as it’s smaller than the smallest in the heap.
- Complexity: Insertion, deletion, and searching operations in a balanced BST have a time complexity of .
- Process:
- Insert each enqueued element into the tree.
- Maintain an additional node attribute to count the size of the subtree to efficiently find the k-th largest element by simple indexed search.
- Benefits: This allows removing of elements when new data provides higher priority while preserving the state of current k elements under observation.
- Application: Particularly useful in scenarios where elements are dequeued as well as enqueued, requiring rebalancing of heaps dynamically.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.