2

I have a final exam coming up in a few weeks, and one of our practice questions is this:

Given a queue of N integers, find the minimum value in the queue and delete it from the queue. When you finish, the rest of the values must be in their original order. You can only use queue operations, i.e., you do not have access to the underlying storage in an array or linked list. Describe the most time-efficient way to implement this operation and give the order (Big O) in terms of N.

I'm pretty lost as to how I'm going to go about this.

EDIT: Queue operations are "Enqueue", "Dequeue", "isFull", "isEmpty", and if it's a circular queue, "front" and "back".

6
  • so, what are "queue operations" by the definition of your exam? Commented Aug 6, 2017 at 22:06
  • 2
    How about popping everything and the push it back to the queue except the minimal element? Commented Aug 6, 2017 at 22:07
  • This isn't really a programming question per-se. You'll have better luck asking the guys at Computer Science S.E. instead. Commented Aug 6, 2017 at 22:07
  • Oh ok, thanks stybl! I wasn't sure if it would be better to ask you guys or not! Commented Aug 6, 2017 at 22:11
  • 2
    @Jake This sort of question is perfectly fine here at Stack Overflow. Commented Aug 6, 2017 at 22:15

1 Answer 1

3

If you have a queue and are only permitted to do enqueues and dequeues on it, then the only way you can find an arbitrary value in the queue is to perform as many dequeues are necessary to find that particular value. If you don't know where in the queue that value is, then in the worst case you might have to inspect all n elements of the queue, which takes Θ(n) time. So in that sense, any solution we come up with has to always do at least Θ(n) work in the worst case.

One way that you can go about doing this is to imagine treating the queue as if it's a ring of elements with a cursor in some position indicating the front. If you dequeue an element and then immediately re-enqueue it, it's as if you've moved the cursor one spot clockwise (or counterclockwise - this is just a metaphor, after all). So you could consider solving this problem by moving the cursor around the queue until you find the element to delete, then dequeue it without re-enqueuing it, then moving the cursor around to back where you started. Here's one way to do this:

/* Store the number of elements in the queue, since it will change as
 * the loop runs. We want to visit each element exactly once.
 */
int numElems = queue.size();
for (int i = 0; i < numElems; i++) {
    ValueType elem = queue.dequeueMin();

    /* Remove the element if it matches the thing to get rid of, or,
     * equivalently, preserve the element if it isn't.
     */
    if (elem != theThingToRemove) {
        queue.enqueue(elem);
    }
}

This algorithm always does Θ(n) work - the loop visits each element exactly once and does O(1) total work - and O(1) total queue operations - per element.

Fun fact: this metaphor for a queue is exactly the idea behind a circular buffer, which is often used to implement fixed-sized queues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.