Thelastcommonmistakewasnotreadingtheimplementationscor

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: e B, Dequeue: linear time; need to traverse the entire list to access the next­to­last node Common Mistakes: The most common mistake was B, dequeue ­ not realizing that since you don’t have previous pointers, you can’t access the next­to­last node without traversing the entire list in linear time. Another common mistake was not realizing that removing or adding something to the beginning of an arrayList (at index 0) requires a shift of all the items in the ArrayList, which is linear. However, adding to the end or removing the end of an ArrayList is in constant time because you can access ArrayLists by index (ArrayList is implemented as an Object array). To access the last item we can do something along the lines of arrayList.get(size() ­ 1). It is NOT necessary to traverse the whole arraylist to find the last index. The last common mistake was not reading the implementations correctly, particularly for B­enqueue... Many people were trying to enqueue the new node to the back of the list even though according to the implementation the back of the queue is the front of the list (so you want to add to the front of the list). Question 3A: (3B on Exam B) Sample Solution: Changing the hashCode() function causes every value to have the same hash code (10). This means they all hash to the same bucket, and a very large chain will form. This will cause adding new keys, or looking up values, to be very slow, as we now need to essentially look through a LinkedList. This loses any benefits the HashMap would’ve given us. Common Mistakes: Most points lost here were for not being specific in answers. For example, stating that there would be many collisions (or many items in the same bucket), but not explaining that it causes the HashMap to act like a LinkedList or run very slowly. Question 3B: (3A on Exam B) Sample Solution: When a key is added to the HashMap, HashMap first computes the hashCode of the key to determine its bucket. Then, within that bucket, it uses .equals() to determine if the new key is alre...
View Full Document

This note was uploaded on 01/24/2014 for the course CS 61bl taught by Professor Courney during the Summer '11 term at University of California, Berkeley.

Ask a homework question - tutors are online