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

  • Home
  • SEARCH
  • 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 8320327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:37:12+00:00 2026-06-08T22:37:12+00:00

I have a stream (whose length I do not know, theoretically may be infinity).

  • 0

I have a stream (whose length I do not know, theoretically may be infinity).

I read the stream’s elements one by one.

Every time an element is read from the stream, I want to be able to return the kth greatest element read so far.

(Ideally for me it would be a code in python and/or lisp/scheme).

K is read at the beginning, and K can be a NUMBER (3rd, 4th), or can be a PROCENT (K % of the total nr. of elements read so far). If K=1/2, that means to extract the median element each time… For example, after reading Nth element, it must return N/2 th greatest element

example K=1/2:

3 -> 3
3,4 -> 3
3,4,2 -> 3
3,4,2,1 -> 2
etc.

I think this example is enough to clarify the question. I need minimal possible time to extract the Kth element. (this supposes read the stream in O(1), then insert the read value , then extract the Kth element).

I want any solution better than O(n).

  • 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-08T22:37:13+00:00Added an answer on June 8, 2026 at 10:37 pm

    So, since you need k-th element and k is known before running the algorithm, first observation you need to store at most k elements, k smallest elements.
    When you read new element you need to insert element in some datastructure keeping it’s properties and having opportunity to retrieve the answer quickly.
    1) You may use max-heap having at most k elements. Read element insert into heap (log(k)), then if you have more then k elements (k+1 to be precise) you need to extract_max O(log(k)) to extract and rebuild and the answer will be on the top of heap access O(1).
    So, each time it takes log(k) to get k-th element, in total for all elements – n * log(k).

    2) In case of using percentage, place of element will be calculated dynamically depending of how many elements were processed, here you may use order statistics tree, http://en.wikipedia.org/wiki/Order_statistic_tree with the same log(amount of elements) insertion and log(amount of elements) lookup.

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

Sidebar

Related Questions

I have a byte stream that may be UTF-8 data or it may be
I have a ASCII file where every line contains a record of variable length.
We have a whole catalogue of videos which we would like to stream to
I have a stream of JSON coming back and am building a table dynamically
I have response stream from a ftp web request that returns binary file. I
I have a stream of data that I want to place into a container.
I have a stream of bytes which contains a flag which identifies the endianness
I have a stream of data coming in over the serial line from an
In a project I have a stream for users, everyone can share and comment
I have a JSON stream being generated by a server side C++ program that

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.