Dartmouth College Computer Science 10, Fall 2014 Solution to Final Exam A

Similar documents
Dartmouth College Computer Science 10, Winter 2012 Final Exam

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

York University AK/ITEC INTRODUCTION TO DATA STRUCTURES. Final Sample II. Examiner: S. Chen Duration: Three hours

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Summer Final Exam Review Session August 5, 2009

Final Examination CSE 100 UCSD (Practice)

XC Total Max Score Grader

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSCE 2014 Final Exam Spring Version A

Introduction to Computing II (ITI 1121) Final Examination

Computer Science 136 Spring 2004 Professor Bruce. Final Examination May 19, 2004

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CS 3114 Data Structures and Algorithms READ THIS NOW!

Section 05: Solutions

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

CS-301 Data Structure. Tariq Hanif

Second Examination Solution

1) Holiday Lights 30 Points

6.006 Final Exam Name 2. Problem 1. True or False [30 points] (10 parts) For each of the following questions, circle either True, False or Unknown.

D. Do inorder traversal on tree, values in ascending order, no repeats.

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

Trees. (Trees) Data Structures and Programming Spring / 28

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Section 05: Solutions

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Data Structures and Algorithms

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Dartmouth College Computer Science 10, Fall 2014 Midterm Exam

Computer Science Foundation Exam

BINARY HEAP cs2420 Introduction to Algorithms and Data Structures Spring 2015

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

Hash Table and Hashing

CS 106X Sample Final Exam #2

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Final exam. CS 2110, December 15, 2016, 9:00AM

Prelim 2 Solution. CS 2110, April 26, 2016, 5:30 PM

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Computer Science II Fall 2009

CS 314 Final Fall 2011

CS171 Final Practice Exam

Computer Science Foundation Exam

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

We assume uniform hashing (UH):

Final Exam. Name: Student ID: Section: Signature:

Prelim 2. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

CS102 Binary Search Trees

CS301 - Data Structures Glossary By

Lecture Notes on Tries

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

CS301 All Current Final Term Paper Subjective 2013 Solved with refernces

Exam Principles of Imperative Computation, Summer 2011 William Lovas. June 24, 2011

CSL 201 Data Structures Mid-Semester Exam minutes

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

CS 315 Data Structures mid-term 2

We have the pointers reference the next node in an inorder traversal; called threads

Binary Search Tree (3A) Young Won Lim 6/2/18

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

CSE 143, Winter 2009 Final Exam Thursday, March 19, 2009

CS 350 : Data Structures Binary Search Trees

Prelim 2 Solutions. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

- 1 - Handout #22S May 24, 2013 Practice Second Midterm Exam Solutions. CS106B Spring 2013

Lecture Notes on Tries

Algorithm Design Techniques (III)

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

CS 104 (Spring 2014) Final Exam 05/09/2014

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Lecture: Analysis of Algorithms (CS )

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

ECE264 Spring 2013 Final Exam, April 30, 2013

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

JAVA MOCK TEST JAVA MOCK TEST II

CS 151 Name Final Exam December 17, 2014

CS 10, Fall 2015, Professor Prasad Jayanti

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Final Exam. CS2110 Spring May 12, Total. Max Score

INSTITUTE OF AERONAUTICAL ENGINEERING

CS 151 Name Final Exam December 17, 2014

Programming II (CS300)

Prelim 2 Solution. CS 2110, April 26, 2016, 7:30 PM

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Total Score /1 /20 /41 /15 /23 Grader

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CS350: Data Structures Binary Search Trees

About this exam review

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 616 FINAL EXAM WINTER 2018

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

University of Maryland College Park Dept of Computer Science

Largest Online Community of VU Students

Transcription:

Dartmouth College Computer Science 10, Fall 2014 Solution to Final Exam A Professor Drysdale Print your name: If you need more space to answer a question than we give you, you may use the backs of pages or you may use additional sheets of paper and attach them to the exam. Make sure that we know where to look for your answer! Read each question carefully and make sure that you answer everything asked for. Write legibly so that we can read your solutions. Please do not write anything in red. We suggest that you include comments for solutions that require you to write Java code. They will help your grader understand what you intend, which can help you get partial credit. Hand in only what you want graded. We do not want your scrap paper unless it contains solutions you want us to grade. You may use an 8.5 x 11 inch sheet of paper with notes. These notes should be handwritten or typed in with a 10 point font or larger. You may use both sides of the paper. We will supply Javadoc-style documentation for any class or interface that you need to answer a question. Question Value Score 1 30 2 15 3 10 4 15 5 15 6 15 Total 100

CS 10 Fall 2014, Solution to Final Exam A 2 Question 1 30 points Short answer (a) (5 points) In binary search tree deletion the tricky case is to delete a node with two children. We instead replace it by a different node which we can easily delete. Describe the node that we delete easily, and explain why it is easy to delete. Solution: We replace node x by its immediate successor (or predecessor). This is the smallest item in x s right subtree, which cannot be empty because x has two children. We find this by going right once, then left until we find a node without a left child. Because it has no left child it is easy to delete by pointing its parent at its right child. (b) (4 points) You wrote a NamedAdjacencyMapGraph class for sa10. In the sample solution the new version of removevertex was: public void removevertex(vertex<v> v) throws IllegalArgumentException { vertexmap.remove(v.getelement()); super.removevertex(v); while the new version of getedge was: public Edge<E> getedge(v uname, V vname) throws IllegalArgumentException { return getedge(getvertex(uname), getvertex(vname)); Why was super needed in the call to super.removevertex(v) but not in the call to getedge( getvertex(uname), getvertex(vname))? Solution: In the first case you are overriding the removevertex method. This method and the one in the superclass both have parameter type Vertex<V>. Therefore without the super you would call the subclass removevertex method recursively. In the second case you are overloading getedge. The method in the superclass has two parameters of type Vertex<V>, while the one in the subclass has parameter types V. Therefore the call to getedge must refer to the one in the superclass.

CS 10 Fall 2014, Solution to Final Exam A 3 (c) (5 points) In the deterministic skip list (every other item is included in the next level up) what is the maximum number of comparisons that can be made on a single level of the skip list when searching for a key? Why can t you do more comparisons? Solution: The maximum number of comparisons on a given level is 2. You continue searching on a given level while the key you are looking for is greater than the key in the skip list. You may be greater than the first key that you look at after you drop down to the current level. However, you cannot be greater than the second one. This is because the second one appeared in the list above, and the reason that you dropped down where you did is because you were less than or equal to that key on the next level up. Therefore you are always less than or equal to the second key, and will stop searching on the current level once you compare the key you are looking for to the second key on the current level. (d) (5 points) What are bounded buffers and how do they work? What do they have to do with Unix pipes? Solution: Bounded buffers are circular queues with a limited amount of space. They are used to communicate between processes where one process produces output and the next process consumes it. This occurs when two processes are connected by a Unix pipe. The first process can add to the buffer until it is full, at which point it blocks until something is removed by the second process. The second process can remove things from the buffer until it is empty, at which point it blocks until something is added to the buffer.

CS 10 Fall 2014, Solution to Final Exam A 4 (e) (6 points) What is memoization, and under what circumstances will it speed up a computation? Under what circumstances will it do little to speed up the computation? Solution: When a recursive function is memoized, each solved subproblem is saved in a map. Before trying to solve a subproblem the problem is looked up in the map. If it is found, then the previously computed solution is returned. If not, it is solved recursively and the solution is added to the map. It is useful when there are a relatively small number of subproblems, but they are generated over and over again (e.g. edit distance). It is not useful when each of the subproblems only arises once (e.g. merge sort). (f) (5 points) Describe the difference between linear probing and double hashing. What is the main drawback to linear probing, and how does double hashing solve this? Solution: Both use an array with no linked lists. The difference is how they handle collisions. Linear probing starts at the place specified by the hash function and walks through the array one step at a time looking for an empty spot to insert (wrapping if necessary). It similarly walks through one step at a time to find a value, quitting when the value or an empty place is found. Double hashing has two hash functions. The first says where to begin in the table to insert or search. The second says how large a step to to take (wrapping if necessary). The drawback to linear probing is that clusters form, and the larger the cluster gets the more likely the cluster will grow at the next insertion. That is because once two keys collide they follow the same path and collide with the same things. Therefore an insertion that collides with anything in a cluster results in the cluster growing at the end. With double hashing if two keys collide they are unlikely to collide again, because each will step through by its own step size. Therefore clusters are less likely to form.

CS 10 Fall 2014, Solution to Final Exam A 5 Question 2 15 points You will add a helper method to the BST class that will verify that the parent/child links are correct. That is, the method will check every node to see if that node s children have parent pointers that point back to it. If the child is the sentinel it will not be tested. The appropriate data declarations and some methods of the BST class are included in the accompanying documentation. Note that because Node is an inner class your method can access its instance variables directly, so you do not have to use the get and set methods supplied. The method that calls your method is: public boolean verifylinks() { return verifylinkshelper(root) && (root == sentinel root.parent == sentinel); Note that it verifies that the parent pointer of the root of a non-empty tree points to the sentinel, so the helper method does not need to check this. Complete the helper method vertifylinkshelper below. / ** * Verifies that the subtree rooted at current has correct parent pointers. * @param current The root of the subtree to check * @return true if the parent pointers are all correct, false otherwise. * / public boolean verifylinkshelper(node current) { Solution: if(current == sentinel) return true; else if(current.left!= sentinel && current.left.parent!= current) return false; else if(current.right!= sentinel && current.right.parent!= current) return false; else return verifylinkshelper(current.left) && verifylinkshelper(current.right);

CS 10 Fall 2014, Solution to Final Exam A 6 Question 3 10 points Consider the following red-black binary search tree, where words are ordered alphabetically. Dark edges indicate that the node at the bottom is red. Insert gander into this tree and draw the updated tree on the next page. You should update the tree according to the rules of red-black tree insertion.

CS 10 Fall 2014, Solution to Final Exam A 7 The red-back tree after inserting gander : Solution:

CS 10 Fall 2014, Solution to Final Exam A 8 Question 4 15 points A useful operation on a graph is: given a vertex v and an edge label l, find an outgoing edge e from v that has label l and return the vertex at the destination of e. So if v is vertex and there is an edge from v to u labelled a, then the result of the operation given v and the label a should be u. You are to implement a method followedge that implements this operation. You may assume that all of the edges out of a vertex have different labels. If there is no outgoing edge with the given label the method should throw an IllegalStateException. / ** * Finds and follows an outgoing edge e from v with a given label, * returning the vertex at e s destination. * * @param g the graph containing the vertex v * @param v the vertex to search from * @param label the label on the desired outgoing edge from v * @return the destination of the outgoing edge with label "label" * / public static Vertex<String> followedge(graph<string,string> g, Vertex<String> v, String label) { Solution: for(edge<string> e: g.outgoingedges(v)) { if(label.equals(e.getelement())) { return g.opposite(v, e); throw new IllegalStateException();

CS 10 Fall 2014, Solution to Final Exam A 9 Question 5 15 points A game tree is shown below. The player on move alternates on each level. Evaluate the tree using game tree search with alpha-beta pruning. Fill in the values of the nodes that get evaluated and draw x s through the nodes that get pruned. Give the value of the game for the first player and show the best first move for that player. You may evaluate the tree by either having maximizing and minimizing levels or by maximizing at every level but negating the values from the level below, as Kalah does.

CS 10 Fall 2014, Solution to Final Exam A 10 Solution:

CS 10 Fall 2014, Solution to Final Exam A 11 Question 6 15 points Write a backtracking method generatestrings that generates all possible strings of length n from a given alphabet that have no immediately repeated letter. (So acc is not allowed, but cac is.). The code: char[] alphabet = { a, b, c ; generatestrings("", alphabet, 3); should generate the output: aba abc aca acb bab bac bca bcb cab cac cba cbc Complete the method whose header is on the next page.

CS 10 Fall 2014, Solution to Final Exam A 12 / ** * Generates all possible strings of length n from the given alphabet * that have no immediately repeated letter. (So "acc" is not allowed, * but "cac" is.) * * @param current the current partial string * @param alphabet the letters allowed. No letter will be repeated. * @param n the length of the desired strings. * / public static void generatestrings(string current, char[] alphabet, int n) { Solution: if (current.length() >= n) System.out.println(current); else { for(char letter:alphabet) { if(current.length() == 0 letter!= current.charat(current.length()-1)) generatestrings(current + letter, alphabet, n);