



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




This exam has 4 questions. Each question starts on a new page. Please answer each question on its page. You may assume java.util has been imported. There will be no deductions for lack of commenting. There will be no deductions for lack of import directives. There will be no deductions for minor syntax errors.
// Returns true if two numbers in c when added together sum // to a third number in c. public static boolean hasSpecial( List
return false; }
Assume that the lists have N items each.
(a) What is the running time of hasSpecial, as written above if the list is an ArrayList? (b) What is the running time of hasSpecial, as written above if the list is a LinkedLists? (c) Suppose it takes 2 seconds to run hasSpecial on a 1,000 item ArrayList. How long will it take to run hasSpecial on a 3,000 item ArrayList? (d) Suppose as a first step, the items in c are copied into an array and sorted by using mergesort. Provide the remaining steps that will allow you to implement hasSpecial faster than the original algorithm above. You do not have to write any code; just describe the key steps.
{ hello=[2,3], good=[1,2,3,4], this=[10], zoo=[10,20,40] }
then the List returned by sumsToTen is
["good","this"]
(a) Write this routine below, using Java 5. (b) Assuming that the Map is a TreeMap, provide the Big-Oh running time of your routine.
interface GridElement { int getRow( ); int getCol( ); boolean containsWater( );
List
Implement method getWaterArea:
public static int getWaterArea( GridElement pos )
getWaterArea accepts a GridElement as a parameter. If the GridElement represents land, getWaterArea returns 0. Otherwise it returns the number of GridElements that are connected to the GridElement parameter via water, including the parameter itself. For example, in the following grid, in which W is water and L is land:
the results of getWaterArea for a few positions are as follows:
GridElement pos getWaterArea( pos ) =============== =================== (0,0) 2 (0,3) 3 (0,5) 5
In implementing getWaterArea, you may create private helpers as you see fit, but you must implement them. You MAY NOT declare any additional static data members. IMPLEMENT getWaterArea ON THE FACING PAGE.