See the criteria on Gradescope for how grades of E/S/N were assigned for each problem.

Question 1 - Conceptual

Part A: Select all lists that would show the bug if passed to removeNegatives

  • [-3, -3, -3]
  • [-10, -5, 0]

Any list that had two side-by-side negative values.

Part B Assertions

Point A Point B Point C Point D Point E
s.isEmpty() S N N N S
s2.isEmpty() A A S S S
list.isEmpty() S S N S S

Part C Explain in Plain English

Option A: “For each line in the given file, prints the sum of all numbers, ignoring non-numeric input.”

Question 2 - Tracing

  • {}
  • {'c'={"c", "cc"}, 'd'={"d", "dd"}}
  • {'f'={"f", "ff"}}
  • {'i'={"e", "o"}, 'j'={"r", "w"}}

Key-value pairs with odd length sets were deleted.

Question 3 - Debugging

The early return within seatParty() was causing the bug. One possible fix was to delete the return foundParty; found within the loop.

Note that we accepted other possible answers so long as the bug was correctly identified and fixed.

Question 4 - Collections Programming

One possible solution appears below.

public Map<String, Set<String>> aveRestaurants(Map<String, Map<String, Double>> ratings, double target) {
    Map<String, Set<String>> result = new TreeMap<>();
    for (String person : ratings.keySet()) {
        result.put(person, new TreeSet<>());

        Map<String, Double> ratingsFor = ratings.get(person);
        for(String restaurant : ratingsFor.keySet()) {
            if (ratingsFor.get(restaurant) >= target) {
                result.get(person).add(restaurant);
            }
        }
    }
    return result;
}

Question 5 - Objects Programming

One possible solution appears below.

public class VegetableGarden implements Garden {
    private int maxSpots;
    private Map<String, Integer> plantsToNum;
    private Map<String, Double> plantsToTemp;

    public VegetableGarden(int maxSpots) {
        if (maxSpots < 0) {
            throw new IllegalArgumentException();
        }
        this.maxSpots = maxSpots;
        this.plantsToNum = new HashMap<>();
        this.plantsToTemp = new HashMap<>();
    }

    public void plant(String name, int num, double temp) {
        if (getTotalPlanted() + num > maxSpots) {
            throw new IllegalArgumentException();
        }
        if (!plantsToNum.containsKey(name)) {
            plantsToNum.put(name, 0);
        }
        plantsToNum.put(name, plantsToNum.get(name) + num);
        plantsToTemp.put(name, temp);
    }

    public int getOccupiedSpots() {
        int total = 0;
        for (int n : plantsToNum.values()) {
            total += n;
        }
        return total;
    }

    public void pick(String name) {
        if (!plantsToNum.containsKey(name)) {
            throw new IllegalArgumentException();
        }
        int newCount = plantsToNum.get(name) - 1;
        if (newCount > 0) {
            plantsToNum.put(name, newCount);
        } else {
            plantsToNum.remove(name);   // not strictly necessary - could check for 0 in other methods
            plantsToTemp.remove(name);  // not strictly necessary - could check for 0 in other methods
        }
    }

    public int getMaxSpots() {
        return this.maxSpots;
    }

    public Set<String> needsShade(double temp) {
        Set<String> result = new TreeSet<>();
        for (String plant : plantsToTemp.keySet()) {
            double plantTemp = plantsToTemp.get(plant);
            if (temp > plantTemp + 10) {
                result.add(plant);
            }
        }
        return result;
    }

    public void copyInto(Garden other) {
        if (other.getMaxSpots() < other.getTotalPlanted() + this.getTotalPlanted()) {
            throw new IllegalArgumentException();   // not strictly necessary - plant will throw the exception
        }
        for (String plant : plantsToNum.keySet()) {
            int num = plantsToNum.get(plant);
            int temp = plantsToTemp.get(plant);
            other.plant(plant, num, temp);
        }
    }
}

Question 6 - Stacks & Queues Programming

One possible solution appears below.

public static int removeMin(Stack<Integer> s) {
    Queue<Integer> q = new LinkedList<>();

    // Find the minimum value
    int min = s.pop();      // could set to Integer.MAX_VALUE;
    q.add(min);
    while(!s.isEmpty()) {
        int next = s.pop();
        if (next < min) {
            min = next;
        }
        q.add(next);
    }

    // Remove all non-minimum values
    while (!q.isEmpty()) {
        int next = q.remove();
        if (next != min) {
            s.push(next);
        }
    }

    // Optional: Return to original order
    s2q(s, q);
    q2s(q, s);
    return min;
}

If you want to check if your solution works, please use this program to test out your solution. If your solution works as you wrote it without modifying the logic of your solution and you did not receive an E, you should submit a regrade request.