{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Midterm 2 Solutions 13

Thelastcommonmistakewasnotreadingtheimplementationscor

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern