Docsity

Computer Science I - Practice Final Solutions: Algorithms and Data Structures, Exams of Computer Science

Toggle sidebar
bg1
CS163
Computer Science I
Winter 2008
Practice Final
Partial Solutions
Review points:
• Be able to do any of the problems on the Midterm or Practice Midterm.
• Be able to write, trace, or fix any sorts we discussed in class (bubble sort, quick sort, merge sort, etc.).
• Know the best-case, worst-case, and average-case running time of all algorithms discussed this semester.
• Be able to trace recursive code.
• Be able to trace linked list code.
• Be able to implement a Stack or a Queue from scratch.
• Be able to implement a Stack of a Queue given an array, ArrayList, or LinkedList.
• Be able to draw a UML diagram given source code.
1). Why does the Comparable<T> interface take a parameter? (In other words, why is it Comparable<T>
instead of just Comparable?)
The Comparable interface specifies this method int comapreTo(T other). The T is necessary
so we know what the type of other is.
2). What value should foo.compareTo(foo) return? Why? What other properties should a well-behaved
compareTo method have?
• compareTo should be reflexive: a.compareTo(a) should be 0.
• compareTo should be transitive: if a.compareTo(b) == 1 and b.compareTo(c) is 1, tehn
a.compareTo(c) should also be 1.
• compareTo should be anitsymmetric: a.compareTo(b) should equal the negative of
b.compareTo(a).
3). Implement a reasonable compareTo method for the following class:
class Name
{
protected String firstName;
protected String lastName;
protected char middleInitial;
}
int compareTo(Name other)
{
String a = lastName + firstName + middleInitial;
String b = other.lastName + other.firstName + other.middleInitial;
Return a.compareTo(b);
}
pf3
pf4

Partial preview of the text

Download Computer Science I - Practice Final Solutions: Algorithms and Data Structures and more Exams Computer Science in PDF only on Docsity!

CS163 Computer Science I Practice Final Winter 2008

Review points:^ Partial Solutions

  • • • Be able to do any of the problems on the Midterm or Practice Midterm.Be able to write, trace, or fix any sorts we discussed in class (bubble sort, quick sort, mergeKnow the best-case, worst-case, and average-case running time of all algorithms discussed this semester. sort, etc.).
  • • • Be able to trace recursive code.Be able to trace linked list code.Be able to implement a Stack or a Queue from scratch.
  • • Be able to implBe able to draw a UML diagram given source code.ement a Stack of a Queue given an array, ArrayList, or LinkedList. 1). Why does the instead of just Comparable Comparable?) interface take a parameter? (In other words, why is it Comparable The so we know what the type of Comparable interface specifies this method other is. int comapreTo(T other). The T is necessary 2). What value should compareTo method have? foo.compareTo(foo) return? Why? What other properties should a well-behaved
  • • compareTcompareTo should be transitive: if a.compareTo(b) == 1 and b.compareTo(c) is 1, tehno should be reflexive: a.compareTo(a) should be 0.
  • a.compareTo(c) should also be 1.compareTo should be anitsymmetric: a.compareTo(b) should equal the negative of 3). Implement a reasonable^ b.compareTo(a). compareTo method for the following class: class Name { protected String firstName; protected String lastName; protected char middleInitial; } int compareTo(Name other) { String String ab = lastName= other.lastName + + firstName other.firstName + middleInitial; + other.middleInitial;

}^ Return^ a.compareTo(b);

4). We define two linked lists to be equal if (1) each list is the same length, and (2) the items at position each list are equal (as equals() for the LinkedList determined by the item’s class described below. Your method must run in O(n) time. equals method). Implement the method boolean x in class LinkedList { private static class Node { protected T item; protected Node next; } protected Node head; } boolean equals(LinkedList other) { Node current = Node current_o head;= other.head; while (current != if (current_o return false;null) { == null) { } if (!current.item.equals(current_o.item)) return false; { } current = current.next; current_o = current_o.next; } return current_o == null; }

7). Compute f (5, 3) using the definition of f below: f(5,3) = 8 int { f(int if (x x,== int1) returny) 1; if if return (y(y ==== f(x x)1)- (^) 1,returnreturn y) + 1;x;f(x-1, y-1); } 8). Describe what f(x, x-1) does in general when x > 1. (MTH 225 people should attempt to prove your answer by induction.) f(x, x-1) = x. As a base case, consider x = 2. f(2, 1) = 2. Now, assume inductive that f(x, x def inductive hypothesis tells us that f(xinition f(x, x-1) = f(x-1, x-1) + f(x - 1, x-1, x - -2). The definition of f tells us that f(x 2) = x - 1. Thus, f(x, x - 1) = x – 1 + 1 = x as desired.- - 1) =x for all x >= 2. By1, x-1) = 1. Our 9). Write a recursive implementation of int indexOf(char c, int place) int indexOf(char c). { if (place == if (c == this.charAt(place)) { return place; } this.length() - 1) { return - 1; } }^ return^ indexOf(c,^ place + 1); 10). Write a method subsequence of array. (The maximum subsequence is the subset [i, j]of the array for which int maxSubsequence(int[] array) that returns the value of the maximum

!

maximized. The method should return the sum of the elements of the maximum subsequence.)^^ x^ "^ x <==^ i^ j^ array [ x ]is

  • • Determine the running time ofTry to find the fastest possible algorithm. your algorithm. 11). value of n. Part 1: O(1). Notice that the number of iterations in each loop is bouded above by 50, regardless of the Part 2: f is O(a) (the value of a determines how many times the g is O(n^2). The body of the loop is O(j), which is bounded by n. The loop runs n times. h is O(n^2). The biggest piece is the call to f(nn), which is O(nn). loop runs). 12). We’ll go over this in class Friday.