Problem overview
The function undoLastAdd receives a shopping list items and two words, first and second. It adds first to the end of the list, then adds second to the end, then undoes the last add by removing the item at the end. It returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes [milk, eggs, bread, jam] and then loses its last item, so the result is [milk, eggs, bread]. With an empty list, first = "apples" and second = "pears", the result is [apples].
Approach
The parameter is a MutableList<String>, a list that can change in place. Kotlin's plain List is a read-only view with no add, so the mutable type is what makes this exercise possible. Three calls do all the work: add, add and removeAt.
The undo must remove the item at the end, not "the word second". The two are different when the same word is already in the list. With items = ["water"], first = "juice" and second = "water", the list after the adds is [water, juice, water]. Removing by position takes away the last water and leaves [water, juice].
Step by step
- Add
first.addappends to the end, so on an empty listfirstbecomes the only item. - Add
second. It now sits afterfirst, as the last item. - Remove the last item by index. The last index of a list is
size - 1, andremoveAtdeletes the item at a given index. - Return the list. The list was changed in place, so returning
itemsreturns the updated list.
The solution in Kotlin
class Solution {
fun undoLastAdd(items: MutableList<String>, first: String, second: String): MutableList<String> {
items.add(first)
items.add(second)
items.removeAt(items.size - 1)
return items
}
}
Some details worth knowing:
removeAtandremoveare different.removeAt(index)deletes the item at a position.remove(element)deletes the first item equal to the value you pass, which would remove the earlierwaterin the third example and give the wrong list.- The list is changed in place. The function doesn't build a new list. The caller's list is the one that grows and shrinks, and the returned value is that same list. Calling it with
mutableListOf("a"),"b"and"c"returns a reference to the list that was passed in, and the list holds[a, b]afterwards. sizeis a property. In Kotlin you writeitems.size, notitems.size().
Removing by value versus by position
The difference between remove and removeAt is the heart of this lesson, so it is worth seeing on the third example. Take the list ["water", "juice", "water"]. Calling remove("water") deletes the first matching item and leaves [juice, water]. Calling removeAt(list.lastIndex) deletes the item at the end and leaves [water, juice]. lastIndex is a property that equals size - 1, and either spelling works for the last position.
The undo is defined by position, "the last thing added", so use the position form.
Edge cases and common mistakes
- Empty list. The two adds run before the removal, so the list always has at least two items when
removeAtruns. The empty case works. - Repeated words. Use the index, as above, so that the item just added is the one removed.
- Removing before adding both words. The undo comes last. Doing it earlier removes the wrong item.
- Forgetting
return. The function declares a return type, so a body withoutreturn itemsdoesn't compile.
Testing
items = ["milk", "eggs"],first = "bread",second = "jam"gives[milk, eggs, bread].items = [],first = "apples",second = "pears"gives[apples].items = ["water"],first = "juice",second = "water"gives[water, juice].
