
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
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 :

Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

- 2) Question in Javaarrow_forwardWrite a Java program for a matrix class that can add and multiply arbitrary two dimensional arrays of integers. Textbook Project P-3.36, pp. 147 Implement Singly Linked List - use textbook Chapter 3.2 as an exaple. Write a main driver to test basic list implementations. Textbook reference : Data structures and algorithms in Java Micheal Goodricharrow_forwardWrite an abstract data type for a queue whose elements include both a 20-character string and an integer priority. This queue must have the following methods: enqueue, which takes a string and an integer as parameters; dequeue, which returns the string from the queue that has the highest priority; and empty. The queue is not to be maintained in priority order of its elements, so the dequeue operation must always search the whole queue.arrow_forward
- Note: javaarrow_forwardJava Implement Stack using Deque (doubly linked list) You must create an array and the user can insert elements into this array and can only access or remove the newly inserted element from the array. The array is executed using a doubly linked list. The following Project should have these classes: 1. Class Book: The main Node for the deque array where it should have the following attributes besides (next, prev nodes): a) Book Id b) Book Name c) Book Author 2. Class Booklists: Where all the main operations are done. You need to apply these following operations: 1) AddBook() [push(0) : The method Inserts the book object into deque Stack (form the last). 2) RemoveBook() [pop()] : This method extracts an object from the last of the Deque stack and it removes it. If such object does not exist, the method returns null.(from the last) 3) isEmpty() : Return True if deque stack is Empty else return False. 4) DisplayAlIBooks() : Print all the books in the deque stack. 5) getlistsize(): Return…arrow_forwardProgram in C++arrow_forward
- Assume class MyStack implements the following StackGen interface. For this question, make no assumptions about the implementation of MyStack except that the following interface methods are implemented and work as documented. Write a public instance method for MyStack, called interchange(T element) to replace the bottom "two" items in the stack with element. If there are fewer than two items on the stack, upon return the stack should contain exactly two items that are element.arrow_forwardDesign an implementation of an Abstract Data Type consisting of a set with the following operations: insert(S, x) Insert x into the set S. delete(S, x) Delete x fromthe set S. member(S, x) Return true if x ∈ S, false otherwise. position(S, x) Return the number of elements of S less than x. concatenate(S, T) Set S to the union of S and T, assuming every element in S is smaller than every element of T. All operations involving n-element sets are to take time O(log n).arrow_forwardDraw a memory map for the code you see on the next page, until the execution reaches the point indicated by the comment /* HERE */.In your diagram:• You must have a stack, heap, and static memory sections • Identify each frame as illustrated by the previous examples.• Draw your variables as they are encountered during program execution. Code: public class Passenger {private String name;private int ticketCost;private StringBuffer luggage;private static final int LUGGAGE_COST = 20;public Passenger(String name, int ticketCost) {this.name = name;this.ticketCost = ticketCost;luggage = new StringBuffer();}public Passenger addLuggage(String desc) {ticketCost += LUGGAGE_COST;luggage.append(desc);return this;}public int getTicketCost() {return ticketCost;}public Passenger reduceCost(int by) {ticketCost -= by;/* HERE */return this;}public String toString() {return "Passenger [name=" + name + ", ticketCost=" + ticketCost + ", luggage=" + luggage + "]";}}public class Driver {public static void…arrow_forward
- in java pls and thank you!arrow_forwardIn Java. The following is a class definition of a linked list Node:class Node{int info;Node next;}Show the instructions required to create a linked list that is referenced by head and stores in order, the int values 13, 6 and 2. Assume that Node's constructor receives no parameters.arrow_forwardJava programming homework please helparrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





