Kth Largest Number in a Stream Problem Statement

Design a data structure that can handle an infinite stream of numbers and efficiently return the k-th largest number at any given time.

Explanation:

You will be provided with a stream of numbers where the total count is unknown. Your task is to implement a system that consistently determines the k-th largest number from this stream.

The data structure should support the following operations:

  • add(DATA): Accepts an integer which is added to the current set of numbers.
  • int getKthLargest(): Returns the k-th largest number from the current set of numbers.

Queries will be of two types:

  • Type 1: Insert a number into the data structure. Format: 1 val
  • Type 2: Query the k-th largest number. Format: 2

Input:

The first line contains two space-separated integers 'Q' and 'K', where Q is the number of queries, and K is the rank of the largest number to find.
The second line includes K space-separated integers that form the initial pool.
Each of the next Q lines contains a query as per the mentioned formats.

Output:

For queries of type 2, print the k-th largest integer.
Each result should be printed on a separate line.

Example:

Input:
Q = 5, K = 2
Initial Pool: [4 5]
Queries:
2
1 6
2
1 3
2
Output:
4
5
5

Constraints:

  • 1 ≤ Q ≤ 104
  • 1 ≤ K ≤ 105
  • 1 ≤ QUERYTYPE ≤ 2
  • 1 ≤ DATA ≤ 109

Note: You do not need to handle output directly; focus on implementing the required methods to achieve the desired results.

AnswerBot
2d

Design a data structure to efficiently return the k-th largest number from an infinite stream of numbers.

  • Implement a max heap to store the numbers in the stream.

  • Keep the heap size limited to k to effi...read more

Help your peers!
Add answer anonymously...
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter