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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:38:41+00:00 2026-06-17T08:38:41+00:00

The task is to implement a queue in java with the following methods: enqueue

  • 0

The task is to implement a queue in java with the following methods:

  1. enqueue //add an element to queue
  2. dequeue //remove element from queue
  3. peekMedian //find median
  4. peekMinimum //find minimum
  5. peakMaximum //find maximum
  6. size // get size

Assume that ALL METHODS ARE CALLED In EQUAL FREQUENCY, the task is to have the fastest implementation.

My Current Approach:
Maintain a sorted array, in addition to the queue, so enqueue and dequeue are take O(logn) and peekMedian, peekMaximum, peekMinimum all take O(1) time.

Please suggest a method that will be faster, assuming all methods are called in equal frequency.

  • 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-17T08:38:42+00:00Added an answer on June 17, 2026 at 8:38 am

    Well, you are close – but there is still something missing, since inserting/deleting from a sorted array is O(n) (because at probability 1/2 the inserted element is at the first half of the array, and you will have to shift to the right all the following elements, and there are at least n/2 of these, so total complexity of this operation is O(n) on average + worst case)

    However, if you switch your sorted DS to a skip list/ balanced BST – you are going to get O(logn) insertion/deletion and O(1) minimum/maximum/median/size (with caching)

    EDIT:

    You cannot get better then O(logN) for insertion (unless you decrease the peekMedian() to Omega(logN)), because that will enable you to sort better then O(NlogN):

    First, note that the median moves one element to the right for each "high" elements you insert (in here, high means >= the current max).

    So, by iteratively doing:

    while peekMedian() != MAX:
       peekMedian()
       insert(MAX)
       insert(MAX)
    

    you can find the "higher" half of the sorted array.

    Using the same approach with insert(MIN) you can get the lowest half of the array.

    Assuming you have o(logN) (small o notation, better then Theta(logN) insertion and O(1) peekMedian(), you got yourself a sort better then O(NlogN), but sorting is Omega(NlogN) problem.

    =><=

    Thus insert() cannot be better then O(logN), with median still being O(1).

    QED

    EDIT2: Modifying the median in insertions:

    If the tree size before insertion is 2n+1 (odd) then the old median is at index n+1, and the new median is at the same index (n+1), so if the element was added before the old median – you need to get the preceding node of the last median – and that’s the new median. If it was added after it – do nothing, the old median is the new one as well.

    If the list is even (2n elements), then after the insertion, you should increase an index (from n to n+1), so if the new element was added before the median – do nothing, if it was added after the old median, you need to set the new median as the following node from the old median.

    note: In here next nodes and preceding nodes are those that follow according to the key, and index means the "place" of the node (smallest is 1st and biggest is last).

    I only explained how to do it for insertion, the same ideas hold for deletion.

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

Sidebar

Related Questions

I am trying to implement Task Queues in GAE. The documentation shows: Queue queue
I try to implement blocking queue. the main parts are the following (it's a
I'm trying to implement a simple queue that performs one task at a time.
I'm trying to implement a simple queue that performs one task at a time.
I am trying to implement some kind of background task queue in Django, because
I am trying to implement a thread pool that processes a task queue using
Assume that you're working a x86 32-bits system. Your task is to implement the
I am trying to implement a gradle task to dynamically create a buildsignature.properties file
I am trying to implement a rake task that will run once a month
I am trying to implement some kind of new Linux task scheduler so I

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.