Data Structures and Software Principles - Final Exam | CS 225, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Spring 2011;

Typology: Exams

2010/2011

Uploaded on 06/14/2011

koofers-user-kuc
koofers-user-kuc 🇺🇸

4.7

(3)

7 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
University of Illinois at Urbana-Champaign
Department of Computer Science
Final Examination
CS 225 Data Structures and Software Principles
Sample Exam 1
3 hours permitted
Print your name, netID, and lab section day/time neatly in the space provided below; print
your name at the upper right corner of every page.
Name:
NetID:
Lab Section (Day/Time):
This is a closed book and closed notes exam. In addition, you are not allowed to use any
electronic aides of any kind.
You should have 12 sheets total (the cover sheet, plus numbered pages 1-22). The last sheet
is scratch paper; you may detach it while taking the exam, but must turn it in with the exam
when you leave. The back of the page before the scratch paper contains the interface to the
Array and pair classes, which you might find useful.
Unless otherwise stated in a problem, assume the best possible design of a particular imple-
mentation is being used.
Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any
compiler error is an exam typo (though hopefully there are not any typos), and (2) assume
you are NOT allowed to write any helper methods to help solve the problem, nor are you
allowed to use additional arrays, lists, or other collection data structures unless we have said
you can.
Problem Points Score Grader
1 16
2 40
3 64
4 15
5 15
6 15
7 15
Total 180
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Data Structures and Software Principles - Final Exam | CS 225 and more Exams Data Structures and Algorithms in PDF only on Docsity!

University of Illinois at Urbana-Champaign

Department of Computer Science

Final Examination

CS 225 Data Structures and Software Principles

Sample Exam 1

3 hours permitted

Print your name, netID, and lab section day/time neatly in the space provided below; print your name at the upper right corner of every page.

Name: NetID: Lab Section (Day/Time):

  • This is a closed book and closed notes exam. In addition, you are not allowed to use any electronic aides of any kind.
  • You should have 12 sheets total (the cover sheet, plus numbered pages 1-22). The last sheet is scratch paper; you may detach it while taking the exam, but must turn it in with the exam when you leave. The back of the page before the scratch paper contains the interface to the Array and pair classes, which you might find useful.
  • Unless otherwise stated in a problem, assume the best possible design of a particular imple- mentation is being used.
  • Unless the problem specifically says otherwise, (1) assume the code compiles, and thus any compiler error is an exam typo (though hopefully there are not any typos), and (2) assume you are NOT allowed to write any helper methods to help solve the problem, nor are you allowed to use additional arrays, lists, or other collection data structures unless we have said you can.

Problem Points Score Grader 1 16 2 40 3 64 4 15 5 15 6 15 7 15 Total 180

  1. [C++ – 16 points].

(a) You have a hash table class declared as follows: class HashTable { private: Array<pair<int, int> > table; public: // public function declarations }; The Array will be indexed from 0 through table.Size() - 1. The first value in each pair holds 0 if the cell is empty, 1 if the cell is valid, and -1 if the cell was deleted. The second value in each pair holds the key value in this cell (if the cell is a valid cell). Write the assignment operator for this class. (It’s okay to just write the code that would appear in the .cpp file; that is, you can just write the definition and assume the declaration occured in the “public function declarations” above section.

  1. [Analysis – 40 points].

(a) You have an red-black tree composed of n nodes that each have three pointers. Each node points not only to its two children, but to its parent as well. (The root’s parent pointer, points to NULL, since the root does not have a parent. You have a pointer to the node in the red-black tree containing the minimum value in the tree. You want to travel from the minimum node to the root of the tree, printing each value you encounter along the way, and then travel from the root to the node containing the AVL tree’s maximum value, again printing each value you encounter along the way. Express the order of growth of the worst-case running time of this procedure, in Big-O notation, and explain convincingly why your answer is correct.

(b) The algorithm known as heapsort was briefly mentioned in lecture once. The algorithm sorts n integers by putting them all into a heap, and then calling DeleteMin repeatedly, thus pulling the integers out of the heap, in order from lowest to highest. Express the order of growth of the worst-case running time of heapsort, in Big-O notation, and explain convincingly why your answer is correct.

(d) You have an ordinary trie, holding n different words, all of which are no longer than L characters (counting the null character at the end of a word). You want to perform F find operations on the trie. Express the order of growth of the worst-case running time of this procedure (that procedure being, “perform F find operations”), in Big-O notation, and explain convincingly why your answer is correct.

  1. [Algorithms – 64 points].

(a) Given a singly-linked list, and a pointer current to a node in the singly-linked list, explain convincingly how you can implement InsertBefore in constant time.

(b) Imagine you have a sparse array with R rows and C columns, implemented by a “list of arrays”. That is, there’s a list, and for every row that has at least one non-default value, this list has a node for that row, containing the index of the row, and an array of every possible column in that row. If every row in the sparse array has at least one value, do we save any memory with this approach? Explain why or why not.

(d) Explain convincingly that for a red-black removal, case 2a works. That is, when the node or “NULL” which you’ve labelled “x” has a black sibling, and of that sibling’s two children, the one furthest from “x” is black and the one closest to “x” is red, explain what is done in that case, and why, and explain why we still have a legal red-black tree after this case.

(e) We looked at a depth-first-search-based algorithm for finding the topological sort of a graph. Explain what this algorithm was and why it worked.

(g) Consider a hash table where h(x) = x mod 11 and h 2 (x) = 7 − x mod 7. Insert the values 56, 27, 38, 25, 78, 50, and 45, in that order, into the hash table. Use double hashing to resolve collisions.

h(x) 1 5 5 3 1 6 1

h 2 (x) 7 1 4 3 6 6 4

(h) If you were to insert the words cot, car, carpet, cow, and coworker into a Patricia tree, not counting the leaves, how many nodes would there be? Explain your answer.

  1. [List Reversal – 15 points].

You have the following ListNode class:

class ListNode { public: int element; ListNode* next; ListNode* prev; };

and a doubly-linked list made up of such nodes, with a ListNode pointer head to the first node and with the first node’s prev and the last node’s next equalling NULL. We will assume it is publicly accessible, rather than nested in a class, for this problem. Write a function ReverseParts which has two parameter and returns nothing. The first parameter will be a reference to a ListNode pointer. This pointer will point to the head node of a doubly-linked list (and thus would be NULL if the list were empty). This list will have the prev of the first node and the next of the last node both pointing to NULL. The second parameter will be a positive integer, which we will call n. This function should re- verse every set of n values. For example, if the list had been 5->3->1->10-14->22->67->4->NULL and n were 3 , then the resultant list should be 1->3->5->22->14->10->4->67->NULL (the set of two values at the end of the list likewise have their order reversed). You can assume that n will be less than or equal to the list size. Whatever linked list this results in, the head parameter should be pointing to the first node of that list when you are done.

void ReverseParts(ListNode*& head) { // your code goes here

  1. [Comparison check – 15 points].

Given the following class:

class TreeNode { public: int element; TreeNode* left; TreeNode* right; };

You have a tree built from the above node type. This tree is NOT a Binary Search tree, though it is a binary tree, by definition. You want a function Greater that takes two parameters. The first will be a pointer to a TreeNode. The second should be an integer value. The function should return an array of all values greater than the parameter integer. The Array you return needs to be exactly the right size for the values it holds. You do not need to keep the array sorted in any particular order. (In this problem, you are allowed to create additional 1-dimensional arrays – you have to, since you are returning one from your function.)

Array Greater(TreeNode* ptr, int value) { // your code goes here

(Verifier, continued)

(Complete Subgraph, continued)

  1. [Trie – 15 points].

You have a de la Briandais tree that stores strings made of lowercase letters. Assume that it does NOT use the Patricia tree optimization, and that it is composed of individual nodes of the following list node type:

class DeLaNode { public: char letter; DeLaNode* subtree; DeLaNode* nextInList; };

To indicate leaves, we just set the null-character cell in that level’s array, to point to leaf (which we’ll assume is a variable we’ve declared elsewhere). You want to write a method numPrefixes that has three parameters. The first is a pointer to a DeLaNode. The second is a positive integer n. The third is an integer holding the depth of this node. You want to return the number of unique prefixes of exactly length n that are represented in the trie. For example, if you had already inserted “car”, “carpet”, and “cotton”, then you have two unique prefixes of length three – namely, “car”, and “cot”.

int numPrefixes(DeLaNode* ptr, int n, int level) { // your code goes here