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

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

CS314 Exam 2 - Fall Suggested Solution and Criteria 1

CS314 Exam 1 - Fall Suggested Solution and Criteria 1

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

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

CS314 Exam 1 - Fall Suggested Solution and Criteria 1

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

CS314 Exam 2 - Spring Suggested Solution and Criteria 1

CS314 Spring 2014 Midterm 1 Solution and Grading Criteria.

CS314 Exam 1 - Spring Suggested Solution and Criteria 1

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

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

1. Code Trace: 2. Program Logic (0.5 each) X <= y y > z x == 1 A S N S N S A A N S S S S A S POINT A POINT B POINT C POINT D POINT E

CS314 Fall 2011 Midterm 1 Solution and Grading Criteria.

1. Expressions - 1 point each. -1 for missing or extra " OR.0. differences in capitalization of true and false okay. No limit on points off.

CS314 Final - Fall Suggested Solution and Criteria 1

CS314 Final - Spring Suggested Solution and Criteria 1

CS314 Fall 2012 Midterm 1 Solution and Grading Criteria.

Fall_2010_M1_SuggestedSolutioAndGradingCrit.txt 10/8/2010

CS 314 Exam 2 Spring

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

CS 312 Midterm 2 Fall 2013

DNHI Homework 3 Solutions List, Stacs and Queues

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

CS 216 Exam 1 Fall SOLUTION

CS61BL Summer 2013 Midterm 2

CS171 Midterm Exam. October 29, Name:

Summer Final Exam Review Session August 5, 2009

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

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

CS 314 Exam 2 Spring 2016

CS 314 Midterm 2 Fall 2012

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

Associate Professor Dr. Raed Ibraheem Hamed

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

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

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

AP Computer Science A

Data Structure. Recitation VII

We've tried to include the common errors and grading standard for every question.

Java Review: Objects

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CSCE 2014 Final Exam Spring Version A

CSE 143 Sp04 Final Exam Sample Solution Page 1 of 12

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

CS 307 Midterm 2 Spring 2011

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

CSE 143, Winter 2010 Midterm Exam Key

AP Computer Science A

University of Maryland College Park Dept of Computer Science

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

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

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

Programming Assignment 2

CS 455 Final Exam Spring 2017 [Bono] May 10, 2017

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CS 314 Exam 2 Spring 2018

Computer Science Foundation Exam

CS 314 Exam 2 Fall 2017

Lecture 10 Notes Linked Lists

COMP 103 : Test. 2018, Sept 12//(Corrected)

CSE 143: Computer Programming II Spring 2015 HW7: 20 Questions (due Thursday, May 28, :30pm)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

COS 226 Algorithms and Data Structures Spring Midterm

Data Structure Advanced

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CS-220 Spring 2018 Test 1 Version A Feb. 28, Name:

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

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

"apple" "grape" "grape" "grape" "apple"

If you took your exam home last time, I will still regrade it if you want.

FORM 2 (Please put your name and form # on the scantron!!!!)

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

CS 206 Introduction to Computer Science II

Note that if both p1 and p2 are null, equals returns true.

UNIVERSITY REGULATIONS

COMP 103 : Test. 2018, Sept 12//(Corrected) ** WITH SOLUTIONS

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

CS24 Week 4 Lecture 2

USAL1J: Java Collections. S. Rosmorduc

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

COMP 103 Introduction to Data Structures and Algorithms

PROBLEM 1 : (Vocabulary: 8 points) For each of the words/phrases below, circle the denition that is the best description as it pertains in the context

CS24 Week 4 Lecture 1

1 Inheritance (8 minutes, 9 points)

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Lecture 10 Notes Linked Lists

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

CS 3410 Ch 7 Recursion

CS165 Practice Final Exam Answer Key

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

Postfix (and prefix) notation

CS32 Discussion Week 3

Intermediate Programming, Spring 2017*

Lecture 8: Iterators and More Mutation

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

Transcription:

CS314 Spring 2017 Exam 2 Solution and Grading Criteria. Grading acronyms: AIOBE - Array Index out of Bounds Exception may occur BOD - Benefit of the Doubt. Not certain code works, but, can't prove otherwise Gacky or Gack - Code very hard to understand even though it works. (Solution is not elegant.) LE - Logic error in code. NAP - No answer provided. No answer given on test NN - Not necessary. Code is unneeded. Generally no points off NPE - Null Pointer Exception may occur OBOE - Off by one error. Calculation is off by one. RTQ - Read the question. Violated restrictions or made incorrect assumption. 1. Answer as shown or -1 unless question allows partial credit. No points off for minor differences in spacing, capitalization, commas, and braces. A. 23 B. -11 C. O(logN) // base 2 okay D. 15 E. O(N) F. O(N 2 ) G. O(N 3 ) H. 84 seconds I. 90 seconds J..004 seconds K. 9 10 3 4 1 2 L. x / (y - z) M. -2 N..21 seconds O. Because adding to the front for enqueue or removing from the front P. 3 for dequeue will be O(N). (Or words to that effect. Needed to mention both or -1.) Q. 8 5 13 3 8 0 10 R. 8 5 3 0 10 13 8 S. 0 10 3 5 8 13 8 T. Two problems. 10 is not less than 5 but is in the left subtree of the 5 AND there are two 8's, duplicates not allowed. (Or words to that effect. Needed to mention both or -1) EXTRA CREDIT: 2 CS314 Exam 2 - Spring 2017 - Suggested Solution and Criteria 1

2. Comments. A relatively simple linked list problem that required moving through two linked lists Common Problems: missing the last or first node not comparing data correctly not handling null data correctly public int getnumdifferences (LinkedList314<E> other) { Node<E> nthis = this.first; Node<E> nother = other.first; int result = 0; while (nthis!= null && nother!= null) { E datathis = nthis.data; E dataother = nother.data; if (datathis == null dataother == null) { if (datathis!= dataother) { result++; // not both null else if (!datathis.equals(dataother)) { result++: nthis = nthis.next; nother = nother.next; // count remaining items in longer list Node<E> n = (nthis!= null)? nthis : nother; while (n!= null) { result++; n = n.next; return result; 20 points, Criteria: 2 temp variables, 1 point track number of differences, 1 point loop until one of temps is null, 3 points Off By One Error (OBOE) due to n.next!= null correctly handle case when one or both data values is null, 2 points correctly call equals on data elements and increment result if not equal, 3 points -2 for comparing nodes instead of data, -2 if use compareto instead of equals, -2 for == instead of.equals -2 for counting equal values instead of different values!n1.data.equals(n2.data) move both references, 5 points move through larger list, counting number of elements left, 4 point return result, 1 point Other penalties: using disallowed methods, -6 (can be less for severity) destroy either list, -6 CS314 Exam 2 - Spring 2017 - Suggested Solution and Criteria 2

3. Comments: A relatively simple stack question. Very similar to the reversing the stack question from lecture using two stacks (not allowed per description) not restoring stacks (early return of false) not stopping at first difference we didn't take off for this (in retrospect we should have) but some people tried to get clever and just push one of the equal items on to the temp stack. The items are equal, so why does it matter if I store one or two. The problem with this is we are storing references and if the objects are mutable putting the same reference onto two different stacks could lead to hard to find logic errors if one of the objects is altered, thus altering the "other one" although there is really only one object not both stacks are empty after the first difference public <E> boolean stacksareequal(stack314<e> s1, Stack314<E> s2) { Stack314<E> tempstack = new Stack314<>(); boolean equal = true; while (!s1.isempty() &&!s2.isempty() && equal) { // OR!(s1.isEmpty() s2.isempty()) && equal equal = s1.top().equals(s2.top()); tempstack.push(s1.pop()); tempstack.push(s2.pop()); // make sure both stacks are empty equal = equal && s1.isempty() && s2.isempty(); // Now put the stuff we took out back. while (!tempstack.isempty()) { s2.push(tempstack.pop()); s1.push(tempstack.pop()); return equal; create temp stack, 1 point loop until one or both of stacks are empty, 4 points stop when first difference occurs, efficiency, 3 points properly remove elements from stacks and place in temp stack, 4 points (if equal can, just add one) check both stacks empty after main loop and update result correctly, 3 points properly put items back in original stacks (order of push's must be reversed), 5 points (or if only pushed one, add back to both) Other deductions: using data temp data structure besides a single Stack, -6 using two stacks instead of 1, -3 using compareto instead of equals, -2 CS314 Exam 2 - Spring 2017 - Suggested Solution and Criteria 3

4. Comments: An interesting map problem with two parts Creating an extra data structure. I chose to take off for this because the question did not allow the use of a Map constructor. Co-modification error. Without the ability to use another data structure if you remove from the map using, the map remove method, in the middle of a for-each loop or an iteration, a co-modification error is thrown. It was necessary to explicitly use the iterator for the keyset and remove via the iterator which does in fact remove from the map. Not checking if the target value is actually present Removing the target point itself. Suggested Solution: private static void removepointshalfaveragedistance(map<string, Point> m, String target) { Point center = m.get(target); if (center!= null) { int x = center.getx(); int y = center.gety(); double total = 0.0; for (String key : m.keyset()) { Point p = m.get(key); int x2 = p.getx(); int y2 = p.gety(); total += Math.sqrt(Math.pow(x - x2, 2) + Math.pow(y - y2, 2)); double halfave = total / (m.size() - 1) / 2; Iterator<String> it = m.keyset().iterator(); while (it.hasnext()) { String key = it.next(); Point p = m.get(key); int x2 = p.getx(); int y2 = p.gety(); double distance = Math.sqrt(Math.pow(x - x2, 2) + Math.pow(y - y2, 2)); if (distance < halfave &&!key.equals(target)) { it.remove(); check target present, 1 point loop through keys, 3 points determine distance between current point and target, 4 points determine correct average distance, 3 points remove key-values that are less than half the average distance, 3 points avoid co-modification error by using iterator remove method, 3 points does not remove target key-value pair, 3 points Others: New data structure -3 CS314 Exam 2 - Spring 2017 - Suggested Solution and Criteria 4

5. Comments: A very interesting recursive back tracking problem, because what and when we return varies based on if the current player is the goal player or not. Incorrect base case (must check gameover()) Not handling the two approaches based on goal == cur and goal 1= cur returning early not using return value to determine what to do. Suggested Solution: (Gacky with repeated code, but possibly easier to understand) public boolean canwin(connectfourboard b, char goal, char cur) { // base case if (b.gameover()) { return b.winner() == goal; else { char next = cur == 'b'? 'r' : 'b'; if (goal == cur) { // looking for a winning move to return true for (int col = 0; col < ConnectFourBoard.NUM_COL; col++) { if (b.columnisopen(col)) { b.droppiece(col, cur); boolean won = canwin(b, goal, next); b.pickuptopchecker(col); if (won) return true; return false; // the goal player can't find a win else { // looking for any non-win to prevent goal from winning for (int col = 0; col < ConnectFourBoard.NUM_COL; col++) { if (b.columnisopen(col)) { b.droppiece(col, cur); boolean won = canwin(b, goal, next); b.pickuptopchecker(col); if (!won) return false; return true; // The goal player can win no matter what we do!! correctly handle base case, 4 points determine color of next checker correctly, 2 points loop through choices (columns) in recursive case, 1 point place and remove the next checker correctly, 2 points make correct recursive call, and use result correctly (not returning early, but returning if desired answer found), 5 points handle the two cases of goal == cur and goal!= cur correctly, 4 points return correctly if none of the choices in the recursive case were desired answer, 2 points CS314 Exam 2 - Spring 2017 - Suggested Solution and Criteria 5