Professional Documents
Culture Documents
Container ADTs Practice - Questions
Container ADTs Practice - Questions
Consider the class definition below (and in the reference material) for a container ADT called a
MinQueue, which stores elements and allows you to find/remove the element with the lowest
value. Such a data structure can be useful for a variety of applications where items should be
processed in order according to priority, rather than the order that they are originally inserted.
// You may assume type T supports ==, !=, <, <=, >, and >=
template <typename T>
class MinQueue {
public:
MinQueue() : size(0) { } // default ctor
private:
// For simplicity, we assume a fixed capacity
// and that the user of a MinQueue never exceeds
// this. This isn't a dynamic memory question :).
int data[10];
1a) (6 points) Assume that we define the representation invariants for MinQueue's data
representation such that the first size elements of the data array contain the valid items in the
MinQueue, and these elements are sorted in descending order (i.e. lower elements to the back).
What is the time complexity for the most efficient algorithm to implement each of the following
functions?
Write "constant O(1)", "linear O(n)", or "logarithmic O(log n)" for each below.
_____________________________ min()
_____________________________ popMin()
_____________________________ push()
1b) (6 points) Write an implementation of the push() function. Your implementation must put
the new element in the proper location to ensure that the sorting invariant is maintained.
1c) (5 points) Implement an overloaded += operator for MinQueue. The operator should be
implemented as a non-member function and should add the right-hand side operand as a new
element to the MinQueue (you may call push() in your implementation). The operator should
evaluate back into the MinQueue object, so that multiple += operations may be chained.
For example:
int main() {
MinQueue<int> q;
q += 6;
// q contains 6
(q += 2) += 5;
// q contains 6, 2, and 5
}
Implement the += operator overload as a non-member function in the box below. Note the
template header already provided for you, and that you should use MinQueue<T> (rather than just
MinQueue) in your code. Use the code editor below to write your solution, but do not modify the
given template header.
}
Sample Question 2
We would like to implement a container ADT for a Library that stores a collection of Books the
library owns and also keeps track of how many times each book has been checked out.
For simplicity, assume book titles are unique - no two different books share the same title.
Internally, the books will be stored in an unsorted array, similar to one that might be used to
implement a set (because we won't allow multiple entries with the same title). However, we
would also like to track which book is the most popular, so we store the index of the book that
has been checked out the most times. (Or if there is a "tie" for most popular, the index of one
such book.)
Let's say we choose the following data representation and representation invariants for Library:
class Library {
public:
static const int MAX_BOOKS = 10000;
private:
// An array with the capacity to store up to MAX_BOOKS books.
Book books[MAX_BOOKS];
// INVARIANT: The Book at index_mp has been checked out >= the
// number of times any other book has been checked out. If the
// Library does not contain ANY books, index_mp is -1.
int index_mp;
};
}
2b) (5 points) Implement the Library::check_out() function according to its RME.
// MODIFIES: this Library
// EFFECTS: Attempts to check out a book from the library. If a Book
// with the given title is found, increases its checked
// out counter by 1, also making sure to maintain the
// "most popular" invariant. If no book with the given
// title is found, this function does nothing.
void check_out(const string &title) {
2c) (4.5 points) Given the description of the Library data representation and invariants, what is
the time complexity for the most efficient algorithm to implement each of the following member
functions?
When determining your answer, consider the "size" N of the data structure to be the total number
of Books in the Library. Your two answer choices are: