CS 314 Exam 2 Spring

Similar documents
CS 314 Exam 2 Spring 2018

CS 314 Exam 2 Fall 2017

CS 314 Exam 2 Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2017

CS 314 Midterm 2 Spring 2013

Points off Total off Net Score. CS 314 Final Exam Fall 2016

CS 314 Exam 1 Fall 2016

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

CS 307 Final Fall 2009

CS 307 Midterm 2 Spring 2008

Points off Total off Net Score. CS 314 Final Exam Spring Your Name Your UTEID

CS 314 Midterm 2 Fall 2012

CS 314 Final Fall 2012

CS 314 Exam 1 Spring 2018

CS 314 Exam 1 Fall 2017

CS 307 Midterm 2 Spring 2011

CS 307 Final Spring 2011

CS 314 Exam 1 Spring 2017

CS 314 Final Fall 2011

CS 307 Final Spring 2010

CS 314 Midterm 1 Fall 2012

CS 307 Midterm 2 Fall 2010

CS 314 Exam 1 Spring 2016

CS 307 Final Spring 2009

CS 307 Midterm 2 Fall 2008

CS 314 Midterm 1 Fall 2011

CS 307 Final Spring 2008

CS 307 Midterm 2 Fall 2009

CS 314 Midterm 1 Spring 2014

for dequeue will be O(N). (Or words to that effect. Needed to mention both or -1.)

CS 314 Final Spring 2013 SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION - SOLUTION

CS 314 Exam 1 Spring 2015

CS 307 Midterm 1 Spring 2009

CS314 Exam 1 - Fall Suggested Solution and Criteria 1

Largest Online Community of VU Students

CS165 Practice Final Exam

CS 307 Midterm 1[corrected] Spring 2008

CS 455 Midterm 2 Spring 2018 [Bono] Apr. 3, 2018

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

This is a set of practice questions for the final for CS16. The actual exam will consist of problems that are quite similar to those you have

CS314 Exam 2 - Fall Suggested Solution and Criteria 1

CS171 Midterm Exam. October 29, Name:

CS165 Practice Final Exam Answer Key

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

CS 216 Exam 1 Fall SOLUTION

CS 312 Final Fall 2013

NATIONAL UNIVERSITY OF SINGAPORE

COS 226 Algorithms and Data Structures Fall Midterm

CS134 Spring 2005 Final Exam Mon. June. 20, 2005 Signature: Question # Out Of Marks Marker Total

Total Score /15 /20 /30 /10 /5 /20 Grader

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

CS 312 Exam 2 Fall Circle your TA's Name: Aish Anthony Bri Carla Chris

Introduction to Computer Science Midterm 3 Fall, Points

CS 307 Midterm 1 Fall 2007

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Midterm Exam 2 CS 455, Spring 2011

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

About this exam review

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Summer Final Exam Review Session August 5, 2009

CS 455 Final Exam Spring 2018 [Bono] May 8, 2018

Data Structure (CS301)

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

ext Total Score /20 /20 /15 /20 /25 /5 Grader

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

Name CPTR246 Spring '17 (100 total points) Exam 3

Total Score /15 /20 /30 /10 /5 /20 Grader

CS 201, Fall 2016 Dec 2nd Exam 3

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

CSCE 2014 Final Exam Spring Version A

CS 455 Final Exam Spring 2015 [Bono] May 13, 2015

CSL 201 Data Structures Mid-Semester Exam minutes

Computer Science Foundation Exam

CSE wi: Practice Midterm

CSE 143 Sample Midterm Exam #7 (11wi)

Thomas Jefferson Invitational Open in Informatics 2012

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Midterm Exam 2 CS 455, Spring 2015

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

1. Answer as shown or -1 unless question allows partial credit. No points off for minor differences in spacing, capitalization, commas, and braces.

Section 05: Midterm Review

Complexity, General. Standard approach: count the number of primitive operations executed.

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

CS 455 Midterm 2 Fall 2017 [Bono] Nov. 7, 2017

CIS 110 Introduction to Computer Programming Summer 2017 Final. Recitation # (e.g., 201):

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

APCS Semester #1 Final Exam Practice Problems

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

Prelim 1. CS2110, October 2, 2014, 5:30 PM Extra Total Question TrueFalse Multiple Object Oriented

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

Lecture 3: Maps and Iterators

UNIVERSITY REGULATIONS

Stacks Fall 2018 Margaret Reid-Miller

Computer Science Foundation Exam

182 review 1. Course Goals

CS302 Data Structures using C++

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Transcription:

Points off 1 2 3 4 5 Total off CS 314 Exam 2 Spring 2017 Your Name Your UTEID Instructions: 1. There are 5 questions on this test. 100 points available. Scores will be scaled to 200 points. 2. You have 2 hours to complete the test. 3. Place you final answers on this test. Not on scratch paper. Answer in pencil. 4. You may not use a calculator or any other electronic devices while taking the test. 5. When answering coding questions, ensure you follow the restrictions of the question. 6. Do not write code to check the preconditions. 7. On coding questions you may implement your own helper methods. 8. On coding questions make your solutions as efficient as possible given the restrictions of the question. 9. Test proctors will not answer any questions regarding the content of the exam. If you think a question is ambiguous or has an error, state your assumptions and answer based on those assumptions. 10. When you complete the test show the proctor your UTID, give them the test and all the scratch paper, used or not, and leave the room quietly. 1. (1 point each, 20 points total) Short answer. Place your answer on the line next to or under the question. Assume all necessary imports have been made. a. If a question contains a syntax error or other compile error, answer compile error. b. If a question would result in a runtime error or exception, answer runtime error. c. If a question results in an infinite loop, answer infinite loop. d. Recall when asked for Big O your answer should be the most restrictive correct Big O function. For example Selection Sort has an average case Big O of O(N 2 ), but per the formal definition of Big O it is correct to say Selection Sort also has a Big O of O(N 3 ) or O(N 4 ). I want the most restrictive, correct Big O function. (Closest without going under.) A. What is returned by the method call a(13)? public static int a(int x) { if (x == 0) { return 0; else { return x + a(x / 2); B. What is returned by the method call a(-7)? C Given a positive int N, what is the order of method a? CS 314 Exam 2 Spring 2017 1

D. What is returned by the method call d(9)? public static int d(int x) { if (x <= 4) { return x; else { int total = 1; total += d(x - 2); total += d(x - 4); return total; E. What is the worst case order (Big O) of method e if the parameter list refers to a Java LinkedList? N = list.size() public static void e(list<integer> list, int t) { Iterator<Integer> it = list.iterator(); for (int i = 0; i < list.size() / 2; i++) { it.next(); while (it.hasnext()) { if (it.next() == t) { it.remove(); F. What is the worst case order (Big O) of method e if the parameter list refers to a Java ArrayList? N = list.size() G. What is the worst case order (Big O) of method g? N = list1.size(), list1.size() == list2.size() // pre: no elements of lists are null public static <E> int g(linkedlist<e> list1, LinkedList<E> list2) { int r = 0; for (int i = 0; i < list1.size(); i++) { E x = list1.get(i); for (int j = i + 1; j < list2.size(); j++) if (x.equals(list2.get(j))) r++; return r; CS 314 Exam 2 Spring 2017 2

H. A sorting methods uses the traditional mergesort algorithm presented in class. It takes the method 40 seconds to sort an array of 1,000,000 ints that are already in ascending order. What is the expected time for the method to sort an array of 2,000,000 ints that are already in ascending order? I. A sorting methods uses the traditional quicksort algorithm presented in class. It takes the method 10 seconds to sort an array of 10,000 ints, all equal to the same value. (For example, an array with 10,000 0s in it.). What is the expected time for the method to sort an an array of 30,000 ints, all equal to the same value. J. A sorting methods uses the traditional insertion sort algorithm presented in class. It takes the method.002 seconds to sort an array of 1,000,000 ints that are already in ascending order. What is the expected time for the method to sort an array of 2,000,000 ints that are already in ascending order? K. What is output by the following code? The code uses the Java Stack class. Stack<Integer> st = new Stack<Integer>(); for(int i = 1; i < 15; i *= 3) { st.push(i + 1); st.push(i); while (!st.isempty()) { System.out.print(st.pop() + " "); L. Given the following postfix expression what is the equivalent infix expression? (x, y, and z are vaiables) x y z - / M. What is the result of the following postfix expression? (single integer for answer) 10 3 8 12 - * + CS 314 Exam 2 Spring 2017 3

N. The binary search tree class in the code below uses the simple, naïve add algorithm demonstrated in class. It takes.1 seconds for the method to complete when n is 1,000,000. What is the expected time for the method to complete when n is 2,000,000? N = n. Assume the Random.nextInt() method is O(1). public static BST<Integer> maketree(int n) { BST<Integer> result = new BST<Integer>(); Random r = new Random(); for (int i = 0; i < n; i++) { result.add(r.nextint()); return result; O. Why is it problematic to use a Java ArrayList as the internal storage container for a Queue class? P. What is the minimum height of a full binary tree with 13 nodes? Consider the following binary tree. 8 is the root of the tree. 8 / \ 5 13 / / 3 8 / \ 0 10 Q. What is the result of a level-order traversal of the binary tree shown above? CS 314 Exam 2 Spring 2017 4

R. What is the result of a pre-order traversal of the binary tree shown on the previous page? S. What is the result of a post-order traversal of the binary tree shown on the previous page? T. The binary tree on the previous page is not a binary search tree. What, specifically in the tree shown, violates the requirements for a binary search tree? EXTRA CREDIT (1 point): How many current and former UT Austin Computer Science faculty members have won the Turing Award? CS 314 Exam 2 Spring 2017 5

2. Linked Lists I (20 points) - Complete the getnumdifferences instance method for the LinkedList314 class. The method determines the number of elements that are different between two lists. The method compares elements at the same position. If the lists are not the same size, then each extra element in the larger list counts as one difference. Do not use recursion. You may not use any other methods in the LinkedList314 class unless you implement them yourself as a part of your solution. The LinkedList314 class uses singly linked nodes. The list only has a reference to the first node in the chain of nodes. When the list is empty, first is set to null. There may be node's whose data variables is set to null. If the list is not empty, the last node in the chain of nodes, has its next reference set to null. You may use the nested Node class and the equals method for Objects. You may not use any other Java classes or native arrays. public class LinkedList314<E> { // refers to first node in the chain of nodes. private Node<E> first; // No other instance variables // The nested Node class. private static class Node<E> { private E data; private Node<E> next; Examples of calls to getnumdifferences (LinkedList314<E> other). In these examples the lists contain Integer objects. calling: [5, 7, 12] -> returns 0, no differences other: [5, 7, 12] calling: [] -> returns 0, no differences other: [] calling: [5, 20, 12, 20] -> return 3, 3 differences due to longer list other: [5] calling: [5, 7, 12] -> returns 2, the 5:4 and the extra 20 other: [4, 7, 12, 20] calling: [4, null, 0, -3, 7, 5] -> returns 4 other: [4, 7, null, -3, 12] Complete the following getnumdifferences method for the LinkedList314 class. CS 314 Exam 2 Spring 2017 6

/* pre: otherlist!= null post: per the question description. Neither list is altered*/ public int getnumdifferences (LinkedList314<E> other) { CS 314 Exam 2 Spring 2017 7

3. Stacks and Queues (20 points) - Write a method that accepts two stacks as parameters and returns true if the stacks have the same number of elements in the same order, false otherwise. This method is NOT part of the Stack class, so you do not have access to the private instance variables of the Stack class. You may create and use a single stack as an auxiliary data structure in your method. Consider the following examples: s1 top -> 4 s2 top -> 4 returns true 3 3 s1 top -> 4 s2 top -> 4 returns false 3 3 1 2 s1 top -> 4 s2 top -> 4 returns false 3 3 1 s1 top -> s2 top -> returns true (both stacks empty) s1 top -> s2 top -> 4 returns false (s1 empty) You may assume none of the elements in either stack equal null. You may use these methods for the stack class. Stack314: isempty, push, pop, top You may also use the equals method declared in the Object class. Do not use any other Java classes or methods. Note, each stack must be restored to its original state by the time the method completes. Do not use recursion. Complete the method on the next page. CS 314 Exam 2 Spring 2017 8

/* pre: s1!= null, s2!= null, no elements in s1 or s2 equal null post: per the problem description */ public <E> boolean stacksareequal(stack314<e> s1, Stack314<E> s2) { CS 314 Exam 2 Spring 2017 9

4. Maps (20 points) Write a method named removepointshalfaveragedistance that accepts a Map that has Strings as keys and Point objects as values. The method also accepts a target String. If the target String is a key in the map, the method removes from the map all key value pairs, other than the target, whose Point is less than half the average distance from the Point associated with the target. The average is calculated based on all of the values in the map other than the value associated with the target key. Do not use recursion. For example, consider the following map. (quotes not shown on Strings). Recall the format of the map is {key1=value1, key2=value2,..., keyn=valuen and in this case the values are Point objects in the form (x, y). {Austin=(0, 0), Buda=(0, -1), Dallas=(2, 10), Dripping Springs=(-1, -1), Houston=(10, -1), Pflugerville=(0, 1), Round Rock=(0, 2) If the target String is Austin, then the average distance from the Point object associated with Austin to all the other Points is roughly 4.3. Half of the average is 2.15. When we remove the key-value pairs with a distance less than half of the average distance (other than the target key itself) we get the following map: {Austin=(0, 0), Dallas=(2, 10), Houston=(10, -1), Round Rock=(1, 2) Pflugerville and Dripping Springs have been removed. Recall the distance formula for two points: distance = sqrt((x1 - x2) 2 + (y1 - y2) 2 ) You may use the following methods from the Map interface. put(key, value) get(key) remove(key) size() keyset() adds a mapping from the given key to the given value returns the value mapped to the given key (null if none) removes any existing mapping for the given key returns the number of key/value pairs in the map returns a set of the keys for this map. You may call the iterator method on the key set and use all of the methods from the Iterator interface. You may use the equals method on any objects. You may use the pow and sqrt methods from the Math class. You may use the following Point class. public class Point { public Point(int x, int y) public int getx() public int gety() Do not create or use any auxiliary data structures. Complete the method on the next page. CS 314 Exam 2 Spring 2017 10

/* pre: m!= null, target!= null, if target is a key in m then m.size() >= 2. post: per the problem description */ public void removepointshalfaveragedistance(map<string, Point> m, String target) { CS 314 Exam 2 Spring 2017 11

5. Recursive Backtracking (20 points) Remember the good old days of CS312? Remember Connect Four? When we studied recursion you probably said to yourself, "I bet I could add a computer player that crushes most humans, just like in Evil Hangman." Well, here is your chance. Write a method that determines if it is possible for a given player, red or black, to win a game of connect four. from a given board state (set up). Recall, Connect Four is played on a board with 6 rows and 7 columns. Players place a checker at the top of a column and the checker falls down to the lowest, open row in that column. A checker may not be placed in a column that is full. The players use red and black checkers. Red goes first. Players alternate turns until one player has four checkers in a row horizontally, vertically, or diagonally. It is possible for the game to end in a draw if no player can achieve four in a row. Here is a sample board after 6 moves: 0 1 2 3 4 5 6 column numbers.............. In the sample board, black can win the game in two moves. It is red's turn and if they....... add a checker to column 1, black wins by adding a checker to column 5. Likewise, if red....... adds a checker to column 5, black wins by adding a checker to column 1. If red moves r...... to any columns besides 1 or 5, black can win in 1 move. r. b b b. r Write a recursive backtracking method that determines if a given player can win the game from the current board state. For this question use the given ConnectFourBoard class that handles many of the low level details of the board. We use the char's 'r' and 'b' to represent the two different players' checkers. '.' is used to represent open spaces. Here is the public interface of the ConnectFourBoard class you may use on this question: public class ConnectFourBoard { public static int NUM_COL = 7; // 0 <= col < NUM_COL public boolean columnisopen(int col) // can we drop a checker in column public boolean gameover() // returns true if game is over // pre: gameover() public char winner(); // returns char of winner. 'r' for red, 'b' for // black, or 'd' for a draw with no winner // pre: 0 <= col < NUM_COL, checkercolor == 'r' or 'b' public void droppiece(int col, char checkercolor) // pre: 0 <= col < NUM_COL public void pickuptopchecker(int col) // remove top checker in col Complete the method on the next page using recursive backtracking to determine if the player represented by goal can win the game from the given board state. You may not use any Java classes or methods besides the ConnectFourBoard class. CS 314 Exam 2 Spring 2017 12

Hints: First, you don't need a helper method. Second, don't alter the value of goal. Third, what makes this method so interesting is we have slightly different tasks if the current player is the same as goal or not. In the recursive case, If the current player is the same as goal, we can return true as soon as we find a single winning move. If the current player is not the same as goal, we return false as soon as we find one move that results in goal not winning. // pre: board!= null, goal and cur == 'r' or 'b', nummoves >= 0 // cur is the current player. public boolean canwin(connectfourboard b, char goal, char cur) CS 314 Exam 2 Spring 2017 13