LeetCode Problem Workspace

Array Prototype Last

Implement a method on all arrays that returns the last element or -1 for empty arrays, using Array.prototype correctly.

category

0

Topics

code_blocks

1

Code langs

hub

0

Related

Practice Focus

Easy · Array Prototype Last core interview pattern

bolt

Answer-first summary

Implement a method on all arrays that returns the last element or -1 for empty arrays, using Array.prototype correctly.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array Prototype Last core interview pattern

Try AiBox Copilotarrow_forward

To solve Array Prototype Last, define Array.prototype.last to return the last array element or -1 if the array is empty. Use the "this" keyword to access the array contents directly. This ensures every array, including those parsed from JSON, can call last() without errors or special checks.

Problem Statement

Enhance all JavaScript arrays with a last() method that returns the final element. If the array has no elements, last() should return -1 instead of undefined.

You may assume input arrays come from JSON.parse and contain any valid JSON value. For example, calling last() on [null, {}, 3] should return 3, while an empty array returns -1.

Examples

Example 1

Input: nums = [null, {}, 3]

Output: 3

Calling nums.last() should return the last element: 3.

Example 2

Input: nums = []

Output: -1

Because there are no elements, return -1.

Constraints

  • arr is a valid JSON array
  • 0 <= arr.length <= 1000

Solution Approach

Direct Prototype Extension

Assign a function to Array.prototype.last that returns this[this.length-1] if length > 0, else -1. This leverages the built-in array length property for O(1) access.

Validation and Edge Handling

Inside last(), check if this.length is zero before accessing elements to avoid undefined results. This handles empty arrays safely and matches the problem's failure mode.

JSON Compatibility

Since arrays may originate from JSON.parse, ensure last() does not modify elements and works for all valid JSON values including null, objects, and numbers.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(1) because accessing the last index is constant. Space complexity is O(1) as no additional storage is required beyond the function itself.

What Interviewers Usually Probe

  • Do you handle empty arrays explicitly to avoid undefined?
  • Are you modifying Array.prototype directly or using a helper function?
  • Can your last() method handle arrays with mixed types, including objects and nulls?

Common Pitfalls or Variants

Common pitfalls

  • Accessing this[this.length] instead of this[this.length-1] causing undefined.
  • Not checking for empty arrays and returning undefined instead of -1.
  • Mutating the array inside last(), which breaks immutability expectations.

Follow-up variants

  • Implement last(n) to return the last n elements instead of just one.
  • Create a first() method alongside last() to retrieve the first element safely.
  • Support chaining by returning a wrapper object with last() and other utility methods.

FAQ

What does Array Prototype Last require?

It requires defining Array.prototype.last to return the last element or -1 if the array is empty.

How do I handle empty arrays for last()?

Check if this.length === 0 and return -1; otherwise, return this[this.length-1].

Can last() handle arrays from JSON.parse?

Yes, it works with any valid JSON array, including those with null, objects, or numbers.

Is modifying Array.prototype safe?

For this problem, modifying Array.prototype is expected, but avoid overwriting other built-in methods.

Can last() be extended to return multiple elements?

Yes, you could create last(n) to return the last n elements, but this requires additional slicing logic.

terminal

Solution

Solution 1

#### TypeScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
declare global {
    interface Array<T> {
        last(): T | -1;
    }
}

Array.prototype.last = function () {
    return this.length ? this.at(-1) : -1;
};

/**
 * const arr = [1, 2, 3];
 * arr.last(); // 3
 */

export {};
Array Prototype Last Solution: Array Prototype Last core interview p… | LeetCode #2619 Easy