Array Prototype Last - Solution & Explanation
Problem Statement
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:
arris a valid JSON array0 <= arr.length <= 1000
Approach Overview
Problem Overview: You need to return the last element of an array. If the array is empty, return -1. In JavaScript, the task specifically asks you to extend Array.prototype with a last() method that behaves this way.
Approach 1: Prototype Method Enhancement (O(1) time, O(1) space)
This approach directly extends Array.prototype by adding a last() method. Inside the method, check the array length. If this.length === 0, return -1. Otherwise, access the last element using index this.length - 1. The key insight is that JavaScript arrays allow constant-time index access, so retrieving the final element requires no iteration. This solution follows the exact requirement of modifying the prototype and demonstrates understanding of JavaScript object behavior.
Approach 2: Utility Function with Direct Index Access (O(1) time, O(1) space)
In languages like C, C++, Java, Python, or C#, prototype modification is not relevant. Instead, implement a small helper function that accepts an array and returns its last element. Check if the array size is zero; if so, return -1. Otherwise, return the value at index n - 1. This relies on constant-time random access provided by arrays. The algorithm performs only a length check and a single index operation, keeping both time and space complexity constant.
Both solutions rely on the same core observation: the last element of an array is always accessible using the index length - 1. No traversal or additional data structures are required.
Recommended for interviews: The direct index access approach is what interviewers expect. It shows you understand array indexing and boundary conditions. When solving the JavaScript-specific version, implementing the Array.prototype.last method demonstrates familiarity with prototypes and built-in object extension. Both solutions run in O(1) time and O(1) space, which is optimal for this problem.
Approach 1: Prototype Method Enhancement
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.
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].
Code
JavaScript
Complexity
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.
Approach 2: Utility Function Approach
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.
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.
Complexity
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.
Approach 3: Default Approach
Code
TypeScript
Complexity Comparison
| Approach | Complexity |
|---|---|
| Prototype Method Enhancement | 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. |
| Utility Function Approach | 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. |
| Default Approach | — |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Prototype Method Enhancement | O(1) | O(1) | JavaScript-specific problems requiring extension of Array.prototype |
| Utility Function with Direct Index Access | O(1) | O(1) | General solution for languages like Python, Java, C++, or C# |
Video Solution
Array Prototype Last - Leetcode 2619 - JavaScript 30-Day Challenge • NeetCodeIO • 5,462 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Array Prototype Last easy or hard?
Array Prototype Last Python/Java solution
How to solve Array Prototype Last in O(1)?
What is the best approach for Array Prototype Last?
Is Array Prototype Last asked at Google/Amazon/Meta?
What data structure is used in Array Prototype Last?
What is the time complexity of Array Prototype Last?
Ready to solve this problem?
Practice Array Prototype Last with our built-in code editor and test cases.
Practice on FleetCode