Write code that enhances all arrays such that you can call the array.last() method on any array and it will return the last element. If there are no elements in the array, it should return -1.
You may assume the array is the output of JSON.parse.
Example 1:
Input: nums = [null, {}, 3]
Output: 3
Explanation: Calling nums.last() should return the last element: 3.
Example 2:
Input: nums = [] Output: -1 Explanation: Because there are no elements, return -1.
Constraints:
arr is a valid JSON array0 <= arr.length <= 1000In #2619 Array Prototype Last, the goal is to extend the JavaScript Array prototype by adding a method that returns the last element of the array. If the array is empty, the method should return -1. The key idea is to leverage the built-in length property of arrays.
Start by attaching a new function to Array.prototype. Inside this function, check whether the array contains any elements by inspecting this.length. If the length is zero, return -1. Otherwise, access the final element using index this.length - 1. Since arrays allow constant-time indexing, retrieving the last element is straightforward.
This approach avoids iteration and works efficiently for arrays of any size. Because it simply checks the length and returns a value by index, both the time and space requirements remain constant.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
Direct index using length - 1 | O(1) | O(1) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Inside the Array.prototype.last function body, you have access to the "this" keyword. "this" is equal to the contents of the array in this case.
You can access elements in the array via this[0], this[1], etc. You can also access properties and method like this.length, this.forEach, etc.
This approach involves extending the Array prototype such that a method last() can be directly called on any array instance. If the array is empty, it should return -1; otherwise, it should return the last element of the array.
Time complexity is O(1) as accessing an array element by index is constant time. Space complexity is O(1) as no extra space is used.
1Array.prototype.last = function() {
2 return this.length === 0 ? -1 : this[this.length - 1];
3};In JavaScript, we can directly augment the Array.prototype to add a new method last(). This method checks if the array is empty and returns -1 if true, otherwise it returns the last element using this[this.length - 1].
This approach involves creating a separate utility function that operates on arrays to return the last element or -1 if the array is empty. This is useful where extending native objects is not recommended or possible.
Time complexity is O(1) since accessing the last element by index is constant time. Space complexity is O(1) as no additional space is needed.
1#include <stdbool.h>
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
While the exact problem may not always appear, similar JavaScript prototype and array manipulation questions are common in front-end and full-stack interviews. They test understanding of core language features and basic algorithmic thinking.
The optimal approach is to access the last element using the array's length property. If the array is empty, return -1; otherwise return the element at index length minus one. This works in constant time because JavaScript arrays support direct indexing.
Returning -1 provides a clear indicator that the array has no elements. Since there is no valid last element in an empty array, the function uses this value as a simple sentinel result.
The problem relies on the properties of JavaScript arrays and prototype extension. By attaching a method to Array.prototype, all array instances gain access to the same functionality without modifying each array individually.
In C, we write a utility function lastElement that takes an array and its length as arguments. It returns -1 if the length is zero, otherwise, it returns the last element using arr[length - 1]. This approach avoids trying to augment the C array prototype, which is not possible in C.