Java design a Queue with O(1) lookup time of the Maximum element. You will implement this design using the ArrayDeque Class in Java. solve the problem as stated below:- (1) Here you will Maintain two Queues - a Main Queue and a Queue holding the Maximum value(s) from the Main Queue (AKA Max Queue). The Main Queue contains the elements. The Max Queue contains the elements with Maximum value. The Max Queue would have to be a double ended Queue as you would like to be able to remove elements from both ends. Example : Let’s say we have the following: We add an integer 1 into our Main Queue and I hope it is really obvious that when the Main Queue contains a single element, the Max Queue can be populated without confusion :) Main Queue: 1 << front of Queue Max Queue : 1 << front of Queue Now, let’s say we insert a 4 into the Main Queue. the Main Queue will look as follows: Main Queue: 4 → 1 << front of Queue In the Max Queue, we don’t need 1 anymore, since 1 can never be the Max of this Queue now. So we remove 1 and insert 4. Main Queue: 4 → 1 << front of Queue Max Queue : 4 << front of Queue Say we insert 2 into the Main Queue. We know 2 is not the Max, but it can be the Max if we de-Queue 1 and 4 from the Queue. So, we insert it onto the Max Queue: Main Queue: 2 → 4 → 1 << front of Queue Max Queue : 2 → 4 << front of Queue Further, If we insert a 3 into the Main Queue, we can get rid of the 2 from the Max Queue, because 2 can no longer be the Max of the Queue, even if 4 and 1 are de-Queued. In that case our Queues become: Main Queue: 3 → 2 → 4 → 1 << front of Queue Max Queue : 3 → 4 << front of Queue In the process of inserting 3, we removed elements from the back of the Max Queue until we found an element ≥ 3. This is because elements < 3 could never be Max after 3 is inserted. What I stated above is exactly the algorithm for inserting an element in the Max Queue. To lookup the Maximum Value (AKA Max), we just check the front of the Max Queue which ensures O(1) lookup time. While de-queuing elements, we check if they are equal to the front of the Max Queue, and if so, we de-Queue from the Max Queue too. For example, after de-queuing 1, lets say we want to de-Queue 4. We see that 4 is the front of the Max Queue, so we remove both the 4s. This does indeed make sense as 4 can no longer remain the Maximum after it is removed from the Main Queue. If the process described above is followed and you code up the example provided we end up with the complexity stated below.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Java

design a Queue with O(1) lookup time of

the Maximum element. You will implement this design using the ArrayDeque

Class in Java.

solve the problem as stated below:-

 (1)  Here you will Maintain two Queues - a Main Queue and a Queue holding the

Maximum value(s) from the Main Queue (AKA Max Queue). The Main Queue

contains the elements. The Max Queue contains the elements with Maximum

value. The Max Queue would have to be a double ended Queue as you would

like to be able to remove elements from both ends.

 

Example :

Let’s say we have the following:

We add an integer 1 into our Main Queue and I hope it is really obvious that

when the Main Queue contains a single element, the Max Queue can be populated without confusion :)

Main Queue: 1 << front of Queue

Max Queue : 1 << front of Queue

Now, let’s say we insert a 4 into the Main Queue. the Main Queue will look as

follows:

Main Queue: 4 → 1 << front of Queue

In the Max Queue, we don’t need 1 anymore, since 1 can never be the Max

of this Queue now. So we remove 1 and insert 4.

Main Queue: 4 → 1 << front of Queue

Max Queue : 4 << front of Queue

Say we insert 2 into the Main Queue. We know 2 is not the Max, but it can be

the Max if we de-Queue 1 and 4 from the Queue. So, we insert it onto the Max

Queue:

Main Queue: 2 → 4 → 1 << front of Queue

Max Queue : 2 → 4 << front of Queue

Further, If we insert a 3 into the Main Queue, we can get rid of the 2 from the

Max Queue, because 2 can no longer be the Max of the Queue, even if 4 and 1

are de-Queued. In that case our Queues become:

Main Queue: 3 → 2 → 4 → 1 << front of Queue

Max Queue : 3 → 4 << front of Queue

In the process of inserting 3, we removed elements from the back of the Max

Queue until we found an element ≥ 3.

This is because elements < 3 could never be Max after 3 is inserted. What I

stated above is exactly the algorithm for inserting an element in the Max Queue.

To lookup the Maximum Value (AKA Max), we just check the front of the Max

Queue which ensures O(1) lookup time.

While de-queuing elements, we check if they are equal to the front of the Max

Queue, and if so, we de-Queue from the Max Queue too. For example, after

de-queuing 1, lets say we want to de-Queue 4. We see that 4 is the front of the

Max Queue, so we remove both the 4s. This does indeed make sense as 4 can

no longer remain the Maximum after it is removed from the Main Queue.

If the process described above is followed and you code up the example provided

we end up with the complexity stated below.

 

***VERY VERY IMPORTANT***

Time Complexity of the Max Queue lookup: O(1)

Space Complexity on the Max Queue : O(n)

(1) Your code should be well commented which explains all the steps you are

performing to solve the problem AND mentioned Time and Space complexities

(2) As a comment in your code, please write your test-cases on how you would

test your solution assumptions and hence your code.

You will address your test cases in the form of code and not prose :

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY