Sample ADT design problem
One of the midquarter assessment tasks will be a data design problem. The problem statement will describe a system in need of a data structure, along with a few operations desired for the system. You are to propose a design for the data structure and describe how to implement the requested operations. The problem statement may also ask you to analyze the Big O runtime of a particular operation or design an operation to meet a particular Big O runtime.
Unless otherwise restricted in the problem statement, you will have access to the full range of C++ primitive data types (bool, char, int, double, string) and Stanford collections (Vector, Grid/GridLocation, Stack, Queue, Set, Map). Your design can mix and match, use more than one type, and/or create nested structures as befitting the needs of the system.
When presenting your proposed design, you should provide precise, valid C++ type declarations for the components and a rough diagram/list of the contents of a tiny system.
When describing an operation, your answer can be presented as a "code sketch". Think of this as somewhat elevated pseudocode. Your code should contain sufficient detail to demonstrate your understanding of the correct access to the data, but will not require the full precision of entirely valid C++.
What we are assessing with this task is your knowledge of the appropriate use of each data structure and understanding of the tradeoffs in choosing among them when designing a system. During the discussion portion of the assessment, you will offer the rationale for the choices you made, the alternatives you considered, and your evaluation of how your design meets the needs of the system.
Sample problem statement
The CS106B Paperless system allows you to re-submit as often as you like, a newer submission replaces any previous submission. (Pro Tip: submit your code as soon as it is partially working, and resubmit it as it improves. This way you always have a “safety” if catastrophe strikes)
The Paperless system is to be re-designed to track of each student’s submissions in a way that allows the student to revert to an earlier version. The new system should support the three operations below:
- The
submitoperation adds a new code submission for an assignment by a student. The assignment is identified by number. A student is identified by their by sunet, which is unique across all students. The code and sunet are strings. - The
discardoperation discards the most recent submission for a given sunet and assignment, reverting to the most recent of any previous submission. If the student has no previous submission, raises an error. - The
countoperation reports the count of submissions for a given assignment. The count is the number of students who have made at least one non-discarded submission for the assignment. It is highly desirable that the count operation be implemented very efficiently.
Your task is to design the data storage for the new Paperless system and give a code sketch for the operations. Here are the deliverables for your answer:
- Declare the type(s) of the data structure(s) you will use. Your choices should meet the needs of the system and show good style and discernment in choosing among the ADT(s).
- Diagram the contents of a tiny system containing three students and one assignment. Student A has two submissions, Student B has one submission, and Student C has not yet submitted.
- Provide a code sketch of the operations
submit,discard, andcount. - What is the Big O runtime of the
countoperation?
Data structure
- Store submissions in
Vector< Map<string, Stack<string> >> data - Vector has one element, a map containing two entries, (key = Student A, value = Stack of 2 submissions) and (key = Student B, value = Stack of 1 submission). No entry in Map for Student C (no submission yet)
submit
- Add code onto stack of submissions for sunet
data[assignNum][sunet].push(code)
discard
- Handle error (no submit), pop top from stack, remove student from map if discarded only submit
if (!data[assignNum].containsKey(sunet)) error(...) data[assignNum][sunet].pop() if (data[assignNum][sunet].isEmpty()) data[assignNum].remove(sunet)
count
- We do not store empty stack for non-submitting student, so count of students submitting is equal to size of map
return data[assignNum].size()- runs in O(1)
Other problems to consider
Here are a few other ADT design tasks to consider.
- Re-design the inverted index from the Assignment 2 search engine to support weighted search. In a weighted search, the matching documents are displayed in order according to the count of occurrences of the search term in the document body text.
- Re-design the inverted index from the Assignment 2 search engine to support phrase search. A search for the phrase "computer science" matches only those documents where the search term "computer" is immediately followed by "science" in the document body text.