Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Sample Question 1

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

// REQUIRES: the MinQueue is not empty


// EFFECTS: Returns the minimum item, but
// does not remove it from the MinQueue
const T & min() const;

// REQUIRES: the MinQueue is not empty


// EFFECTS: Removes the minimum item from the MinQueue
void popMin();

// REQUIRES: the MinQueue is not full


// EFFECTS: Adds 'item' to the MinQueue
void push(const T &item);

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];

// number of valid items in the data array


int size;
};

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.

// REQUIRES: the MinQueue is not full


// EFFECTS: Adds 'item' to the MinQueue
void push(const T &item) {

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.

template <typename T>


MinQueue<T> & operator+=(MinQueue<T> &q, const T &t) {

}
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];

// The number of books currently in the array


// INVARIANT: 0 <= num_books <= MAX_BOOKS
int num_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;
};

2a) (3 points) Implement the Library::most_popular() function according to its RME.

// REQUIRES: there is at least one Book in the Library


// EFFECTS: returns the Book that has been checked out the most
// times (or if there are multiple books that are "tied",
// any one of those most popular books)
const Book & most_popular() const {

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

● Constant (Not dependent on the size N)


● Linear (Proportional to the size N)

Write either "Constant" or "Linear" in each box below

// REQUIRES: there is at least one Book in the Library


// EFFECTS: returns the Book that has been checked out the most
// times (or if there are multiple books that are "tied",
// any one of those most popular books)
const Book & most_popular() const;

// 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);

// EFFECTS: Adds a Book with the given title to the library,


// unless an entry with the same title already exists,
// in which case nothing happens.
void add_book(const string &title);

You might also like