Skip to main content

Array Prototype Last - Solution & Explanation

Easy6 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

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:

  • arr is a valid JSON array
  • 0 <= 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.

Try this approach in the editor →

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.

Code

C

C++

Java

Python

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.

Try this approach in the editor →

Approach 3: Default Approach

Code

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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

ApproachTimeSpaceWhen to Use
Prototype Method EnhancementO(1)O(1)JavaScript-specific problems requiring extension of Array.prototype
Utility Function with Direct Index AccessO(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 is classified as an Easy problem. The main concept is handling the empty array edge case and retrieving the final element using constant-time array indexing.
Array Prototype Last Python/Java solution
In Python or Java, implement a helper function that returns -1 if the array or list is empty; otherwise return arr[len(arr) - 1] in Python or arr[n - 1] in Java. Both implementations run in O(1) time and O(1) space.
How to solve Array Prototype Last in O(1)?
Use direct index access. If the array is empty, return -1. Otherwise return the element at index n - 1 where n is the array length. This avoids loops and keeps the solution constant time.
What is the best approach for Array Prototype Last?
The best approach is direct index access. Check if the array length is zero and return -1; otherwise return the element at index length - 1. This works in O(1) time and O(1) space since array indexing is constant time.
Is Array Prototype Last asked at Google/Amazon/Meta?
Array Prototype Last is a basic array manipulation problem commonly used in coding platforms to test understanding of language features and edge cases. While simpler than typical FAANG interview questions, similar array boundary problems appear in early screening rounds.
What data structure is used in Array Prototype Last?
The problem uses a standard array data structure. The solution relies on constant-time random access provided by arrays to retrieve the last element using index length - 1.
What is the time complexity of Array Prototype Last?
The time complexity is O(1). The algorithm performs only a length check and a single array index lookup, both constant-time operations. No iteration or additional processing is required.

Ready to solve this problem?

Practice Array Prototype Last with our built-in code editor and test cases.

Practice on FleetCode