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 8544785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:38:37+00:00 2026-06-11T12:38:37+00:00

This is an interview question. You need to design a queue which holds integer

  • 0

This is an interview question. You need to design a queue which holds integer values and has a getMedian() function that returns median element of current queue. You can use O(n) extra space.

Can getMedian() be implemented with time complexity < O(n)?

For Example:
When the queue has the following values (2, 1, 2, 2, 6, 4, 2, 5)
this method returns 2 and doesn’t remove that object.

  • 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-11T12:38:38+00:00Added an answer on June 11, 2026 at 12:38 pm

    The known implementation for your problem is as so:

    What you need to implement is 2 heaps, one will be a min-heap and the other a max-heap.
    Also you will need an integer to tell us the number of objects in your queue.

    The constraints for the heaps are as follows:
    1. The min-heap will have the larger objects of your queue
    2. The max-heap will have the smaller objects of your queue
    3. The max-heap will have the same or 1 more object than your min-heap

    That way, if you have an odd number of objects the median would be exactly the max object in your max-heap. If you have an even number of objects your median would then be the average of both roots of your heaps (max of max-heap, min of min-heap).

    It is important to notice that if your heaps become uneven, for instance if you will “pop” from a certain heap, you will need to remove from the other heap and move it. But thats not a problem as all you need to deal with is the roots of your heaps and nothing more.

    The time complexity of getMedian becomes O(1)

    Just found an article on the subject: link

    Answer to comment

    The max-heap holds the half smallest elements.
    When you add a new number to the queue, you first check what is the number of objects in the queue.
    if the number you are adding is an even number, it means it needs to be added to the max-heap as both queues are equal in size.
    You then see what the max in the max-heap is.
    If it is larger than ur number, you can just insert it into the max-heap.
    if it is smaller, meaning ur new number might be bigger than a number in the min-heap.
    so you see what the min in the min-heap is.
    if your number is smaller than the min, than you can insert it in the max-heap, if it is bigger, then u move the min in the min-heap to the max-heap, and insert your new number in the min-heap.
    If the number is an odd number, you need to add to the min-heap as the max-heap has one more, and so on..

    Its a bit complicated but if you still dont understand I dont mind psuedo coding it for you

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

Sidebar

Related Questions

This is an interview question. You need to design a stack which holds an
This is an interview question. I have K machines each of which is connected
Just finished reading this blog post: http://www.skorks.com/2010/03/an-interview-question-that-prints-out-its-own-source-code-in-ruby/ In it, the author argues the case
I was asked in an interview this question LinkedList A has {1,2,3,4,5,6} LinkedList B
This question was asked at interview.I need to have running total ( only using
I had a recursion interview question problem in Java,Need your help on this. Write
I was given this MySQL Interview Question, which made me disqualified for the job.
I can't figure this interview question. You have an array of integers. You need
I got this as an interview question ... infinite array which is sorted and
This is one of an interview question which I had recently. I would like

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.