CPSC 211, Sections 201 203: Data Structures and Implementations, Honors Final Exam May 4, 2001 Name: Section: Instructions: 1. This is a closed book exam. Do not use any notes or books. Do not confer with any other student. 2. Show your work. Partial credit will be given. Grading will be based on correctness, clarity and neatness. Clearly state any assumptions you make they should be reasonable! 3. I suggest that you read the whole exam before beginning to work any problem. Budget your time wisely according to the point distribution. 4. There are 7 questions worth a total of 100 points, on 9 pages (including this page). DO NOT BEGIN THE EXAM UNTIL INSTRUCTED TO DO SO. GOOD LUCK! 1
1. (16 pts total) Short answer. (a) (2 pts) What is the asymptotic (big-oh notation) worst-case time complexity of the mergesort algorithm? (b) (2 pts) What is the asymptotic (big-oh notation) worst-case time complexity of the sorting problem? (c) (2 pts) Name two benefits of object-oriented software engineering. (d) (2 pts) How does iterative prototyping contribute to developing good software? (e) (2 pts) When using depth-first search to search the state space of a production system, how is the decision made as to where to continue the search? (f) (2 pts) What is serializability for database management systems? (g) (2 pts) What is the purpose of the two-phase locking algorithm in a DBMS and what is one of its drawbacks? (h) (2 pts) True or False: Every mathematical function from the natural numbers (0, 1, 2,... ) to the natural numbers can be computed with a Java program. 2
2. (10 pts total) Draw the memory diagram (stack and heap) after the following C code fragment executes: typedef enum {FALSE, TRUE} Bool; typedef struct { int location; Bool visited[10]; Bool counting; int count; } Token; Token maintoken; Token* backuptokens = (Token*) malloc(sizeof(token)*4); maintoken.location = 0; maintoken.visited[0] = TRUE; maintoken.counting = FALSE; maintoken.count = 0; backuptokens[0] = maintoken; backuptokens[1].location = 8; backuptokens[1].visited[1] = TRUE; backuptokens[1].counting = FALSE; backuptokens[1].count = 0; 3
3. (14 pts total) Suppose you have a linked list in which each list node contains an integer and a link to the next node. refers to the first node, or is null if there isn t one. Write a recursive method (or methods) to calculate the sum of the integers in the top level call. You may use either Java or C.. Be sure to indicate 4
4. (20 pts total) Draw the result of inserting the following sequence of numbers into the following data structures: 20, 30, 10, 15, 25, 35, 19, 18, 22, 33. (a) (3 pts) binary min heap (b) (3 pts) binary search tree (c) (3 pts) hash table of size with chaining, where the hash function is (d) (3 pts) hash table of size with double hashing, where and 5
(e) (2 pts) What is the worst-case running time for insert in a binary min heap containing big-oh notation. Explain your answer. keys? Use (f) (2 pts) What is the worst-case running time for insert in a binary search tree containing big-oh notation. Explain your answer. keys? Use (g) (2 pts) What is the average-case running time for insert in a hash table of size containing keys, when collisions are resolved with chaining? Use big-oh notation. Explain your answer. (h) (2 pts) Under what circumstances is the running time from part (g) constant? 6
5. (16 pts total) Deletions. (a) (4 pts) Draw the result of doing five deletes from this binary min heap: (b) (4 pts) Draw the result of deleting the following sequence of keys from this binary search tree: 8, 4, 12, 11, 5 (c) (4 pts) What is the worst-case running time for delete in a binary min heap containing big-oh notation. Explain your answer. keys? Use (d) (4 pts) What is the worst-case running time for delete in a binary search tree containing Use big-oh notation. Explain your answer. keys? 7
6. (12 pts total) ADTs. (a) (4 pts) Explain how you could implement the Stack ADT (with push and pop operations) with one (or more) binary search trees (with the usual operations, including insert and delete). (b) (4 pts) What are the asymptotic worst-case running times for your push and pop operations, when there are keys in the stack? (c) (4 pts) What are the advantages and disadvantages of implementing a stack this way? Is it a good idea overall? 8
7. (12 pts total) You are to design a software system to support a hospital emergency room. When a patient comes to the emergency room, a nurse assigns a priority number to the patient, depending on how sick s/he is. Patients are seen by doctors in order of priority (sickest first). The software is used to enter information about each new patient and to decide who is the next patient to be seen by a doctor. (a) (6 pts) What data structure would you suggest using to store the information about the patients in the waiting room? Explain your answer and include time performance considerations in your answer. (b) (6 pts) Now suppose that the software should also be able to list all the waiting patients in order of their priorities. Does this change your answer from part (a), and if so how? Explain your answer. 9