CS61B Practice Midterm #2, Exams of Data Structures and Algorithms

A practice midterm for the CS61B: Data Structures course at UC Berkeley. It contains multiple-choice and open-ended questions related to topics such as binary search trees, hash maps, and runtime analysis. The document also includes tips for taking the actual midterm, such as working on problems you are comfortable with first and not getting overly captivated by complex corner cases. The practice test has been written by a TA and is not an official exam. The document could be useful as study notes, exam, or assignment for students taking the CS61B course or similar courses in data structures.

Typology: Exams

2015/2016

Uploaded on 05/11/2023

amoda
amoda 🇺🇸

4.1

(13)

257 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UC Berkeley – Computer Science
CS61B: Data Structures
Practice Midterm #2, Spring 2016
Constructed by Sherdil Niyaz
(Please email me with any typos or other mistakes that you find)
This practice test has ? questions worth a total of ?? points. This exam has been written by a TA who has
not yet looked at the official Midterm 2. This is an unofficial practice test to give you practice for the
actual midterm. There’s a very high chance your midterm will look nothing like this. This is only a tool
to give you practice and tell you what you need to study.
“I pledge to solve all problems on this practice exam without looking at the solutions, and to never be
discouraged while studying!”
Signature: ________________________
Tips:
There may be partial credit for incomplete answers on the actual exam. Write as much of the
solution as you can, but bear in mind that we may deduct points if your answers are much more
complicated than necessary.
There are a lot of problems on this exam. Work through the ones with which you are comfortable
first. Do not get overly captivated by interesting design issues or complex corner cases you’re
not sure about.
Not all information provided in a problem may be useful.
Unless otherwise stated, all given code on this exam should compile. All code has been compiled
and executed before printing, but in the unlikely event that we do happen to catch any bugs in the
exam, we’ll announce a fix. Unless we specifically give you the option, the correct answer is not
‘does not compile.’
Ask Professor Hug to give you a hug at the end of the semester.
Optional. Mark along the line to show your feelings Before exam: [____________________].
on the spectrum between and . After exam: [____________________].
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS61B Practice Midterm #2 and more Exams Data Structures and Algorithms in PDF only on Docsity!

UC Berkeley – Computer Science CS61B: Data Structures Practice Midterm #2, Spring 2016 Constructed by Sherdil Niyaz (Please email me with any typos or other mistakes that you find) This practice test has? questions worth a total of ?? points. This exam has been written by a TA who has not yet looked at the official Midterm 2. This is an unofficial practice test to give you practice for the actual midterm. There’s a very high chance your midterm will look nothing like this. This is only a tool to give you practice and tell you what you need to study. “I pledge to solve all problems on this practice exam without looking at the solutions, and to never be discouraged while studying!” Signature: ________________________ Tips:

  • (^) There may be partial credit for incomplete answers on the actual exam. Write as much of the solution as you can, but bear in mind that we may deduct points if your answers are much more complicated than necessary.
  • (^) There are a lot of problems on this exam. Work through the ones with which you are comfortable first. Do not get overly captivated by interesting design issues or complex corner cases you’re not sure about.
  • (^) Not all information provided in a problem may be useful.
  • (^) Unless otherwise stated, all given code on this exam should compile. All code has been compiled and executed before printing, but in the unlikely event that we do happen to catch any bugs in the exam, we’ll announce a fix. Unless we specifically give you the option, the correct answer is not ‘does not compile.’
  • (^) Ask Professor Hug to give you a hug at the end of the semester. Optional. Mark along the line to show your feelings Before exam: [☹____________________☺]. on the spectrum between ☹ and ☺. After exam: [☹____________________☺].

UC BERKELEY Login: _________

  1. Tree Huggers a) If we insert n items into a BST, and these items are in sorted order (from least to greatest), what is the total runtime of all n of these insertions? Why? Draw what the tree looks like now. b) If we now removed all n of these entries in the BST in sorted order (from least to greatest), what is the runtime of all n of these operations? Why? c) Fill in the following tree so its postorder traversal is K O I N C R.

UC BERKELEY Login: _________

  1. Code Runtime a) Consider the following pseudo-code: What is the runtime, in Θ notation? Now consider the following, different pseudo-code: What is the runtime this time, in Θ notation? b) Which one of the two would you think completes faster?

CS61B PRACTICE MIDTERM II, SPRING 2016

Login: _______

  1. Dat Hash Doe After acing 61B, you just landed an internship at MyFace , a hip new startup in Silicon Valley! Congratulations on being on your way to six figs respect. Unfortunately, the other intern on your team is from that Junior College across the bay. After enjoying all of the free food, nap pods, and complementary rickshaw service that MyFace has to offer, the two of you get into an argument about hashing (once you stop goofing off and actually start working on your project together). a) Your friend from that “other” school is building a hashmap. For insertion, they take the value produced by calling .hashcode() on some random Java object (you can’t assume anything about how .hashcode() works). Your friend then uses this value as the index of the bucket that this random item will map to. Is this correct? It so, explain why. If not, why is it broken, and how would you fix it? b) Your friend suggests another, simpler hash function. All items will map to bucket zero, no matter what! Simplicity at its finest. What is the runtime of any lookup operation in terms of N, the number of items in a map using this hash function? Use Θ notation. c) After settling your disagreements, you and your friend decide to build a hash map together. Assume its a good one that works properly. This map resizes by a factor of 3 after exceeding a load factor of 1. Your friend is a bit antsy, and gives the following explanation for their worries: “This seems weird. Usually this thing has constant runtime on an insert operation, but every now and then it has to resize which takes a bunch of operations. I’m not sure we can hit constant runtime on average .” You believe that this is possible. Using this page and the next, give a mathematical argument on why you’re right. ( Hint : Amortized analysis!). You can assume that insert always runs in constant time.

CS61B PRACTICE MIDTERM II, SPRING 2016

Login: _______

  1. I Like My Trees Bushy a) Which of the figures below represent legal left-leaning red-black trees?

UC BERKELEY Login: _________ b) Draw the following B-Tree (2-3-4 Tree) after you perform insert(43): : c) The smallest number of keys in a valid 2-3-4 Tree of height 3 (where the root is at height 0) is…..?

UC BERKELEY Login: _________

union(B, G)