2013 CS140 Final Exam
- Answer all questions.
- Answer the questions on the answer sheets.
- Do not answer questions on this exam.
- Answer the questions on the front of the answer sheets, not the back.
- When programming, you do not have to use include or using statements.
Question 1 - Basic Programming
Write the procedure get_n_rev() with the prototype to the right.
This procedure should return a vector composed of the last n
lines of standard input in reverse order.
If standard input has fewer than n lines, it should return all of the lines of
standard input in reverse order.
You may use any data structure from the standard template library or from class
(I've included them all on the cheat sheet). If standard input contains l
lines, your program should run in O(l) time and O(n) space. You will
lose half credit otherwise.
|
vector <string> get_n_rev(int n);
|
|
Question 2 - Basic Data Structures and Big-O
Tell me the running time of the following actions in terms of Big-O.
Use the answer sheet provided. If the answer depends on the state of
the data structure, give the worst-case.
- A: Finding an item in a AVL tree with n items.
- B: Erasing the first item from a linked list with n items.
- C: Performing a rotation on a node in an AVL tree with n items.
- D: Deleting an item from an AVL tree with n items.
- E: Finding an item in a regular binary search tree with n items.
- F: Erasing the first item from a deque with n items.
- G: Deleting an item from a set with n items.
- H: Finding an item that is in a hash table with n items that had no collisions when the items were inserted.
- I: Calling Push() on a stack with n items.
- J: Inserting an item into a map with n items.
- K: Erasing the first item from a vector with n items.
- L: Finding an item in a linked list with n items.
Question 3 - Were you in this class?
Recall the Towers of Hanoi problem from class/lecture notes. We had a
class Towers that had a public method called Make_Move()
with the following prototypes:
void Make_Move(int from, int to);
|
This double-checks the legality of moving a disk from tower from to tower to,
where from and to are 0, 1 or 2. If the move is legal, it prints the move and
moves the top disk from tower from to tower to.
Write a procedure called Solve() with the following prototype:
void Solve(Towers *t, int from, int to, int npieces);
|
This procedure should make the proper Make_Move() calls on the instance t
to move the top npieces disks from tower from to tower to.