Start learning free

LESSON 119 OF 291 · ARRAYS - CHALLENGES

Undo the Last Add in Kotlin: MutableList add and removeAt

Practice Read the full explanation

You keep a shopping list, an ArrayList of strings items. Add the word first to the end of the list, then add the word second to the end, then undo the last add by removing the last item. Return the list.

The undo always removes the item at the end of the list, even when the same word also appears earlier in the list.

 

Example 1:

Input: items = ["milk", "eggs"], first = "bread", second = "jam"
Output: ["milk", "eggs", "bread"]
Explanation: After the two adds the list is ["milk", "eggs", "bread", "jam"]. The undo removes "jam", the last item.

Example 2:

Input: items = [], first = "apples", second = "pears"
Output: ["apples"]
Explanation: The list starts empty. "apples" and "pears" are added, then "pears" is removed.

Example 3:

Input: items = ["water"], first = "juice", second = "water"
Output: ["water", "juice"]
Explanation: The list becomes ["water", "juice", "water"]. The undo removes the last item, the "water" just added, not the first one.

 

Constraints:

  • items has between 0 and 10 words: it may be empty.
  • first, second and every word in items are single words of lowercase English letters.
  • The same word may appear more than once.
STEP 1 OF 4
    Ctrl + Enter to check

    KOTLIN · ARRAYS - CHALLENGES

    Undo The Last Add in Kotlin, explained

    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

    1. Add first. add appends to the end, so on an empty list first becomes the only item.
    2. Add second. It now sits after first, as the last item.
    3. Remove the last item by index. The last index of a list is size - 1, and removeAt deletes the item at a given index.
    4. Return the list. The list was changed in place, so returning items returns 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:

    • removeAt and remove are 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 earlier water in 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.
    • size is a property. In Kotlin you write items.size, not items.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 removeAt runs. 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 without return items doesn'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].
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free