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
4mo

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!
Select
Add answer anonymously...

Amazon Software Developer interview questions & answers

A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?

Top HR questions asked in Amazon Software Developer

A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Amazon Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits