Docsity

CS 61BL Midterm 2, Summer 2019, Exams of Data Structures and Algorithms

Toggle sidebar
bg1
CS 61BL Data Structures & Programming Methodology
Summer 2019 Midterm 2
This exam has 7questions worth a total of 45 points and is to be completed in 110 minutes.
The exam is closed book except for two double-sided, handwritten cheat sheets. No calculators or
other electronic devices are permitted. Give your answers and show your work in the space provided.
Write the statement below in the blank provided and sign. You may do this before the exam begins.
“I have neither given nor received any assistance in the taking of this exam.”
Signature:
Question Points
1 5
2 7
3 12
4 0
5 6
6 6
7 9
Total 45
Name
Student ID
GitHub account # su19 - s
Lab Section #
Name of person to left
Name of person to right
•There may be partial credit for incomplete answers. Write as much of the solution as you can,
but we may deduct points if your answers are much more complicated than necessary.
•Work through the problems 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, and you may not need all lines. For
code-writing questions, write only one statement per line and do not write outside the lines.
•Unless otherwise stated, all given code on this exam should compile. All code has been compiled
and executed, but in the event that we do 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.’
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS 61BL Midterm 2, Summer 2019 and more Exams Data Structures and Algorithms in PDF only on Docsity!

CS BL Data Structures & Programming Methodology

Summer  Midterm 

This exam has  questions worth a total of  points and is to be completed in  minutes. The exam is closed book except for two double-sided, handwritten cheat sheets. No calculators or other electronic devices are permitted. Give your answers and show your work in the space provided.

Write the statement below in the blank provided and sign. You may do this before the exam begins.

“I have neither given nor received any assistance in the taking of this exam.”

Signature:

Question Points             Total 

Name

Student ID

GitHub account # su19 - s

Lab Section #

Name of person to left

Name of person to right

  • There may be partial credit for incomplete answers. Write as much of the solution as you can, but we may deduct points if your answers are much more complicated than necessary.
  • Work through the problems 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, and you may not need all lines. For code-writing questions, write only one statement per line and do not write outside the lines.
  • Unless otherwise stated, all given code on this exam should compile. All code has been compiled and executed, but in the event that we do 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.’

CS BL Midterm , Summer 

. ( pts) Mechanical Balanced Search Trees

We learned that - Trees have a one-to-one correspondence with Left-Leaning Red-Black Trees. For each subpart in this question, take the - Tree given in the left column and perform the indicated operation on it, drawing the resulting - Tree below the given tree. Then convert both the given - Tree and the - Tree you drew into LLRB Trees, drawing them in the right column next to their counterparts.

Indicate a node is red by putting an asterisk (*) next to it. You must use the conventions and procedures we taught in lab this summer. Only your final answer within the boxes will be graded; perform any scratch work (which will not be graded) outside of the boxes.

(a) insert(5);

CS BL Midterm , Summer 

. ( pts) Heaps Spark Joy

Shown below is a binary Min Heap , in which each symbol represents a node in the heap. The values contained within the nodes are comparable, and are not necessarily unique (except for the allowance of duplicate values here, all of the heap invariants from lab apply).

For each of the following questions, select all possible relationships that could occur between the values represented by the letter and symbol shown. IMPORTANT: For this entire question, assume that X > Y. Erase any undesired markings from boxes completely if you change your answer.

(a) Mark all that could be true of X :

2 X < 2 X = 2 X >

2 X < 2 X = 2 X >

2 X < 2 X = 2 X >

(b) Mark all that could be true of Y : 2 Y < 2 Y = 2 Y >

2 Y < 2 Y = 2 Y >

2 Y < 2 Y = 2 Y >

(c) Representing the heap using the array representation from lab (in which elements begin starting at index ), at what index will Y be located at? Write your integer answer in the blank below.

Answer:

Images courtesy of Google Noun Project.

Name:

. ( pts) #BARTable

Assume the standard java.util.LinkedList is imported everywhere as needed in this question.

Bay Area Rapid Transit is the local rail and subway transportation system. From the nearby Downtown Berkeley station, you can travel to a number of exciting destinations in varying amounts of time. Our goal here is to print the names of all the stations, ordered by their travel times from Berkeley (from shortest to longest). A Station is represented as:

public class Station { String name; int travelTime; LinkedList children = new LinkedList<>();

public Station(String name, int travelTime) { this.name = name; this.travelTime = travelTime; } }

Note that the travelTime of a Station represents the travel time from the Berkeley station (not from another neighboring station).

We can build a tree of Stations rooted at the Berkeley station:

From this example tree, the expected result should be: berkeley macarthur elcerrito embarcadero civiccenter dublin

Assume there are no cycles/loops in our rail system, and that every child station has a travelTime that is strictly greater than that of its parent.

Name:

(c) What happens if the @Override line is deleted from above the StationComparator’s compare function, assuming that all of the blanks are filled in with the correct solution? (Choose one answer.)

Compiler Error

Runtime Error

Unexpected Behavior

No Difference

(d) Another way to solve this problem is to use a BFS-inspired approach. But before we fully implement that in part e, let’s first remind ourselves of the standard BFS procedure.

Run BFS (Breadth First Search) as you learned in lab on the example tree from before (repro- duced below), starting with the berkeley station. When inserting multiple children into the fringe, insert in order from lowest to highest travel time.

BFS Traversal (one station name per line):

In general, this doesn’t always work in helping us print the station names in order of travel time, so your answer here may or may not match the example’s expected output.

CS BL Midterm , Summer 

(e) A little trick resolves the problem we have with ordinary BFS: use dummy nodes such that the travelTime difference between any two consecutive nodes (whether a real Station or just a dummy node) is 1. For example, as shown below, since sanbruno has a child sfo that is 3 more away from it than its own travelTime of 122 , we use two dummy nodes between them.

Implement print2 to do the same thing as print but using this BFS approach instead. For your fringe, we will use a LinkedList, which can behave as either a stack or queue, depending on your usage of its addFirst, addLast, removeFirst, removeLast methods.

public static void print2(Station berkeley) { LinkedList fringe = new LinkedList<>(); fringe.add(berkeley);

while ( ) {

Station curr =

if ( ) { System.out.print(curr.name + " "); } for ( ) {

if ( ) {

Station dummy = new Station(null, );

} else {

CS BL Midterm , Summer 

. ( pts) Lost and Found

A Lost and Found is a place where lost items are turned in and where people looking for their lost items can check to see if they’ve been found.

We are managing a Lost and Found, and we decided to use a HashMap to store people’s lost belongings. When someone finds a lost item and brings it to us, we create a Ticket, and store it and the item in the map. When someone asks us to look for an item they had lost, we make a new Ticket and use that to query our map. Since our lost and found just opened, the map is initially empty.

(a) To be able to use Ticket instances as keys in our map, we need to implement the equals method. Two Tickets should be considered the same if the location Strings spell out the same word, and if the provided function says the descriptions are similar enough.

public class Ticket { String location; String description;

public boolean equals(Object o) { if (!(o instanceof Ticket)) {

return ; } Ticket t = ; // The next two blanks are for one long statement.

// Returns true if the two argument strings are "similar enough". private static boolean isSimilar(String d1, String d2) { // Implementation not shown. }

public int hashCode() { return location.hashCode() + description.hashCode(); } }

Name:

(b) In one situation, a Phone was turned in to us. A Ticket t1 was made for it and this pair was put into the map. When the owner came for it, a Ticket t2 was made, and the Phone was successfully retrieved from the map using t2.

In another situation, a Charger was turned in to us. A Ticket t3 was made for it and this pair was put into the map. After that, we found out that the Charger was actually found in a different location, and so we updated t3 using a reference we still had to it (i.e. we used a line like t3.location = ...). When it was to be retrieved, a Ticket t4 was made. t4 should have matched the updated t3, but the map strangely returned null rather than the Charger as desired.

Why? Write no more than  words explaining this. Take note of the hashcode function from the Ticket class from part a.

(c) Subsequent to the events from the previous part, one thousand Shirts were turned in to us, and so we made a Ticket for each Shirt, and put them all into the map. After that, we again tried looking for t3 using t4, and miraculously the Charger is successfully retrieved this time.

Why? Write no more than  words explaining this.

Name:

(b) In the tree below, again the downward triangles represent minimizers and the upward triangles represent maximizers. The provided values shown on the non-leaf nodes are the values with alpha-beta pruning already performed. The branches with X’s through them represent branches that have been pruned.

For each leaf node that is labeled with a letter, of the value options listed, mark all values for which it can be, given all of the provided values and also the branches that are pruned. You may make notes on this tree diagram, but it will not be graded.

A B 

×

C  D 

×

E  F 

×

(a) Mark the checkbox next to all of the values that Node A can be.

(b) Mark the checkbox next to all of the values that Node B can be.

(c) Mark the checkbox next to all of the values that Node C can be.

(d) Mark the checkbox next to all of the values that Node D can be.

(e) Mark the checkbox next to all of the values that Node E can be.

(f) Mark the checkbox next to all of the values that Node F can be.

CS BL Midterm , Summer 

. ( pts) Decision Tree

Here you will implement a tree structure that is commonly used in machine learning since it naturally resembles an easy to interpret flow-chart. The non-leaf attributes and split values are hard-coded here, but typically these would be learned from training data.

Assume the standard java.util.HashMap is imported everywhere as needed in this question.

(a) Every day when lunchtime rolls around, Christine, Matt, and Jackson experience a different craving, which can be described by several Attributes. In order for us to determine whether a restaurant will satisfy our overall craving, we need to be able to compare individual Attributes of our craving and the Attributes a restaurant can offer.

Fill in the blank below so that Attribute implements the Comparable interface.

public class Attribute implements Comparable { public String name; public int value;

public Attribute(String name, int value) { this.name = name; this.value = value; }

/**

  • Returns a negative integer, zero, or a positive integer as this Attribute
  • is less than, equal to, or greater than the specified Attribute in value.
  • Assumes that the Attributes have names that are .equals(). */ @Override public int compareTo(Attribute o) {

CS BL Midterm , Summer 

Fill in the blanks to complete the predict function implementations such that a provided craving will find its way to the appropriate Leaf and return the restaurant it best describes. You may not need all lines provided. No more than one statement per line.

public class Node { public Node left, right; public Attribute attr;

public Node(String name, int value) { this.attr = new Attribute(name, value); }

String predict(HashMap craving) {

int c =

if ( ) {

} else {

public class Leaf extends Node { public String restaurant;

public Leaf(String restaurant) { super(null, -1); this.restaurant = restaurant; }

Name:

(c) Answer the following runtime questions in terms of the following variables:

  • N, the number of Nodes in the binary decision tree. Make sure to use the correct asymptotic bound notations ( O , Ω, Θ) and to write them clearly (if you mean to write O , do not put a little loop at the top, because it may be interpreted as a Θ). Simplify your answers.

What is the best case runtime of calling predict on the root of the decision tree and making recursive calls until we get a String answer? Note that the decision tree is not necessarily balanced. Assume that deletions are never performed on our tree (if this fact is relevant to your analysis).

The worst case?