Docsity

Midsemester Exam: Data Structures and Algorithms - Prof. Mark Weiss, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
COP 3530
Data Strutures
Midsemester Exam Version A
Name:
June 22, 2004
This exam has 4 questions. Eah question starts on a new page. Please answer eah question on its page. You
may assume
java.util
has been imported. There will be no dedutions for lak of ommenting. There will be no
dedutions for lak of import diretives. There will be no dedutions for minor syntax errors.
pf3
pf4
pf5

Partial preview of the text

Download Midsemester Exam: Data Structures and Algorithms - Prof. Mark Weiss and more Exams Data Structures and Algorithms in PDF only on Docsity!

COP 3530

Data Stru tures

Midsemester Exam Version A

Name:

June 22, 2004

This exam has 4 questions. Ea h question starts on a new page. Please answer ea h question on its page. You may assume java.util has b een imp orted. There will b e no dedu tions for la k of ommenting. There will b e no dedu tions for la k of imp ort dire tives. There will b e no dedu tions for minor syntax errors.

  1. [50 p oints℄ Metho d hasSum returns true if the array ontains either one or two di erent items that sum to parameter sum exa tly and false otherwise:

publi stati boolean hasSum( int [ ℄ arr, int sum ) { for( int i = 0; i < arr.length; i++ ) { if( arr[ i ℄ == sum ) return true;

for( int j = i + 1; j < arr.length; j++ ) if( arr[ i ℄ + arr[ j ℄ == sum ) return true; }

return false; }

(a) What is the Big-Oh running time of hasSum? (b) If it takes 8 millise onds to return false for 1000 items, approximately how long would it take to return false for 2000 items? ( ) Using the Colle tions API, des rib e an algorithm (in English, no o de) that is more eÆ ient than the one ab ove, and provide the running time of your algorithm, with a brief justi ation. (d) Do es a hange to the problem that requires exa tly two items to sum (rather than allowing a single item) make the problem easier in a Big-Oh sense? Why/why not?

  1. [50 p oints℄ Assume that you have a java.util.Map in whi h the keys are names of students (stored as a String), and for ea h student, the value is a java.util.List of ourses (ea h ourse name is a String) that the student is enrolled in.

(a) Write a routine that given a student, returns the list of ourses the student is enrolled in. (b) Write a routine that given a ourse, returns the list of students registered for the ourse.

publi stati List getCourses( Map m, String student ) {

publi stati List getStudents( Map m, String ourse ) {

  1. [50 pts℄ Metho d hasSum returns true if the array ontains zero or more items that sum to parameter sum exa tly and false otherwise. Example: arr is [3, 4, 9, 1℄. If alled with sum = 8, the result is true (3,4,1); if alled with sum = 0, the result is true (use zero elements); if alled with sum = 11, the result is false. The easiest way to implement hasSum is to use re ursion. The publi driver and the start of the re ursive routine is shown. Implement the re ursive routine.

publi stati boolean hasSum( int [ ℄ arr, int sum ) { return hasSum( arr, 0, arr.length - 1, sum ); }

// Returns true if some subset of elements in arr[low..high℄ adds to sum // returns false otherwise // HINT: BASIC IDEA IS TO // return true if some a subset of elements in arr[low+1..high℄ adds to sum OR // some subset of elements in arr[low+1...high℄ adds to sum-arr[low℄ publi stati boolean hasSum( int [ ℄ arr, int low, int high, int sum ) {