CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

Similar documents
CS61B Spring 2016 Guerrilla Section 6 Worksheet

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CS61B, Fall 2011 Final Examination (corrected) P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CS61B, Spring 2006 Final Examination P. N. Hilfinger

CS61B, Fall 2007 Final Examination P. N. Hilfinger

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger and Josh Hug

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger. Test #1 (corrected)

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

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

CS61B, Fall 2009 Final Examination (revised) P. N. Hilfinger

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Final Examination CSE 100 UCSD (Practice)

Recitation 9. Prelim Review

Data Structures Brett Bernstein

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

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

Lecture Summary CSC 263H. August 5, 2016

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS61BL. Lecture 5: Graphs Sorting

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

Technical University of Denmark

Exam 3 Practice Problems

( ) n 3. n 2 ( ) D. Ο

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

Midterm solutions. n f 3 (n) = 3

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. Fall 2016.

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

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

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

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

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

I have neither given nor received any assistance in the taking of this exam.

Course Review for Finals. Cpt S 223 Fall 2008

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

(the bubble footer is automatically inserted into this space)

CSE373 Fall 2013, Midterm Examination October 18, 2013

(the bubble footer is automatically inserted into this space)

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

Exam Sample Questions CS3212: Algorithms and Data Structures

CSL 201 Data Structures Mid-Semester Exam minutes

COS 226 Algorithms and Data Structures Fall Final

University of Waterloo Department of Electrical and Computer Engineering ECE 457A: Cooperative and Adaptive Algorithms Midterm Examination

CS 61BL Data Structures & Programming Methodology

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

COMP 251 Winter 2017 Online quizzes with answers

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

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

Final Exam in Algorithms and Data Structures 1 (1DL210)

CS171 Final Practice Exam

CS 3114 Data Structures and Algorithms Test 1 READ THIS NOW!

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

COS 226 Algorithms and Data Structures Fall Midterm

CS171 Final Practice Exam

DO NOT REPRODUCE. CS61B, Fall 2008 Test #3 (revised) P. N. Hilfinger

(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

Prelim 2. CS 2110, April 26, 2016, 5:30 PM Total Question True/False Complexity Heaps Trees Graphs Max Score Grader

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

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Section 1: True / False (1 point each, 15 pts total)

Practice Midterm Exam Solutions

Final Exam in Algorithms and Data Structures 1 (1DL210)

CMSC351 - Fall 2014, Final Exam

Introduction to Algorithms October 12, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Quiz 1.

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

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

CSci 231 Final Review

CMSC132, Practice Questions

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

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

INSTITUTE OF AERONAUTICAL ENGINEERING

END-TERM EXAMINATION

CSE 247 Data Structures and Algorithms Fall Exam I

CSE 332 Winter 2018 Final Exam (closed book, closed notes, no calculators)

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

Data Structure and Algorithm, Spring 2013 Midterm Examination 120 points Time: 2:20pm-5:20pm (180 minutes), Tuesday, April 16, 2013

CSE332 Summer 2012 Final Exam, August 15, 2012

CSE373 Fall 2013, Final Examination December 10, 2013 Please do not turn the page until the bell rings.

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Princeton University Computer Science COS226: Data Structures and Algorithms. Midterm, Fall 2013

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

COMPSCI 311: Introduction to Algorithms First Midterm Exam, October 3, 2018

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

ECE250: Algorithms and Data Structures Final Review Course

Prelim 2. CS 2110, 16 November 2017, 7:30 PM Total Question Name Short Heaps Tree Collections Sorting Graph

CISC 320 Midterm Exam

Problem Score Maximum MC 34 (25/17) = 50 Total 100

CSE 373 Autumn 2012: Midterm #2 (closed book, closed notes, NO calculators allowed)

CS521 \ Notes for the Final Exam

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Transcription:

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger READ THIS PAGE FIRST. Your exam should contain 14 problems on 16 pages. Officially, it is worth 46 points. This is an open-book test. You have three hours to complete it. You may consult any books, notes, or other inanimate objects available to you. You may use any program text supplied in lectures, problem sets, or solutions. Please write your answers in the spaces provided in the test. Make sure to put your name, login, and lab section in the space provided below. Put your login and initials clearly on each page of this test and on any additional sheets of paper you use for your answers. Be warned: our tests are known to cause panic. Fortunately, this reputation is entirely unjustified. Just read all the questions carefully to begin with, and first try to answer those parts about which you feel most confident. Do not be alarmed if some of the answers are obvious. Should you feel an attack of anxiety coming on, feel free to jump up and run around the building once or twice. Your name: Login: Login of person to your Left: Right: Discussion TA: 1. /2 2. /4 3. / 4. /6 5. /4 6. /4 7. /4 8. /4 9. /4 10. /4 11. /2 12. /2 13. /2 14. /4 Please sign the following: I pledge my honor that during this examination I have neither given nor received assistance. Signature:

Final Exam Login: Initials: 2 1. [2 points] a. Describe as concisely as possible what foo does for all but one non-negative value of x. public static int foo(int x) { int y = 1; x = x ^ y; while ((x & y) == 0) { y = y << 1; x = x ^ y; return x; b. What is the remaining value of x and what number results from applying foo to it?

Final Exam Login: Initials: 3 2. [4 points] Consider a max heap of integers that is stored in an array, with the root vertex at index 1 (and the element at index 0 unused). Define the length of the edge between a vertex and a child of that vertex as the value of the parent minus that of the child. Fill in the function below to fulfill its comment. Use only one expression or statement per blank. /** Given that A is a max heap with root at index 1, and U>=1 and V>=1 * are valid indices of vertices in the heap, returns the length of the * shortest path from U to V, where the length of an edge is the * value at the parent vertex minus the value at the child. */ public static int shortestheappath(int[] a, int u, int v) { int u1 = u; v1 = v; while ( ) { if ( ) { else { ; ; return ; 3. [1 point] What do the words murder, skulk, parliament, and exaltation have in common?

Final Exam Login: Initials: 4 4. [6 points] In the questions below, give the bounds asked for in asymptotic (Θ( ), O( )) notation, as a function of the value of N in each case. Give your answer in as simple a form as you can (e.g. Θ(N 9 ), not Θ(12N 9 )), and give as tight a bound as possible. a. Assume that L is a linked list, N is its length, and calls to test take constant time. What is the worst-case running time for executing: IntList q = L; int c; c = 0; for (IntList p = L; p!= null; p = p.tail) { while (q!= null && test(q, p)) { c += 1; q = q.tail; Answer: b. Assume that Vertex is the vertex type for a connected, directed, acyclic graph in which each vertex has a maximum out-degree of 3. Define boolean find(vertex v, int x) { for (Vertex s : v.successors()) { if (s.getlabel() == x find(s, x)) { return true; return false; Let N be the number of edges in the graph. For Vertex w, find a bound for the worst-case running time of find(w, 42); Answer: O( ) c. Repeat part (b), but assume that the graph is a tree. Answer: Continues on next page.

Final Exam Login: Initials: 5 d. Give an asymptotic bound on 0 i k (lgn) i N k i Answer: Θ( ) e. True or false: If f(n) O(N 2 ) and g(n) O(N) are positive-valued functions, then f(n)/g(n) O(N). If true, explain why; if false give a counterexample. f. True or false: If f(n) Θ(N 2 ) and g(n) Θ(N) are positive-valued functions, then f(n)/g(n) Θ(N). If true, explain why; if false give a counterexample.

Final Exam Login: Initials: 6 5. [4 points] Consider the following definition of a tree type with integer labels: public class IntTree { public int value; public IntTree left, right; // Perhaps other members. Fill in the method closestlabel to fulfill its comment. For a bushy argument T, its running time should be O(lgN). To simplify coding, feel free to use the methods closer, below. /** Return whichever of Y or Z is closer to X. */ static int closer(int x, int y, int z) { return Math.abs(x - y) < Math.abs(x - z)? y : z; /** Assuming T is a BST, return the label in T that is closest to * X, or DFLT if T is empty. */ static int closestlabel(inttree T, int x, int dflt) { if (T == null) { return dflt; if ( ) { return ; else if ( ) { return ; else { return ;

Final Exam Login: Initials: 7 6. [4 points] Consider an N N grid whose cells contain integer values. We wish to find a shortest path from the lower-left corner of the grid to the upper-right corner, where a path consists of a sequence of squares, each one of which is adjacent to the preceding square in the path and either above, to the right, or diagonally above and to the right of the preceding square. The length of the path is the sum of the integers in its squares. For example, the shaded path shown below is shortest: 3 6 3 2 5 1 8 10 7 4 2 1 2 3 1 1 Here s a pseudo-code function to find the length of the shortest path from row i, column j to row N, column N, for 1 i N, 1 j N, in an N N grid G. G(r,c) is the value at row r, column c of G (numbering from (0, 0) in the bottom left corner. Unfortunately, its running time of lsp is exponential. def lsp(i, j): if i >= N or j >= N: return infinity if i == N-1 and j == N-1: return G(i, j) else: return G(i, j) + min(lsp(i+1, j), lsp(i, j+1), lsp(i+1, j+1)) a. Fill in the program below to compute the result in polynomial time into L[0][0]. You may assume you have a multi-argument min function available, as in Python. int[][] L = new int[n][n]; L[N-1][N-1] = G(N-1, N-1) for (int c = ; c ; c ) { for (int r = ; r ; r ) { = ; for (int c = ; c ; c ) { b. As a function of N, what is the worst-case execution time of this program?

Final Exam Login: Initials: 8 7. [4 points] Given the following minimax tree (where the root node is a minimizing node): Min Max Min 20 10 8 12 11 13 4 2 a. What is the value of the root node? b. Circle the nodes that will not be examined as a result of alpha-beta pruning (assuming we process child nodes left-to-right).

Final Exam Login: Initials: 9 8. [4 points] Consider the following directed graph: D E A B C G F a. Give a valid topological ordering of its nodes. b. Is it possible to add an edge to the graph so that no valid topological sort exists? If so, indicate such an edge. Otherwise, briefly indicate why no such edge exists. Continues on the next page

Final Exam Login: Initials: 10 c. Consider the following pseudo-code for operating on a directed graph, G: 1. Let G be a copy of G, with all edges reversed. 2. Initially, all vertices of G are colored white and are unnumbered. 3. Set n to 0. 4. For each vertex, v, of G, 4.1 If v is unnumbered, execute tsort(v). where the function tsort is defined 5. tsort(w): 5.1 Color w gray. 5.2 For each unnumbered successor, s, of w in G, 5.2.1 tsort(s) 5.3 Give w the number n. 5.4 Increment n. When this algorithm finishes execution on a graph that has a valid topological sort, what is a correct order for sorting the vertices of G? d. Whattest can you addtothefunctiontsort toraiseanerror ifghas novalid topological sort?

Final Exam Login: Initials: 11 9. [4 points] For each of the following, give a data structure or algorithm that you could use to solve the problem or write impossible if it is impossible to meet the running time given in the question. For each question, we have one or more right answers in mind, all of which are among the data structures and algorithms listed below. For each answer, provide a brief description of how the algorithm or data structure can be used to solve the problem. Possible answers (you can use some more than once and not use others at all): DFS Heap Quick sort Impossible BFS Trie Merge sort Dijkstra s Hash table Insertion sort Topological Sort Balanced BST Radix sort Kruskal s Linked list Selection Sort a. Given a list of N words with k characters each, find for each word its longest prefix that is also the prefix of some other word in the list. Worst-case running time: O(Nk) character comparisons. b. Given an undirected weighted connected graph with E edges, find the heaviest edge that can be removed without disconnecting the graph. Worst-case running time: O( E log E ). c. We would like to build a data structure that supports the following operations: add, remove, and findthe k th largest element for any k. Worst-case runningtime: O(log(N)) comparisons for each operation (where N is the number of elements currently in the data structure). d. Given an unordered list of N Comparables, construct a BST containing all of them. Running time: O(N) comparisons.

Final Exam Login: Initials: 12 10. [4 points] a. Draw the graph corresponding to the following adjacency matrix (vertex labels A D for the rows and columns are above and to the left: A B C D A 0 1 0 0 B 0 1 0 1 C 1 0 0 1 D 0 0 0 0 b. Give the adjacency-list representation of the above graph. c. Using an adjacency-list representation of a graph, how long does it take in the worst case to check for the existence of an edge between two vertices? Answer in terms of V and E, the number of vertices and edges in the graph, respectively, using Θ( ) notation. d. How long does the same operation take using an adjacency matrix, in the worst case?

Final Exam Login: Initials: 13 11. [2 points] When conducting an A* search on the following graph, for what range of values can the heuristic function return in order to make the heuristic admissible? Our heuristic function h takes in two vertices, x, and y, and returns a number for the estimated shortest path length from x to y. 2 4 B C E 2 1 10 A F D 3 7 h(b,d), h(a,d) 12. [2 points] Consider an undirected, connected graph G where all edges have a weight of 1, and a spanning tree T of G. For any two distinct vertices s and t in G, define L T (s,t) to be the length of the path from s to t in T, and define L G (s,t) to be the length of the shortest path from s to t in G. a. IfN isthenumberofverticesing, whatisthemaximumpossiblevalueofl T (s,t)/l G (s,t) for any two vertices of G? Give an exact expression, without using asymptotic notation (Θ( ), etc.). Answer: b. Draw an example of a graph and an MST of that graph that achieves this ratio. Be sure to indicate the nodes s and t in your example.

Final Exam Login: Initials: 14 13. [2 points] The (unmemoized) hashcode method for the class java.lang.string is as follows: public int hashcode() { int h = 0; for (int i = 0; i < length() ; i++) { h = 31 * h + charat(i); return h; For this problem, assume that a HashSet<String> uses the value of this function by taking it modulo the number of buckets, after first masking off the sign bit to make the number non-negative. (The actual HashSet and HashMap implementations do something more sophisticated.) a. Given a String of length L and a HashSet<String> that contains N Strings, give the worst- and best-case running times of inserting the String into the HashSet. Worst case: Θ( Best case: Θ( ) String comparisons ) String comparisons b. In Java, HashSets always use arrays whose size is a power of two. If this were not the case, the hashcode method shown above could be a very poor hash function. Give an example of an integer N such that hashcode would be a very poor choice of hash function for a HashSetwhose array had size N. Give a brief explanation of your answer. Assume that the Java HashSet uses external chaining to resolve collisions.

Final Exam Login: Initials: 15 14. [4 points] Complete the following program by filling in the blank line on this page and adding any necessary code to the next page. public interface Tree { /** Return true iff I contain X. */ boolean contains(int x); /** Insert X into me, if not already present, returning the result. */ Tree insert(int x); /** The empty tree, containing nothing. [NOTE: static methods * in interfaces is a Java 8 feature.] */ static Tree emptytree() { return ; public final class RegularTree implements Tree { /* NOTE: Final classes cannot be extended. */ /** A Tree containing label LAB and the contents of trees L and R, * where all elements of L are < LAB and those of R are > LAB. */ RegularTree(int lab, Tree L, Tree R) { _label = lab; _left = L; _right = R; @Override public boolean contains(int x) { return x == _label? true : x < _label? _left.contains(x) : _right.contains(x); @Override public Tree insert(int x) { if (x < _label) { _left = _left.insert(x); else if (x > _label) { _right = _right.insert(x); return this; private int _label; private Tree _left, _right; Continued on next page

Final Exam Login: Initials: 16 Add any additional code here. It may not contain any if statements, while statements, switch statements, conditional expressions, or try statements. // FILL IN, IF NEEDED.

Scratch space

Scratch space