Java Priority Queue With Linked Lists - java

I have been trying to figure out a question on a recent assignment for a few days now, and I can't seem to wrap my head around it. The question reads as follows:
Create a PriorityQueue class that contains two fields noOfPriorities
and a LinkedList… It should have one constructor that takes in an int
value assign that value to the noOfPriorities… at the same time add as
many LinkedLists as numberOfPriorities.. Enqueue method that takes in
a priority and an object.. Dequeue method that returns the next
priority element… and remove it from the list…
A large part of my problem is that I can't determine exactly what the professor is looking for because the wording seems a bit weird to me... simply asking about it yielded no help either.
Just to clarify, I'm not looking for anyone to give me the answer. I'm simply looking for a push in the right direction. If anyone could help It would be greatly appreciated.

Cheers on being honest about this being homework.
I think you can understand the problem better if you read up on what a priority queue is.
Let's take a small example. You have a few tasks to do, and each task has a priority.
Pri 1 - breathe, eat, sleep
Pri 2 - study, play
Pri 3 - watch a movie
All the above information can be handled by your PriorityQueue. You have 3 kinds of priorities, so you have 3 lists. Each list is to maintain tasks with the same priority.
Once you construct the empty PriorityQueue by calling PriorityQueue(3), you can add tasks to it.
Let's say you want to add the task "study" that has priority 2.
You can say, priorityQueue.enqueue(2, "study"). You would then go to the list that maintains priority 2 items, and add the task "study" to that list.
Similarly, when you want to find out what the next priority 3 item is, you can say, priorityQueue.dequeue(3). You would then find the list that handles priority 3 items, and remove the last element from that list.
This should give you a good understanding to start working. :)

Agreed, the assignment's badly worded.
at the same time add as many LinkedLists as numberOfPriorities..
This should probably be "at the same time add as many nodes to the LinkedList as numberOfPriorities.."
The next question to ask yourself is, just what type of thing should I store in all these linked nodes...?

I think you'd need an array of linked lists instead of just one. The problem description is contradictory, saying that you need both a class that has one linked list, and to create a number of them when you construct an object.
Here's a constructor for your class:
MyPriorityQueue(int npriorities)
{
noOfPriorities = npriorities;
queueArray = new ArrayList<List<T>>();
for (int i = 0; i < npriorities; ++i) queueArray.add(new LinkedList<T>());
}
You'd then have a mapping of priorities to queues. Your enqueue method would take an object and a priority (an int representing a priority) and would add the object to the queue specified by the priority. Your dequeue method would simply return the end of the highest priority queue with an element in it.
Make any sense?

Related

Does a partial traversal of a linked-list count as "one pass" of the list?

I've been going through algorithm challenges on LeetCode and just completed "Remove Nth Node From End of List".
Many of the top answers claimed to have found a "one pass" solution and I've included a Java example below.
Please could someone explain why "while(n-->0) h2=h2.next;" doesn't count as an extra pass of the linked list and, therefore, make this a "two pass" solution?
public ListNode RemoveNthFromEnd(ListNode head, int n) {
ListNode h1=head, h2=head;
while(n-->0) h2=h2.next;
if(h2==null)return head.next; // The head need to be removed, do it.
h2=h2.next;
while(h2!=null){
h1=h1.next;
h2=h2.next;
}
h1.next=h1.next.next; // the one after the h1 need to be removed
return head;
}
I've looked in the comments to this and other solutions and couldn't find an answer. Equally, a general Google search didn't yield an explanation.
Thanks in advance.
No, it's not one-pass. One-pass is defined with respect to a sequential I/O mechanism (canonically a tape) and means that each piece of data is read at most once, in order. Analogizing the linked list to the tape here, this algorithm is not one-pass because in general, some node will have its next field read once by h2=h2.next (in either loop) and again by h1=h1.next in the second loop.
The algorithm is not single pass, but not because of the first loop.
The first loop performs a partial pass on n elements.
The second loop performs two simultaneous partial passes on l-n elements (that on h2 being complementary to that in the first loop). In total, 2l-n lookups of next fields.
A single-pass solution can be implemented with the help of a FIFO queue of length n, but this is "hiding" a partial pass.

ArrayList Sorting Based on Method

I am in need of some help trying to compare 2 arraylist and then sorting; If I am even on the right track. So here is my problem..
Say arraylist 1 contains the objects in which 3 integers adds up to 4 and each integer has to be at least greater or greater/equal to the next number.
So for example, arraylist 1 contains {(2,1,1), (2,2,0), (3,1,0), (4,0,0)}.
Also each object integers are sorted from greatest to least.
Now I have a take the (2,1,1) and send it to a method to perform an algorithm on it. for each integer place, i need to add 2 to that integer place, and subtract 1 from the rest. We can call these A event, B event, or C Event.
for example, (2,1,1) which these events would be
A Event: (4,0,0)
B Event: (3,1,0)
C Event: (3,1,0)
Now, my question is because A event produced (4,0,0) how would I sort the first arraylist to have that number come next and then have (3,1,0) but without (3,1,0) duplicating. So after sorting the array, it should be
(2,1,1) ( 4,0,0) (3,1,0) (2,2,0)
At present your question is not clear to me, but I think I can help point you in the right direction.
Since you said:
without (3,1,0) duplicating
I'd suggest that to ensure uniqueness of objects in a Collection you should consider using a class that implements the Set interface (see Javadoc for Set for details).
Assuming you are not writing the sorting algorithm yourself you could use a SortedSet and then implement the necessary functionality (Comparable interface or Comparator) so that your objects are sorted in the way you want (the earlier link provides links that describe how to do this).
Hope this helps, and if you could try to make the question more clear I'd be happy to offer some additional pointers.
Here are some of the confusing parts that you could work on:
each integer has to be at least greater or greater/equal to the next number.
This is confusing because you're giving two conflicting requirements. Is each number strictly greater than the next or is it greater than or equal to the next?
Also each object integers are sorted from greatest to least.
I'm not sure what you mean by this because you already said that the integers contained in the objects were in a specific order.
because A event produced (4,0,0) how would I sort the first arraylist to have that number come next
You're not really telling us how the sorting should work. Can you describe the algorithm more - how does it decide the order of the items, how should one item be compared with another?

Add a number to an ArrayList by sorting

Include a method addEventToTimeline that will be passed an Event. When an element is added to the ArrayList of events, it should be put in the correct place in the list. The elements in events should always be in chronological order.
Use similar logic and code to what we used yesterday to add an Event into the
correct place in the list. (Events are compared using compareTo.)
My code looks like this so far. I know it's wrong but I have no idea where to go from here.
public void addEventToTimeline(Event a){
for(int i = 0; i <events.size(); i++){
if(a.compareTo(events.get(i))<0);
events.add(i+1, events.get(i));
events.set(i, a);
}
}
}
Are you forced to use an ArrayList ? I would use a tree for this kind of problem. Here's what you should do with an ArrayList solution:
Find the correct place for the new Event (O(n) if the arrayList is unsorted, Log(n) if it is sorted - using Binary Search
Put the Event in its place, and move all the subsequent Events one position to the right ( O(n) )
I would use a more advanced data structure for this. For example if all you want is the min Event each time use a heap
If you need a global order, use a BST , or even better, a red black tree
This post on StackExchange can also be useful

element of a Linked List

Can someone please explain In Java how do you find middle element of a linked list in single pass?
I have googled it, but cannot seem to find a simple explanation on how to code it.
LinkedList<String> list = new LinkedList<>();
list.add("foo");
list.add("bar");
list.add("baz");
String middle = list.get(list.size()/2);
System.out.println(middle); // bar
The call to assign middle will pass through half of the list during the get call.
As pointed out in the comments, the middle is the worst place to operate on a LinkedList. Consider using another variation, such as ArrayList.
I think this is a sort of trick question that you see on lists of possible interview questions.
One solution would be to have two pointers to step through the list, one taking steps of two and one taking steps of one.
When the pointer that is taking two steps at a time reaches the end of the list, the one taking only one step will be halfway through.
I doubt that this practice is really useful though..
good luck!
Since it's a LinkedList, you won't be able to find out its size until after the first (and only) pass. To find the middle element, you need to know two things; what index is at the middle, and what is the value of the element at that index. Finding the middle index is easy--just make one pass through the list, keeping a counter of how many nodes there are. As you do this, you'll need to keep track of each element in a separate data structure, perhaps an ArrayList, since you're only allowed one pass through the LinkedList. Once you're done, half the counter to find the middle index, and return the ArrayList element at that index.
The pseudo code looks like this:
int count
ArrayList elements
for each node in LinkedList:
count++
elements.append(node)
middleIndex = count/2
middleElement = elements.getIndex(middleIndex)
return middleElement
Of course, you'll need to take care of the case where there isn't a single middle element.

Java - Recover the original order of a list after its elements had been randomized

The Title is self explanatory. This was an interview question. In java, List is an interface. So it should be initialized by some collection.
I feel that this is a tricky question to confuse. Am I correct or not? How to answer this question?
Assuming you don't have a copy of the original List, and the randomizing algorithm is truly random, then no, you cannot restore the original List.
The explanation is far more important on this type of question than the answer. To be able to explain it fully, you need to describe it using the mathematical definitions of Function and Map (not the Java class definitions).
A Function is a Map of Elements in one Domain to another Domain. In our example, the first domain is the "order" in the first list, and the second domain is the "order" in the second list. Any way that can get from the first domain to the second domain, where each element in the first domain only goes to one of the elements in the second domain is a Function.
What they want is to know if there is an Inverse Function, or a corresponding function that can "back map" the elements from the second domain to the elements in the first domain. Some functions (squaring a number, or F(x) = x*x ) cannot be reversed because one element in the second domain might map back to multiple (or none) elements in the first domain. In the squaring a number example
F(x) = x * x
F(3) = 9 or ( 3 -> 9)
F(12) = 144 or ( 12 -> 144)
F(-11) = 121 or (-11 -> 121)
F(-3) = 9 or ( -3 -> 9)
attempting the inverse function, we need a function where
9 maps to 3
144 maps to 12
121 maps to -11
9 maps to -3
Since 9 must map to 3 and -3, and a Map must have only one destination for every origin, constructing an inverse function of x*x is not possible; that's why mathematicians fudge with the square root operator and say (plus or minus).
Going back to our randomized list. If you know that the map is truly random, then you know that the output value is truly independent of the input value. Thus if you attempted to create the inverse function, you would run into the delimma. Knowledge that the function is random tells you that the input cannot be calculated from the output, so even though you "know" the function, you cannot make any assumptions about the input even if you have the output.
Unless, it is pseudo-random (just appears to be random) and you can gather enough information to reverse the now-not-truly random function.
If you have not kept some external order information (this includes things like JVM trickery with ghost copies), and the items are not implicitly ordered, you cannot recover the original ordering.
When information is lost, it is lost. If the structure of the list is the only place recording the order you want, and you disturb that order, it's gone for good.
There's a user's view, and there's internals. There's the question as understood and the question as can be interpreted.
The user's view is that list items are blocks of memory, and that the pointer to the next item is a set of (4?8? they keep changing the numbers:) bytes inside this memory. So when the list is randomized and the pointer to the next item is changed, that area of memory is overriden and can't be recovered.
The question as understood is that you are given a list after it had been randomized.
Internals - I'm not a Java or an OS guy, but you should look into situations where the manner in which the process is executed differs from the naive view: Maybe Java randomizes lists by copying all the cells, so the old list is still kept in memory somewhere? Maybe it keeps backup values of pointers? Maybe the pointers are kept at an external table, separate from the list, and can be reconstructed? Maybe. Internals.
Understanding - Who says you haven't got an access to the list before it was randomized? You could have just printed it out! Or maybe you have a trace of the execution? Or who said you're using Java's built it list? Maybe you are using your own version controlled list? Or maybe you're using your own reversable-randomize method?
Edwin Buck's answer is great but it all depends what the interviewer was looking for.

Categories