Grading
This assignment will be tested and graded by a computer program (and you can submit as many times as you like). For this to work, there are some important rules you must follow:
- Keep the directory structure of the provided zip file. If you find a file in the subdirectory
comp2402a2
leave it there. - Keep the package structure of the provided zip file. If you find a
package comp2402a2;
directive at the top of a file, leave it there. - Do not rename or change the visibility of any methods already present. If a method or class is
public
leave it that way. - Submit early and often. The submission server compiles and runs your code, and gives you a mark. You can submit as often as you like and only your best submission will count. There is no excuse for submitting code that does not compile or does not pass tests.
- Write efficient code. The submission server places a limit on how much time it will spend executing your code, even on inputs with a million lines. For some questions it also places a limit on how much memory your code can use. If you choose and use your data structures correctly, your code will easily execute within the time limit. Choose the wrong data structure, or use it the wrong way, and your code will be too slow for the submission server to grade (resulting in a grade of 0).
Submitting and Testing
The submission server is now accepting submissions. Please try it out.
Warning: Do not wait until the last minute to submit your assignment. There is a hard 5 second limit on the time each test has to complete. For the largest tests, even an optimal implementation takes 3 seconds, and may take longer if the server is heavily loaded.
The Assignment
Start by downloading the Assignment 2 Zip File, which contains a skeleton of the code you need to write.
The Tester
class, included in the zip file gives a very basic demonstration of the code. As is, Tester
throws an exception when it tries to test FastMinStack
and FastMinDeque
because they're not implemented yet. Despite the name, Tester
does not do thorough testing. The submission server will do thorough testing.
- [5 marks] A
MinStack
supports three main operations: the standardStack
operationspush(x)
andpop()
and the non-standardmin()
operation which returns the minimum value stored on the stack. The zip file gives an implementationSlowMinStack
that implements these operations so thatpush(x)
andpop()
each run in $O(1)$ time, butmin()
runs in $\Theta(n)$ time.
For this question, you should complete the implementation ofFastMinStack
that implements all three operations in $O(1)$ time per operation. As part of your implementation, you may use any of the classes in the Java Collections Framework and you may use any of the source code provided with the Java version of the textbook. Don't forget to also implement thesize()
anditerator()
* methods.
Think carefully about your solution before you start coding. Here are two hints: (i) don't use any kind ofSortedSet
orSortedMap
, These all require $\Omega(\log n)$ time per operation; (ii) take a look at the sample solution for Part 10 of Assignment 1. Really understanding this solution will help you design your data structure. - [5 marks] A
MinDeque
supports five operations: The standardDeque
operationsaddFirst(x)
,removeFirst()
,addLast(x)
, andremoveLast()
and the non-standardmin()
operation that returns the minimum value stored in theDeque
. Again, the zip file provides an implementationSlowMinDeque
that supports each of the first four operation in $O(1)$ time per operation but requires $\Omega(n)$ time for themin()
operation.
For this question, you should complete the implementation ofFastMinDeque
that implements all five operations in $O(1)$ (amortized) time per operation. As part of your implementation, you may use any of the classes in the Java Collections Framework and you may use any of the source code provided with the Java version of the textbook. Don't forget to also implement thesize()
anditerator()
* methods. Think carefully about your solution before you start coding. Here are two hints: (i) don't use any kind ofSortedSet
orSortedMap
, These all require $\Omega(\log n)$ time per operation; (ii) consider using one of the techniques we've seen in class for implementing theDeque
interface.
*You can still get part marks without correctly implementing the iterator()
method. But for full marks, the iterator()
method needs to return an Object
that supports the next()
and hasNext()
methods from the Iterator<T>
interface. For a MinStack
, the iterator should output the elements at the bottom of the stack first (the elements that have been in the stack the longest), finishing with the element at the top of the stack (the element that would be returned by a call to pop()
). For a MinDeque
, the iterator should output the elements beginning with the first element (the one that would be returned by removeFront()
) and finishing with the last element (the one that would be returned by removeLast()
).
Hint: There's no need to build your iterators from scratch. The collections in java.util
all have iterators.