Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Prelim Two Solution - Solved Exam - Fall 2005 | CS 2110, Exams of Computer Science

Material Type: Exam; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Fall 2005;

Typology: Exams

Pre 2010

Uploaded on 08/30/2009

koofers-user-vsi
koofers-user-vsi 🇺🇸

10 documents

1 / 9

Toggle sidebar

Related documents


Partial preview of the text

Download Prelim Two Solution - Solved Exam - Fall 2005 | CS 2110 and more Exams Computer Science in PDF only on Docsity! Name____________________________________________ NetID______________ Prelim Two Solution CS211 — Fall 2005 Closed book; closed notes; no calculators. Write your name and netID above. Write your name clearly on each page of this exam. For partial credit, you must show your work. Do not begin until you are instructed to do so. You have 90 minutes. 1 of 9 Name____________________________________________ 1. (22 points; 2 each) True or false? a) T F The expected runtime for QuickSort is O(n log n) and the expected runtime for InsertionSort is O(n2), so QuickSort is always better than InsertionSort. b) T F QuickSort is a stable sorting algorithm. c) T F In a Binary Search Tree (BST) with n nodes, it always takes O(log n) time in the worst-case to find a node. d) T F A binary tree of height h has O(2h) nodes. e) T F QuickSort's efficiency always improves if you pick the largest element as your pivot instead of the smallest element. f) T F If f(n) = n2log n + 2n + 2 then we know that f(n) is O(n3). g) T F If f(n) = O(g(n)) and g(n) = O(h(n)) then we know that f(n) = O(h(n)). h) T F f(n) + g(n) is O(min{f(n), g(n)}). i) T F If f(n) is O(n2) and g(n) is O(n) then we know that f(n) + g(n) is O(n2). j) T F If item A is an ancestor of item B in a Binary Search Tree (BST) used as a Dictionary (operations = insert, find, remove) then it must be the case that the Insert operation for item A occurred before the Insert operation for item B. k) T F Both QuickSort and MergeSort use the Divide-and-Conquer strategy. 2. (BSTs; 8 points; 2 each) Suppose we wish to search for the number 400 in a Binary Search Tree (BST) holding integers. For each of the following sequences, can it be the sequence of nodes examined during a search for 400 in some BST? Answer yes or no; no proof is required. a. 150, 490, 312, 440, 301, 322, 400 no (because of 301) b. 101, 102, 103, 100, 200, 300, 400 no (because of 100) c. 150, 600, 200, 230, 250, 450, 350, 400 yes d. 150, 200, 250, 300, 450, 350, 500, 400 no (because of 500) 2 of 9 Name____________________________________________ 5. (Inheritance; 10 points) You are designing a system that works on a given double value using a “pipeline” of manipulators. The system receives an array of Manipulator objects; this array determines the set of operations that are applied to a double. For example, one Manipulator object may square the double, another Manipulator may divide it by two. The following Java code snippet shows how an array of Manipulator objects is used to perform the default manipulation (truncate the double, throwing away the decimal part), add one to the given value, square it, and then take the base 8 logarithm. public static void main(String [] args) { if (args.length == 0) System.exit(0); //quit Manipulator[] mArray = new Manipulator[] { new Manipulator(), new AddOneManipulator(), new SquareManipulator(), new LogarithmManipulator(8.0) }; process(mArray, Double.parseDouble(args[0])); } public static double process(Manipulator[] manipulators, double value) { for (Manipulator m : manipulators) { value = m.manipulate(value); } return value; } Write the Java class declarations for Manipulator, AddOneManipulator, SquareManipulator, and LogarithmManipulator. You may use the methods Math.floor(double value) which computes the floor of the value and Math.log10(double value) which returns the base-10 logarithm of the value. To find a logarithm of some value to base b, use Math.log10(value)/Math.log10(b). Use the next page to write your Java classes. 5 of 9 Name____________________________________________ Use this page to write the classes Manipulator, AddOneManipulator, SquareManipulator, and LogarithmManipulator for problem 5. class Manipulator { public double manipulate (double value) { return Math.floor(value); } } class AddOneManipulator extends Manipulator { public double manipulate (double value) { return value + 1; } } class SquareManipulator extends Manipulator { public double manipulate (double value) { return value*value; } } class LogarithmManipulator extends Manipulator { private double base; public LogarithmManipulator (double base) { this.base = base; } public manipulator (double value) { return Math.log10(value)/Math.log10(base); } } 6 of 9 Name____________________________________________ 6. (Data Structures; 20 points; 4 each) For each of the following pairs of data structures, name one advantage that the first data structure has over the second and one advantage that the second has over the first. Your answers should be brief. Note that "having more operations" is not an acceptable answer since the given Abstract Data Type (ADT) determines the set of operations. Further, note that we are assuming high-quality implementations are readily available, so “easier to implement” is also not an acceptable answer a. [Linked List vs. Array] used as a Stack. Linked List advantage: No bound on size Array advantage: Less space b. [Heap vs. Sorted Array] used as a Priority Queue. Heap advantage: All PQ operations are O(log n) time. Sorted Array advantage: Fast if no insertions after initialization or getMax takes O(1) time. c. [Hashing with Chaining and Table-Doubling vs. Balanced BST] used as a Dictionary. Hashing advantage: O(1) expected time (but bad worst-case) Balanced BST advantage: O(log n) worst-case time d. [Array of Lists, one list for each priority (sometimes called a bounded-height priority queue) vs. Heap] used as a Priority Queue. Array of Lists advantage: O(1) per operation (but requires small number of priorities) or FIFO for items with tied priorities Heap advantage: Can allow arbitrary number of different priorities. e. [Sorted Array vs. Balanced BST] used as a Dictionary. Sorted Array advantage: Uses less space Balanced BST advantage: Insert and remove are faster (O(log n)). 7 of 9
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved