Problem overview
The method undoLastAdd receives a shopping list, items, and two words, first and second. It adds first to the end of the list, adds second to the end, then undoes the last add by removing the final item, and returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes ["milk", "eggs", "bread", "jam"], and removing the last item leaves ["milk", "eggs", "bread"]. The method returns exactly that. With an empty list, "apples" and "pears", the result is ["apples"].
Approach: remove by position, not by value
The undo must take away the item at the end, even when the same word also appears earlier. With items = ["water"], first = "juice" and second = "water", the list after the two adds is ["water", "juice", "water"]. A method that searched for "water" and removed the first match would delete the wrong item. The method removes by index instead: the last item sits at index items.size() - 1.
Step by step
- Add
firstwithadd. On anArrayList,add(x)always appends to the end, so on an empty listfirstbecomes the only item. - Add
secondthe same way. It is now the last item. - Remove the item at index
items.size() - 1. Indexes start at 0, so a list of size 4 has its last item at index 3. - Return
items. The list was changed in place, so it already holds the final contents, which end withfirst.
The solution in Java
class Solution {
public List<String> undoLastAdd(List<String> items, String first, String second) {
items.add(first);
items.add(second);
items.remove(items.size() - 1);
return items;
}
}
The Java details worth knowing:
removehas two forms.remove(int index)deletes the item at that position, andremove(Object o)deletes the first item equal too. Here the argumentitems.size() - 1is anint, so the index form runs. On aList<Integer>the choice matters: with the list[10, 20, 3],remove(2)removes the item at index 2 and leaves[10, 20], whileremove(Integer.valueOf(10))removes the value 10.- The list is modified in place. The method returns the same list object that the caller passed in, now with the new contents.
- The list must be growable. An
ArrayListworks. A fixed-size list such as the one fromList.of("a")throws anUnsupportedOperationExceptiononadd.
Related list methods
The method declares its parameter as List<String>, the interface, so any list implementation can be passed in. ArrayList is the usual choice. remove(int) also returns the item it removed, which the lesson ignores. For the list [a, b, c], String removed = l.remove(l.size() - 1); gives "c" and leaves [a, b]. Java 21 adds removeLast() to lists, which does the same job without the index arithmetic, but the index form works in every Java version and is the one this lesson practises.
Edge cases and common mistakes
- Removing by value.
items.remove(second)removes the first item equal tosecond. For["water"]with"juice"and"water"it would give["juice", "water"]instead of["water", "juice"]. - Off-by-one index.
items.remove(items.size())is one past the end and throws anIndexOutOfBoundsException. - Wrong order. Removing before the second add, or adding
secondfirst, leaves the wrong word at the end. - Empty start. An empty
itemsneeds no special case: after two adds the list has two items, so the last index is valid.
Testing
["milk", "eggs"],"bread","jam"gives[milk, eggs, bread].[],"apples","pears"gives[apples].["water"],"juice","water"gives[water, juice].
