I want to blow my head off

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Set<Long> pageSet is a set of page indices and Long pageIndex is a page index. Write the condition "pageSet does not contain pageIndex" in Java. How do we add pageIndex to the set?

!pageSet.contains(pageIndex) pageSet.add(pageIndex) (Quiz 12)

Give a Java expression in terms of the integers 31, 'c', 'a', and 't' for the hash code of the String "cat". Don't use ^ or pow.

'c' x 31 x 31 + 'a' x 31 + 't' x = * sorry double asterisk *bolds* on quizlet (Quiz 10)

What is the result of removing the minimum key (leftmost) node in this tree? ---5 -1---- 6 ---3 ----8 -2-4---7-9

---5 -3----6 2-4 ---8 -------7-9 Sorry about this one quizlet hates formatting (Quiz 07)

Heap Sort is in the removal phase. The 9 has already been removed (size==7). Show the entire array (or tree) after each swap used to remove the 6. 6 4 5 1 1 3 2 9 -------------

--6-- 4 5 1 1 3 2 (9) 2 4 5 1 1 3 (6) (9) 5 4 2 1 1 3 (6) (9) 5 4 3 1 1 2 (6) (9) 2 and 6 2 and 5 2 and 3 sorry i'd write these as trees but im lazy (Quiz 09)

The following Name class will be the key for either a TreeMap or HashMap: public class Name implements Comparable<Name> { private String first, last; public Name (String first, String last) { this.first = first; this.last = last; } Implement an equals method for Name using the standard rule that two people have the same name if they have the same first name and the same last name. ****DO NOT CREATE ANY NEW STRINGS IN YOUR ANSWERS TO 7, 8, and 9.**** public boolean equals (Object o) { Name that = (Name) o; boolean firstEqFirst = this.first.equals(that.first); boolean lastEqLast = this.last.equals(that.last); // ONE LINE:

// ONE LINE: return firstEqFirst && lastEqLast; } (Quiz 10)

Questions 0-5 refer to the following radix trie INDEPENDENT of any other question. bob null null by bobby 0 ca null null lf bobcalf 1 t bobcat 2 catdog catdog 3 0. Each line is a node in the radix trie, displayed in order node.sub, node.key, and node.value. The indentation indicates sublist: "lf" and "t" are node.list for the node with node.sub="ca". The public toString() calls recursive private toString(list, indent), where indent is the number of spaces to indent. When printing out node.list for the node with node.sub="ca", what is the recursive call? 1. What is the result of put("apple", 4)? Answers to 2-5 DO NOT use this answer! 2. put("bob", 5)? 3. put("bobsled", 6)? 4. put("cat", 7)? 5. put("bottle", 8)?

0. toString(node.list, indent + 2 /* node.sub.length() */) 1. apple apple 4 bob null null by bobby 0 ca null null lf bobcalf 1 t bobcat 2 catdog catdog 3 2. bob bob 5 by bobby 0 ca null null lf bobcalf 1 t bobcat 2 catdog catdog 3 3. bob null null by bobby 0 ca null null lf bobcalf 1 t bobcat 2 sled bobsled 6 catdog catdog 3 4. bob null null by bobby 0 ca null null lf bobcalf 1 t bobcat 2 cat cat 7 dog catdog 3 5. bo null null b null null by bobby 0 ca null null lf bobcalf 1 t bobcat 2 ttle bottle 8 catdog catdog 3 (Quiz 11)

How many cycles per second in a GHz (giga Hertz)?

1,000,000,000 (Quiz 01)

For questions 1-4: a) Is it stable? b) What is the worst case O() running time? c) What is the O() running time on randomly ordered input? 1. Insertion Sort. 2. Heap Sort. 3. Quick Sort in place always using first element as pivot. 4. Merge Sort.

1. a) yes b) O(n^2) c) O(n^2) 2. a) no b) O(nlogn) c) O(nlogn) 3. a) no b) O(n^2) c) O(nlogn) 4. a) yes b) O(nlogn) c) O(nlogn) (Quiz 09)

If a single weighing can determine that a pile of coins contains one real coin, how many weighings does it take to locate the one real coin in a pile of 1000?

10(or 11) (Quiz 01)

Assuming you use binary search, what is the largest number of pages you have to look at in order to look up a word in an alphabetical dictionary with 1,000 pages? With 1,000,000 pages? With 1,000,000,000 pages?

10, 20, 30 (Quiz 02)

About how many comparisons would it take to find all matches if you COULD use binary search to look for each Milenkovic page in the list of Java pages? Again, answer as a number, not an O().

1000000 Milenkovic pages do binary search in the 1000000000 Java pages for each one. One binary search takes ?? comparisons: log2(1000000000) = 30 1000000 binary searches take ?? comparisons: 30,000,000 Unfortunately, each lookup takes a millisecond, so over 200 days! VS 1 second for #2

Show the individual changes to the array for the next insertion (the 2) in Insertion Sort. Write out the entire array each time. Assume the 6 is already removed. 1 1 3 5 9 2 6

2 1 1 3 5 9 9 6 1 1 3 5 5 9 6 1 1 3 3 5 9 6 1 1 2 3 5 9 6 (Quiz 09)

What is the minimum time to the nearest second that a computer with 16 GB RAM and a quad (4) core 2 GHz processor can locate all bytes in its RAM which are set to 'V'?

2 seconds. 2 billion*4 bytes per second = 8 billion. 16 billion/8 billion = 2 seconds (Quiz 01)

Which power of 2 is closest to a million? What is the nearest integer to log2 1000000?

2^20, 20 (Quiz 01)

A disk block is 512 (2^9) bytes. How many blocks are there on an 8T (8 * 2^40 bytes) disk drive? Give both a power of 2 and an approximate base-10 value. Can you use a 32-bit int to address those blocks?

2^43 / 2^9 = 2^34 = 2^4 * 2^30 about 16 billion NO (Quiz 12)

Heap Sort is in first, heapifying phase. Show the sequence of required swaps to finish putting the array into heap order with the **LARGEST** on top. If it helps, draw it as a tree. 3 1 9 6 5 4 2 1

3 1 9 6 5 4 2 1 3 6 9 1 5 4 2 1 9 6 3 1 5 4 2 1 9 6 4 1 5 3 2 1 the first 1 and the 6 3 and 9 3 and 4 (Quiz 09)

Quick Sort is using the 3 as the pivot. Show the swaps that partition it into elements <=3, =3, >3. 3 1 4 1 5 9 2 6

3 1 4 1 5 9 2 6 --i-----------j 3 1 4 1 5 9 2 6 ----i-------j 3 1 2 1 5 9 4 6 ------i---j 3 1 2 1 5 9 4 6 -------j -------i 3 1 2 1 5 9 4 6 ------j-i 3 1 2 1 5 9 4 6 swap j and the pivot--> 1 1 2 3 5 9 4 6 (Quiz 09)

Suppose rebalancing a binary search tree of size s takes s units of time. If we rebalance a tree whenever an add makes it unbalanced, when is the smallest multiple of n that is an upper bound on the total rebalancing time for adding n items to an (initially empty) binary search tree?

4 n Same reasoning as for array. reallocate and has capacity for 50% more before the next reallocate. rebalance and has capacity for 50% more before the next rebalance. (Quiz 08)

Each add to a binary tree also adds to all the subtrees to which the new node gets added. If we also rebalance any subtree that gets unbalanced, what is the total cost of rebalancing if we add n nodes to an empty tree?

4 n log 1.5 n O(n log n) (Quiz 08)

If name1="Victor and name2="Vincent", then name1.compareTo(name2) is -11 because 'c' comes 11 letters before 'n'. What is it if name1="Jean" and name2="Jane"?

4. (Quiz 02)

Starting with an empty stack s, perform s.push(3), s.push(1), s.push(4), s.push(1), s.pop(), s.push(5), s.pop(). What will s.peek() return?

4. (Quiz 05)

Suppose ArrayBasedPD reallocate only increases the array length by 50%. What is the smallest multiple of n that is an upper bound on the number of array writes executed by n add operations? (Hint: each new item also pays for a future move of itself and some old items.)

4n (see amortized.pdf) Each baby pays to put itself in the current array and to move itself and two old guys to the new array later (Quiz 08)

A method takes about 20 microseconds. How many times can you run it in 1 second?

50,000. (Quiz 03)

public class Computer { private int ramSize; // gibibytes private double speed; // gigaHertz public Computer (int ram, double speed) { ramSize = ram; this.speed = speed; } public int getRamSize () { return ramSize; } public int getSpeed () { return speed; } public double getPower () { return ramSize * speed; } } public class Laptop extends Computer { private double weight; public Laptop (int ram, double speed, double weight) { super(ram, speed); this.weight = weight; } public double getWeight () { return weight; } public double getPower() { return ramSize * speed / 2; } } ------------------------------------------------------------------------- 6. Why doesn't Laptop.getPower compile and what are two ways to fix it? If you fix Laptop.getPower, what will the following code (#7-#9) do? Answer compiler error, runtime exception, or what it prints out. 7. Computer c = new Laptop(8, 2.5, 7.5); System.out.println(c.getWeight()); 8. Computer c = new Laptop(8, 2.5, 7.5); System.out.println(c.getPower()); 9. Computer c = new Computer(16, 3.0); Laptop l = (Laptop) c; System.out.println(l.getPower());

6. It tries to use private variables. Making them protected. Use the getters. 7. Compiler error 8. 10 9. Runtime Exception (Quiz 01)

Implement hashCode. It should combine the hash code of the first and last name. People with names "Bernard Frank'' and "Frank Bernard'' should not hash to the same value. (Hint: why isn't "BF".hashCode() == "FB".hashCode()?) public int hashCode () { int firstCode = this.first.hashCode(); int lastCode = this.last.hashCode(); // ONE LINE:

All are viable answers. firstCode - lastCode; firstCode * 31 + lastCode; firstCode * 53 + lastCode; (Quiz 10)

This loop from remove is supposed to move all the entries after theDirectory[index] back one, but when you test it (with CAPACITY=5), it crashes. Why? How can you fix it? for (int i = index; i < size; i++) theDirectory[i] = theDirectory[i+1];

ArrayOutOfBoundsException for ( int i = index + 1; i < size; i++) theDirectory[i-1] = theDirectory[i]; (Quiz03)

Inserting into a sorted list with n entries is O(n). Since a radix trie has multiple sorted lists, why isn't inserting into a radix trie O(n) or worse?

Because each list has at most 26 elements (or whatever the size of the alphabet). (And O(26) is O(1).) (Quiz 11)

TheDirectory: Jay 1 Bob 8 Ian 2 Ann 3 Eve 6 1. Result of ArrayBasedPD.remove(1). (Don't forget: ZERO BASED INDEXING!!) 2. Result of SortedPD.remove(1). 3. Result of ArrayBasedPD.add(2, "Zoe", 777). 4. Result of SortedPD.add(2, "Zoe", 777)

Before (1.) (2.) (3.) (4.) Jay Jay Jay Jay Jay 1 1 1 1 1 Bob Eve Ian Bob Bob 8 6 2 8 8 Ian Ian Ann Zoe Zoe 2 2 3 777 777 Ann Ann Eve Ann Ian 3 3 6 3 2 Eve Eve Ann 6 6 3 Ian Eve 2 6 Sorry about this one he formatted the answers weird and i just copy-pasted (Quiz 02)

In what way does get or put of a radix trie have the SAME O() as a hash table?

Both are O(L) where L is the length of the string being gotten or put. (Quiz 11)

How might finding or adding a group of strings with a common prefix to a radix tree be made faster than finding or adding each one individually?

Don't go back to the top for each one. Start each one at the common sublist. (Quiz 11)

Suppose "Milenkovic" appears on about a million pages and "Java" appears on about a billion pages. What is the MAXIMUM number of comparisons it could take to find all pages with "Milenkovic" and "Java" using the merge-like algorithm (from #1)? Answer is a number, not an O().

I am always advancing the smaller one. It takes a comparison to tell who is smaller. I advance at most 1,001,000,000 times (Quiz 13)

How many numbers can YOUR computer (the one you use to do your homework) add up in one millisecond (1/1000 second)? If you don't know the speed of your computer, use the speed of my computer(2GHz)

If the processor is 2GHz, 2 million. (Quiz 02)

When the Jumble solver reads the word "computer" from the dictionary, what key and value does it use to store it in its Map? KEY = VALUE =

KEY = cemoprtu VALUE = computer (Quiz 08)

Can we use binary search to find a name in a sorted doubly linked list of n entries in O(log n) time? If not, why not?

No, it takes O(n) to get to the middle. (Quiz 04)

In addition to overriding remove, add, and find in SortedPD, will you need to write a sort method in case the input file is not sorted by name?

No. (Quiz 02)

What is the O() for adding a new entry at a KNOWN location is a linked list? (You are given a pointer to the next or previous entry.) Does that make SortedDLLPD.addOrChangeEntry faster than SortedPD.addOrChangeEntry? Why or why not?

O(1). No, it is not faster, as find is now O(n)(instead of O(log n)). (Quiz 04)

Suppose SortedPD uses LinkedList<DirectoryEntry> to store the entries but SortedPD.find still uses binary search. (Start at the middle, etc. Of course, it would use theDirectory.get(middle) instead of theDirectory[middle].) What would be the (worst) O() running time of find? (Hint: how many TIMES will it call theList.get(middle)?)

O(n log n) (Quiz 06)

What is the O() of the following function? 7 ( log2(n) ) + 2 ( n - 11 )

O(n) (Quiz 03)

List<E> has implementations ArrayList<E> and LinkedList<E> which implement the list as either a (partially filled) array or a (doubly) linked list. What is the (worst) O() running time of get(i) for a LinkedList of size n?

O(n) because you have to step to it (middle is worst) (Quiz 06)

What is the worst case O() running time of ArrayBasedPD.removeEntry? Is your answer for n=size or n=theDirectory.length?

O(n). n=size (Quiz03)

Give two reasons why it is important for Binge to use a queue instead of a stack in the GATHER method.

So it is breadth first. So you index the whole web site before moving on. When you get to the first web page, you index in increasing number of links. Makes lookups in the Trie faster. You may never read the first web page. So each word's list of page indices is ?? in order! (Quiz 13)

In order to find the top 100 pages out of a million matches, you use a priority queue. Why not just sort the million matches by increasing ref count and take the last 100?

Sorting takes n log n: 20,000,00 vs 1000000 times mostly comparing with the top of the queue. About a million.

Each web page is indexed by the Long integer block address of the first block of its information file (on Binge's hard disk). The same is true for each word. Given a word, we need to determine quickly which web pages contain it. Where do we store this information and in what format?

Store it in the word's file. List of Long web page indices (not the URLs) (Quiz 12)

Give at least two reasons why it is a harder task for Google to find the best ten pages with Victor Milenkovic on them than your computer finding all instances of letter 'V' in its RAM.

Takes longer than a nanosecond to access a web page. Looking for a 16 character or full name instead of just a letter. Sorts in terms of relevance. Has to deal with partial matches (and alternate spellings). Multiple trillion web pages (not 16 billion) Each web page is more like a kilobyte, not just one byte. (Quiz 01)

What is the BEST case O() running time of ArrayBasedPD.removeEntry? Which entry?

The first entry. O(1). (Quiz 03)

What is the WORST case O() running time of SortedPD.removeEntry? Which entry?

The first entry. O(n). (Quiz 03)

Why doesn't Binge use a directory TRIE to implement the map from a word to its index?

There are only about a million words. That map will fit into RAM. (Quiz 12)

Suppose we say a tree is unbalanced if the left or right subtree is more than twice the size (number of nodes) of the other. How many nodes would we have to add to a perfectly balanced tree with n nodes to make it unbalanced? What percentage increase is that?

Tree has n nodes. So n/2 in each subtree. You would have to add n/2 more nodes to the right subtree to make it twice as large (n) as the left subtree (still n/2). 50% (Quiz 08)

Explain why Google might often deal with groups of strings with a common prefix.

URLs All the URLs at the same web SITE will start with a long common prefix. (Quiz 12)

Give one way Merge Sort is worse than Heap Sort and one way it is better.

WORSE hard to do in place BETTER external sorting stable (Quiz 09)

If a class implements an interface, does it need to have a public method with the same name and parameters for EVERY method in the interface? Can the class have methods OTHER than those in the interface it implements? (Hint: ArrayBasedPD.)

Yes, and yes. (Quiz 02)

a)Suppose ArrayQueue<String> has an array theItems of length 5 with the following contents: null, "had", "a", "little", "lamb" Suppose first==1 and last==4. (Zero based indexing!!!) After offer("fleece"), what are the contents of the array and the values of first and last? Would your answer be different if that null had been "Mary"? b)Continuing from a, (disregarding my question about "Mary"), after offer("white"), what are the contents of the array and the values of first and last?

a) "fleece", "had", "a", "little", "lamb" first = 1, last = 0 no b) "had", "a", "little", "lamb", "fleece", "white", null, null, null, null first = 0, last = 5 (Quiz 06)

a) Show the steps involved in inserting 4 into the following heap: 1 6 5 9 9 7 8 b) Returning to the initial heap in the previous problem, show the steps involved in removing 1. 1 6 5 9 9 7 8

a) 1 6 5 9 9 7 8 4 1 6 5 4 9 7 8 9 1 4 5 6 9 7 8 9 b) 8 6 5 9 9 7 5 6 8 9 9 7 5 6 7 9 9 8 (Quiz 07)

a)When solving the Tower of Hanoi, the goal 4ab stands for "Move a pile of 4 disks from peg a to peg b.'' Break this goal into three subgoals. b)Which one of the subgoals of the previous answer would you push first onto the goal stack?

a) 3ac 1ab 3cb b) 3cb (Quiz 05)

a) Assuming it takes one disk seek to go to a subdirectory, what is the maximum number of disk seeks to get to the TRIE directory of the following web page? http://www.cs.miami.edu/~vjm/csc220/index.html b) Why is it likely to take fewer disk seeks than that maximum (in a)?

a) 40 (give or take) b) Similar web pages. Common prefixes. Compressed. Probably index.html is the only thing starting with i in my csc220 folder. (Quiz 12)

a) Suppose I gave you an online copy of the UM directory and asked you to record the number of people who have each first name: how many "Aaron"s, "Julie"s, "Tracy"s, etc. Then I ask you how many "Victor"s there are. What interface would you use? Which implementation? Which methods? What are their O() running times assuming m entries and n different names? What is the total O() running time? The time to look up "Victor" (or any other name)? b) Now suppose I ask you for a list of the k most popular first names, in order, with their frequencies. Which (additional) interface, implementation, methods? What total O() running time? c) Suppose instead I just want to pick a first name at random every day ("The name of the day is Lucina!") with each first name equally likely, using Random nextInt. Same questions.

a) Map<String, Integer> from first name to number HashMap or TreeMap put and get o(1) or O(log n) O(n) or o(n log n) to put them all O(1) or O(log n) b) Queue PriorityQueue or Heap offer, poll, and peek O(log k), O(log k), O(1) O(n) or (n log k) c) List ArrayList get and put O(1) O(n) to put them all in O(1) to get a random name.

a) If n-1 items are stored in a chained hash table of length n, what is the expected O() time to determine that an item with key "Victor" is *not* in the table? What is the worst O() time? b) What is the answer for an open addressed hash table? Assume all unused locations are null (no "traffic cones").

a) O(1) expected because on average the length each list is (n-1)/n or slightly less than 1. O(n) worst case if all end n-1 up in the same list (and Victor has that index too). b) If all but one space in the parking is full, how long will it take Prof. Sutcliffe to find my car? On average he has to look at n/2 cars before he gets to the empty space. O(n) Obviously worst is O(n). (Quiz 10)

a) Suppose the Jumble solver uses PDMap(SortedDLLPD) for its Map. What is the O() to store one new word into the Map? What is the O() to store all the words in the file into the Map? b) If the solver in a) takes 12 seconds to read in a dictionary of size 50,000, how many MINUTES do you predict it will take to read in a dictionary of size 500,000? c) Suppose the Jumble solver uses BST for its Map. Is it the same O() in the worst case to store in one word into the BST (as #2)? Why is it faster?

a) O(n) O(n^2) b) 10 times as large 100 times as long 12 = c 50000^2 c = 12 / 50000^2 t = c n^2 t = 12 / 50000^2 * 500000^2 t = 1200 second 20 minutes c) Yes it is still O(n) in the worst case. The worst case is sorted keys, but the alphabetized words are not in sorted. Halfway between random and sorted. Somewhat sorted. (Quiz 08)

a) An open addressed hash table contains the following keys (data not shown). find(key) returns the index of the key or where it should be inserted if it is not there. DELETED is the "traffic cone". hashIndex("pup")==7 and hashIndex("nit")==5. What are find("pup") and find("nit")? [0] "cat", [1] "dog", [2] null, [3] "rat", [4] null, [5] "ant", [6] DELETED, [7] "bug" b) If hashIndex("dog")=6, what is find("dog")? What would it be if someone incorrectly removed "cat" by setting table[0]=null?

a) find pup first goes to 7 then 0, then 1, then 2 and sees null --> pup is not there no traffic cones on the way so pup should park at ***2*** find nit first goes to 5, then 6, 7, 0, 1, 2 which is null BUT there was a traffic cone at 6, so nit parks at ***6*** b) 1 because that's where dog is if that crazy cat leaves without leaving a parking cone, then find will start at 6, then 7, then 0, which is null will think dog is not there and return ***6*** (don't forget about the traffic cone rule) (Quiz 10)

a) The array theArray[] and int size represent a COMPLETE binary tree. If theArray[i] is an item in the tree with i>0, where is its parent? b) Same situation for theArray[], size, and theArray[i]. What test tells you that theArray[i] has a left child?

a) theArray[(i - 1) / 2] b) 2 * i + 1 < size (Quiz 07)

a) Add 7 to the following binary search tree ---4 -1---5 --3--8 -2--6-9 b) Starting with the same tree (before inserting 7), show the two ways to remove 4.

a)-4 -1---5 --3--8 -2--6--9 ------7 b)3 1--5 -2---8 -----6-9 and ----5 --1----8 ---3--6-9 --2 (Quiz 07)

The first column shows the last page id read from each of three lists and the rest shows the unread portion of those lists. Show the result of reading what you should read next (to avoid skipping over matches). Just cross out numbers and write new numbers to the left (to the right of the a. and b.) a. 222 333 444 555 666 777 444 666 1234 444 555 600 666 678 880 888 b. 555 666 777 666 1234 550 600 666 678 880 888

a. 222 333 444 555 666 777 444 666 1234 444 555 600 666 678 880 888 a. 333 444 555 666 777 444 666 1234 444 555 600 666 678 880 888 b. 555 666 777 666 1234 550 600 666 678 880 888 b. 666 777 666 1234 600 666 678 880 888 (Quiz 13)

When the Jumble solver is asked to unscramble "ewting", what key does it look up in the Map?

egintw (Quiz 08)

Write a method that takes a queue as input and returns a String that is the contents of the queue separated by spaces. There can be an extra space at the end. String queueToString (Queue<String> queue) { String s = ""; return s; }

for (String data : queue) s = s + data + " "; (Quiz 06)

This loop from addOrChangeEntry is supposed to move entries forward to open up a space for a new entry at theDirectory[index]. Fill in the missing parts of the for loop. size has not been incremented yet. for (int i = ; ; ) theDirectory[i+1] = theDirectory[i];

for (int i = size()-1 ; i >= index; i--) (Quiz03)

Write a loop to print out all the names and numbers in a doubly linked list with first entry, head, and last entry, tail. The entries are of type DLLEntry, with methods getName(), getNumber(), getNext(), and getPrevious().

for( DLLEntry entry = head; entry != null; entry = entry.getNext()){ System.out.println(entry.getName()); System.out.println(entry.getNumber());} (Quiz 04)

A DLLEntry has methods getName(), getValue(), getNext(), and getPrevious(). DLLEntry variables head and tail point to the first and last elements of a linked list of DLLEntry objects, sorted by name. Implement a find method which returns the DLLEntry whose name equals name, if it is there, or the one with the next larger name, if it is not. If neither applies, return null. DLLEntry find (String name) {

for(DLLEntry entry = head; entry != null; entry = entry.getNext()){ if ( entry.getName.compareTo() <= 0) return entry;} return null; (Quiz 05)

Finish writing the time method (below) for an implementation of Fib. public interface Fib { /** The Fibonacci number generator 0, 1, 1, 2, 3, 5, ... @param n index @return nth Fibonacci number */ double fib (int n); /** The order O() of the implementation. @param n index @return the function of n inside the O() */ double o (int n); } /** Determine the time in microseconds it takes to calculate the n'th Fibonacci number AVERAGED over ncalls calls. @param fib an object that implements the Fib interface @param n the index of the Fibonacci number to calculate @param ncalls the number of calls @return the average time per call */ public static double averageTime (Fib fib, int n, int ncalls) { // Get the current time in nanoseconds long start = System.nanoTime();

for(int i = 0; i <= ncalls; i++) fibn = fib.fib(n); long end = System.nanotime(); return (end - start)/1000.0/ncalls; (Quiz 03)

Write code to add an entry with key name and value number at the HEAD of a doubly linked list. Draw the diagram to help you. You can assume there is at least one entry already in the list. DLLEntry has methods setNext(newNext) and setPrevious(newPrevious) and you are given the variable head. DLLEntry entry = new DLLEntry(name, number);

head.setPrevious(entry); entry.setNext(head); head = entry; (Quiz 04)

Implement pop for linked stack with Node variable top pointing to the top Node in the stack. Don't forget to throw an EmptyStackException. E pop () {

if (empty()) throw new EmptyStackException(); E item = top.data; top = top.next; return item; } (Quiz 05)

If stack is implemented with a List<E> theList, implement E peek () {

if (empty()) throw new EmptyStackException(); return theList.get(theList.size() - 1); } (Quiz 05)

LinkedQueue<E> uses a (singly) linked list with the following Node class. It has Node variables first and last and int variable size. Implement offer. class Node { E item; Node next; Node (E item) { this.item = item; this.next = null; } } boolean offer (E obj) { Node newNode = new Node(obj);

if (last == null) { first = newNode; last = newNode; } else { last.next = newNode; last = newNode; } size++; return true; } (Quiz 06)

Implement the compareTo method. This should correspond to the standard way to alphabetize people's names: John Jones < Jane Smith < John Smith. public int compareTo (Name that) { int firstCmpFirst = this.first.compareTo(that.first); int lastCmpLast = this.last.compareTo(that.last); if ( ) return ; else return ;

if (lastCmpLast == 0 or lastCmpLast != 0 ) return firstCmpFirst or lastCmpLast ; else return lastCmpLast or firstCmpFirst ; (Quiz 10)

Insert newEntry between previous and next in a doubly linked list. Variables head and tail point to the first and last element or are both null if the list is empty. The value of previous is null if newEntry should go at the beginning, and the value of next is null if newEntry should go at the end. DLLEntry newEntry = new DLLEntry(name, number); newEntry.setPrevious(previous); newEntry.setNext(next);

if (previous == null) head = newEntry; else previous.setNext(newEntry); if (next == null) tail = newEntry; else next.setPrevious(newEntry); (Quiz 06)

ArrayQueue<E> uses an E[] array theItems and int variables first, last, and size. Implement poll. Don't forget the empty case. You can use nextIndex(). E poll () {

if (size() == 0) return null; E data = theItems[first]; first = nextIndex(first); size--; return data; } (Quiz 06)

The methods of AbstractQueue<E> work by calling the methods that YOU WROTE in prog06. How can AbstractQueue<E> implement remove() by calling the poll() and size() you implemented? Remember, remove() should throw NoSuchElementException if there is no element to remove. E remove () {

if (size() == 0) throw new NoSuchElementException(); return poll(); } (Quiz 06)

Write a method which calculates the hash code of a String using a prime p other than 31. Don't use ^ or pow. int hashCode (String s, int p) {

int code = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); code = p * code + c; } return code; } (Quiz 10)

If a word has index wordIndex, then parents[wordIndex] is the index of its parent word. The start work has parents[startIndex] == -1. Implement method that determines the number of steps back to the start word index. int numSteps (int wordIndex) {

int count = 0; while (parents[wordIndex] != -1) { wordIndex = parents[wordIndex]; count++; } return count; for (int i = wordIndex; i != -1; i = parents[i]) count++; } (Quiz 07)

theArray is an array of DirectoryEntry references, sorted by name, with size = the number which are currently in use. DirectoryEntry has a getName method. Implement find which returns the index of the entry which has that name, but if it is not there, it returns -insert_index-1, where insert_index is the index where that name should go. Your implementation should run in O(log n) time for n=size. int find (String name) { int first = int last = while ( ) { int middle = int cmp = if (cmp < 0) else if (cmp > 0) else } return }

int find (String name) { int first = 0 int last = size-1 while ( first<=last ) { int middle = size/2; int cmp = theArray[middle].getName().compareTo(name); if (cmp < 0) first = middle + 1; else if (cmp > 0) last = middle - 1; else return middle; } return -first - 1; } (Quiz 04)

Write the method that calculates the hash index for a String s into a hash table of length m. Use the default hashCode method for String. int hashIndex (String s, int m) {

int index = s.hashCode() % m; if (index < 0) return index + m; return index; } (Quiz 10)

Suppose every subtree of a binary search tree is at most 2/3 the size of its parent subtree (which is the same as saying it is at most twice the size of the other subtree). What is the maximum number of comparisons it takes to find a key in the tree?

log 1.5 n If each subtree was 1/2 of the entire tree, then how many comparisons? Tree of size n, then n/2, then n/4, .... log 2 n comparisons. 1/2 -> log base 2 2/3 -> log base 3/2 (Quiz 08)

If a single weighing can determine that a pile of coins contains one real coin, how many weighings does it take to locate the one real coin in a pile of n?

log10(n) (Quiz 01)

Using ui.getInfo(prompt) and ui.sendMessage(message), write code that gets a name from the user and sends a message "Blank is not allowed" if the user gives you a blank name. Cancel should not cause your code to crash.

name = ui.getInfo("Enter name: "); if ( name.equals("")) ui.sendMessage("Blank is not allowed!"); break; if ( name == null) break; (Quiz 02)

Suppose root.key==4, root.right==null, and we add key==6. What is root.right after the call? (Are you sure?) Node<K,V> add (Node<K,V> root, K key, V value) { if (root == null) return new Node<K,V>(key, value); if (key.compareTo(root.key) < 0) add(root.left, key, value); else add(root.right, key, value); return root; }

null We forgot to update root.right with the value of the new subtree returned by the recursive call to add. Variable cannot change if there is no assigment (variable = something). (Quiz 07)

If my web page contains ten links to your web page, how many times should the number of references for your page be incremented? Why?

once to be fair avoid Google bombing (Quiz 12)

The IndexComparator class has a value method which returns a value for a word index (the number of steps back to the start word plus the number of letters different from the target word). Implement a compare method for ordering by increasing value. (Hint: think about compareTo for String.) int value (Integer wordIndex) { /* returns value of wordIndex */ } public int compare (Integer indexA, Integer indexB) {

return value(indexA) - value(indexB); if (value(indexA) < value(indexB)) return -1; if (value(indexA) > value(indexB)) return 1; return 0; } (Quiz 07)

Give two reasons you might use Insertion Sort instead of Heap Sort.

sorted or almost sorted faster for small n stable easy to implement (Quiz 09)

1. Suppose a method has O(log(n)) running time. It takes 30ms (milliseconds) for n=1000. What is the constant? Indicate which log you are using. 2. What is the estimated running time of the method in #1 for n=10000?

t = c(log10n) 30 = c(log10(1000)) 30 = 3c c = 10 t = 10(log10(10000)) t = 40ms (Quiz 03)

Write code to remove the last entry (tail) of a doubly linked list. You can assume that the list has at least two elements.

tail = tail.getPrevious(); tail.setNext(null); (Quiz 04)

Implement push for linked stack with Node variable top pointing to the top Node in the stack. class Node { E data; Node next; Node (E data, Node next) { this.data = data; this.next = next; } } E push (E item) {

top = new Node(item, top); return item; } (Quiz 05)

If a stack is implemented using an array E[] theArray and an int top, the index of the top element, implement the push method. You can assume a reallocate() method. E push (E item) {

top++; if (top == theArray.length) reallocate(); theArray[top] = item; return item; } (Quiz 05)

Write code to print out the contents of stack s from top to bottom and leave s empty afterwards. Use System.out.println.

while(!s.empty()) System.out.println(s.pop()); (Quiz 05)

List<Long> wordFile is a list of page indices and Long pageIndex is a page index. Write the condition "wordFile does not have pageIndex as its last element" in Java. Be careful!

wordFile.size() == 0 || wordFile.get(wordFile.size() - 1) != pageIndex !(wordFile.size() > 0 && wordFile.get(wordFile.size() - 1) == pageIndex) (Quiz 12)

The following is a directory listing of a TRIE of web pages. Directory "edu." has subdirectories "miami." and "nova.www" and so forth. What are the original web pages? (You can leave off the http://) edu. miami. c cs.www data file s.www data file www data file nova.www data file

www.ccs.miami.edu www.cs.miami.edu www.miami.edu (Quiz 12)

Which is closer to www.miami.edu alphabetically: www.cs.miami.edu or www.google.com?

www.google.com (g is closer to to m than c is) (Quiz 12)


Kaugnay na mga set ng pag-aaral

Nurs 204 - Chapter 15: Head and Neck (3)

View Set

Chap. 7 Organization, Teamwork, and Communication

View Set

Estate planning Process, Goals, and Property Ownership

View Set

The Art of Public Speaking, Ch. 11-14, 16-19

View Set

Leadership Exam 2: Ch 8- Power and Influence

View Set

CYSE 300- Introduction to Cybersecurity

View Set