Problem overview
The method undoLastAdd receives an array of strings, 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 last item. It returns the list.
With items = ["milk", "eggs"], first = "bread" and second = "jam", the list becomes ["milk", "eggs", "bread", "jam"] after the two adds. The undo removes "jam", so the result is ["milk", "eggs", "bread"]. With an empty list, "apples" and "pears", the result is ["apples"].
Approach
An array in TypeScript has a method for each end of the list. push adds a value after the last element, and pop removes the last element and returns it. Together they behave like a stack: the last thing added is the first thing taken back. The task is exactly that, so it is three method calls in the right order.
The undo removes the item at the end, not the item with a certain value. That matters when the same word appears twice. With items = ["water"], first = "juice" and second = "water", the list is ["water", "juice", "water"] before the undo. pop removes the last "water", the one just added, and the result is ["water", "juice"].
Step by step
- Push
first. The word goes after every item already in the list. On an empty list it becomes the only item. - Push
second. Nowsecondis the last item, because it was added last. - Pop. This removes the last item, which is
second, wherever else the same word appears. - Return
items. The list now ends withfirst.
The solution in TypeScript
class Solution {
undoLastAdd(items: string[], first: string, second: string): string[] {
items.push(first);
items.push(second);
items.pop();
return items;
}
}
A few details are worth knowing:
- The array is changed in place.
pushandpopmodify the array they are called on, and the method returns that same array. A caller who passed["a"]and kept a reference sees the array become["a", "b"]afterundoLastAdd(items, "b", "c"), and the returned value is the very same object. popreturns the removed item. Here the return value is ignored, which is fine. On an empty arraypopreturnsundefinedand changes nothing, but this lesson never pops from an empty array, becausepushhas run twice first.- Types are only annotations.
string[]tells the compiler what the array should hold. When TypeScript compiles to JavaScript the annotations are erased, so they check nothing while the program runs.
Complexity
Time: O(1). Adding to or removing from the end of an array takes constant time on average, and the method does three such operations. The cost does not depend on the length of the list.
Space: O(1) extra. The method creates no new array. It works on the one it was given.
Edge cases and common mistakes
- Empty list.
[]with"apples"and"pears"returns["apples"]. - Repeated words. Removing the word
secondby searching for its value would delete the first match, not the last item. Usepop, which always works on the end. - Same word twice in the adds. With
first = "tea"andsecond = "tea", the list gets two"tea"items and the undo removes one, so one"tea"remains. - Adding the words in the wrong order. If
secondis pushed first, the undo removesfirstinstead. - Forgetting the return. The array is changed in place, but the method's result is still what the tests read, so it must return
items.
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"].
