Practice Array Prototype Last
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Extend JavaScript arrays with a last() method. Calling it should return the final stored element without changing the array. If the array is empty, return -1. Inputs are arrays produced by JSON.parse, so null is a valid element and must be returned as null. Practice approach: Check this.length explicitly. Return this[this.length - 1] for a nonempty array; otherwise return -1. Do not use a truthiness fallback, since zero, false, and null can be legitimate last values. A normal function is needed when assigning the prototype method so this refers to the calling array. Time and extra space are O(1).
Source: Array Prototype Last on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [null, {}, 3]3
# Example 2
nums = []
-1
Constraints
How to solve Array Prototype Last
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
The whole exercise is distinguishing an empty array from an array whose last element is falsy. Only the length can make that decision, and the method must be a regular function so this is bound to the array it was called on.
Why brute force is not enough
Nothing here is asymptotically slow; the trap is returning this[this.length - 1] with a falsy fallback to -1, which replaces a legitimate trailing null, 0, false or empty string with -1. Scanning the array to find its end is also needless, since indexing is constant time.
Approach
- Attach the method to Array.prototype with a traditional function expression rather than an arrow function so this refers to the receiving array.
- Test whether this.length is zero; the length is the sole source of truth for emptiness.
- If empty, return -1 as specified; otherwise return the element at index this.length - 1 exactly as stored, including null or objects.
- Never pop or splice, since callers expect the array unchanged afterward.
Walkthrough of Example 1
nums = [null, {}, 3] → 3
- The array is [null, {}, 3], so this.length is 3.
- The length is nonzero, so the last index is 3 - 1 = 2.
- Reading index 2 gives 3.
- Return 3; the array is unchanged.
Time and space
Time O(1) · Space O(1). Reading length and indexing one position are constant-time on a JavaScript array of any size n, and nothing is allocated.
Interview pitfalls
- Using an arrow function, which captures this lexically so it is not the array.
- Relying on a truthiness fallback such as this.at(-1) or this[this.length - 1] combined with an or-operator; a trailing null, 0 or false then wrongly yields -1.
- Calling this.at(-1) alone, which returns undefined for an empty array instead of -1.
- Using pop, which mutates the array and breaks later calls.
- Adding the method as an enumerable property so it appears in for-in loops; Object.defineProperty with enumerable false is the safer pattern.
Follow-up questions interviewers ask
- How would you declare this in TypeScript, including the interface merging needed to extend Array?
- Why is extending built-in prototypes risky in shared code, and what alternative would you propose?
- How should the method treat a sparse array whose final slot is a hole versus a stored undefined?
Further reading on the technique: Prototype-based programming
Related LeetCode Problems
Practice more problems similar to Array Prototype Last.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.