Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:44:45+00:00 2026-06-04T20:44:45+00:00

Given the following problem: Store the largest 5000 numbers from a stream of numbers

  • 0

Given the following problem:

“Store the largest 5000 numbers from a stream of numbers”

The solution which springs to mind is a binary search tree maintaining a count of the number of nodes in the tree and a reference to the smallest node once the count reaches 5000. When the count reaches 5000, each new number to add can be compared to the smallest item in the tree. If greater, the new number can be added then the smallest removed and the new smallest calculated (which should be very simple already having the previous smallest).

My concern with this solution is that the binary tree is naturally going to get skewed (as I’m only deleting on one side).

Is there a way to solve this problem which won’t create a terribly skewed tree?

In case anyone wants it, I’ve included pseudo-code for my solution so far below:

process(number)
{
  if (count == 5000 && number > smallest.Value)
  {
    addNode( root, number)
    smallest = deleteNodeAndGetNewSmallest ( root, smallest)
  }
}

deleteNodeAndGetNewSmallest( lastSmallest)
{
  if ( lastSmallest has parent)
  {
    if ( lastSmallest has right child)
    {
      smallest = getMin(lastSmallest.right)
      lastSmallest.parent.right = lastSmallest.right
    }
    else
    {
      smallest = lastSmallest.parent
    }
  }
  else 
  {
    smallest = getMin(lastSmallest.right)
    root = lastSmallest.right
  }
  count--
  return smallest
}

getMin( node)
{
  if (node has left)
    return getMin(node.left)
  else
    return node
}

add(number)
{
  //standard implementation of add for BST
  count++
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:44:47+00:00Added an answer on June 4, 2026 at 8:44 pm

    The simplest solution for this is maintaining a min heap of max size 5000.

    • Every time a new number arrives – check if the heap is smaller then
      5000, if it is – add it.
    • If it is not – check if the minimum is smaller then the new
      element, and if it is, pop it out and insert the new element instead.
    • When you are done – you have a heap containing 5000 largest elements.

    This solution is O(nlogk) complexity, where n is the number of elements and k is the number of elements you need (5000 in your case).

    It can be done also in O(n) using selection algorithm – store all the elements, and then find the 5001th largest element, and return everything bigger than it. But it is harder to implement and for reasonable size input – might not be better. Also, if stream contains duplicates, more processing is needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following problem, given the following service contract, data contract, and service
Please help me out with an algorithm for the following problem - Given a
I have the following problem: I am given a tree with N apples, for
Given the following html table and script shown below I am having a problem
Problem Given the following two tables, I'd like to select all Ids for Posts
Problem One friend suggested an interesting problem. Given the following code: public class OuterClass
I have a problem with nested templates and their template specialization. Given the following
I'm simplifying a larger complex problem with the following... Given three arrays of integers,
I'm trying to solve the following problem: Given an input of, say, 0000000000000000 0011111111110000
Problem I need a key-value store that can store values of the following form:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.