Problem overview
The method undoLastAdd receives a shopping list, items, which is an ArrayBuffer[String], and two words, first and second. It adds first to the end, adds second to the end, then undoes the last add by removing the last item, and returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list grows to milk, eggs, bread, jam. The undo removes jam, so the result is milk, eggs, bread.
Approach
An ArrayBuffer is Scala's growable, mutable sequence. Unlike a List or Vector, it changes in place, so the method can modify the buffer it was given and return it. Three operations are enough: append, append, and remove by position.
The undo must remove the item at the end, by position, not "the item equal to second". Removing by value would take away the first matching word, which is the wrong one when the same word is already in the list.
Step by step
- Append
firstwith+=. On an empty list it becomes the only item. - Append
secondthe same way. It is now the last item. - Remove the item at index
items.size - 1, the last valid index, withremove. - Return
items.
The solution in Scala
class Solution {
def undoLastAdd(items: ArrayBuffer[String], first: String, second: String): ArrayBuffer[String] = {
items += first
items += second
items.remove(items.size - 1)
return items
}
}
Some details worth knowing:
+=on anArrayBufferis a method call that appends in place. The buffer is aval-like parameter, but its contents can change.remove(i)with anIntremoves the element at indexiand returns it. Here the returned word is simply ignored.- Indexes start at 0, so the last index is
size - 1. After the two adds the buffer always has at least two items, so that index is always valid. - In a file of your own, add
import scala.collection.mutable.ArrayBufferto use the type by its short name.
Edge cases and common mistakes
- Repeated words. With
items = ["water"],first = "juice"andsecond = "water", the buffer becomeswater, juice, water, and removing the last index leaveswater, juice. The originalwaterstays. - Empty list. With
items = [],first = "apples"andsecond = "pears", the result isapples. - Off-by-one.
items.remove(items.size)throws an exception, because the indexsizeis one past the end.
What the method really does
Add, add, undo: the net effect is that only first ends up on the list, and second is added and removed again. The exercise is there to practise the three operations that make an undo feature work in any list-based program: append to the end, remove from the end, and find the end with size - 1. A real undo would remove whatever was added last, not a fixed word, and the index size - 1 is what makes that possible.
Why the buffer is changed in place
Because ArrayBuffer is mutable, the method doesn't build a new list. It edits the one it received, and the returned value is that same buffer. Anyone still holding the original reference sees the changes too. Tracing items = ["water"] with first = "juice" and second = "water" shows the buffer after each line:
after items += first ArrayBuffer(water, juice)
after items += second ArrayBuffer(water, juice, water)
after the remove ArrayBuffer(water, juice)
Removing by value is a different operation
The operator -= removes an element by value, and it removes the first match. On ArrayBuffer(water, juice, water), -= "water" leaves ArrayBuffer(juice, water): the old item went, not the one just added. That is why this lesson removes by index.
Testing
These calls give:
undoLastAdd(ArrayBuffer("milk", "eggs"), "bread", "jam") -> ArrayBuffer(milk, eggs, bread)
undoLastAdd(ArrayBuffer(), "apples", "pears") -> ArrayBuffer(apples)
undoLastAdd(ArrayBuffer("water"), "juice", "water") -> ArrayBuffer(water, juice)
