






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Bermudez; Class: DATA STRUC/ALGORITHMS; Subject: COMPUTER PROGRAMMING; University: University of Florida; Term: Fall 2005;
Typology: Exams
1 / 10
This page cannot be seen from the preview
Don't miss anything!







Given name: Family name:
Student number: Signature:
Computer and Information Science and Engineering
COP3530 (Data Structures and Algorithms) Instructor: Dr. Manuel Berm´udez
MIDTERM 1 October 14, 2005
Duration: 50 minutes
No aids allowed
This examination paper consists of 10 pages and 4 questions. Please bring any discrepancy to the attention of an invigilator. The number in brackets at the start of each question is the number of points the question is worth.
Answer all questions.
To obtain credit, you must give arguments to support your answers.
For graders’ use:
a = 0 b = 1 ababaaab Compressed string = null
a = 0 b = 1 ab = 2 babaaab Compressed string = 0
Solution:
a 0 b 1 ab 2 ba 3 aba 4 aa 5 aab 6
Compressed string = 012051
x x x x x x x x x zero x x x x x x x x
x denotes a possible non-zero all other terms are zero
(a) [5] Give a sample 5 × 3 K-matrix containing unique integers and the matrix’s compact representation as described above.
Solution:
Suppose that we are defining a class K-Matrix that represents a (2n − 1) × n K-matrix in an one-dimensional array element as above.
class KMatrix { int [] element; int n; // the amount of rows in this matrix
public KMatrix(int cols) { element = new int[4*cols-3]; n = cols; } public void set(int i, int j, int newValue){ //your code goes here } }
(b) [15] Write Java code for the member method void set(int i, int j, int newValue) which stores newValue as the (i, j) element of the K-matrix repre- sented compactly as described above. Throw an IllegalArgumentException if either i or j is improper.
public void set(int i, int j, int newValue) { if (i > 2 * n - 1 || i < 1 || j > n || j < 1) throw new IllegalArgumentException(); else if (j == 1) element[i - 1] = newValue; else if (i + j == n + 1) element[2 * n - 3 + j] = newValue; else if (i - j == n + 1) element[3 * n - 4 + j] = newValue; throw new IllegalArgumentException(); }
public boolean circleChecking(){ //your code goes here public boolean circleChecking() { MyChainNode lastNode = firstNode; for (int i = 0; lastNode != null && i < size; i++) { lastNode = lastNode.next; } if (lastNode.next == null) return false;
int circleSize = 0; MyChainNode circle = lastNode.next; while (circle != lastNode) { circle = circle.next; circleSize++; } System.out.println(circleSize); return true; }
(b) [10] What is the time complexity of your code as a function of the size of MyChain? Explain. Solution: O(n) Since we consider every element at most twice. Hence O(2n) which is equal to O(n).
(b) [10] Complete the following the put method for the class PseudoQueue.
/*Add theElement to the rear of the queue/ public void put(Object theElement) { //your code goes here s1.push(theElement); }
(c) [5] What is the time and space complexity of the methods: get() and put()? Be as precise as possible.
Solution: get(), time = O(n), space = O(1) put(), time = O(1), space = O(1)
End of examination Total pages: 10 Total marks: 100