See the criteria on Gradescope for how grades of E/S/N were assigned for each problem.
Part 1
Question 1 - Conceptual¶
Part A The following are true: Option C: A TreeMap stores keys in sorted order Option D: A HashMap is generally more efficient than a TreeMap
Part B Assertions
Point A | Point B | Point C | Point D | Point E | |
---|---|---|---|---|---|
list.isEmpty() | S | N | N | N | N |
s.length() == num | S | N | A | N | S |
count > 0 | S | N | S | A | S |
Part C Explain in Plain English
Option C : Returns a map containing all key value pairs that exist in both m1 and m2
Question 2 - Collections Programming¶
One possible solution is shown below.
public static Map<Integer, Integer> countMatchingLength(Map<Integer, List<String>> m) {
Map<Integer, Integer> counts = new TreeMap<>();
for (int length : m.keySet()) {
List<String> strs = m.get(length);
int count = 0;
for (String word : strs) {
if (word.length() == length) {
count++;
}
}
counts.put(length, count);
}
return counts;
}
Question 3 - Objects Programming¶
One possible solution is shown below.
public class BasketballTeam implements Team {
private String mascot;
private Map<String, Integer> players;
public BasketballTeam(String mascot) {
this.mascot = mascot;
players = new TreeMap<>();
}
public String getMascot() {
return mascot;
}
public void addPlayer(String player, int points) {
if (players.containsKey(player) || points < 0) {
throw new IllegalArgumentException();
}
players.put(player, points);
}
public int getTotalPoints() {
int total = 0;
for (String player : players.keySet()) {
int points = players.get(player);
total += points;
}
return total;
}
public String getTopScorer() {
if (players.size() == 0) {
throw new IllegalStateException();
}
int mostPoints = 0;
String topScorer = "";
for (String player : players.keySet()) {
int points = players.get(player);
if (points > mostPoints) {
mostPoints = points;
topScorer = player;
}
}
return topScorer;
}
public boolean hasMorePoints(Team other) {
return this.getTotalPoints() > other.getTotalPoints();
}
public String toString() {
return mascot + ": " + players.size() + " players";
}
}
Part 2
Question 1 - Tracing¶
- First call
[12, 24]
- Second call
[5, 10, 22, 30, 35]
- Third call
[0, 10, 21, 39, 45]
- Fourth call
[4, 13, 22, 31, 41]
Question 2 - Debugging¶
- Part A: Line 5 was the line causing the bug (condition was incorrect)
- Part B: One possible fix to line 5 was to change the condition to be
if (num < min || num > max)
.
Question 3 - Stacks & Queues Programming¶
One possible solution is shown below.
public static void maxMirroredPairs (Queue<Integer> q) {
Stack<Integer> s = new Stack<>();
// move front half -> stack
int size = q.size();
for (int i = 0; i < size / 2; i++) {
s.push(q.remove());
}
// if odd, move middle element to the back
if (size % 2 == 1) {
q.add(q.remove());
}
// compare top of stack with front of queue, keep the larger one
while (!s.isEmpty()) {
int num1 = s.pop();
int num2 = q.remove();
q.add(Math.max(num1, num2));
}
// reverse order
qToS(q,s);
sToQ(s,q);
}