2619. Array Prototype Last - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Problem Statement

You need to add a method last() to JavaScript’s Array prototype so that calling someArray.last() returns the last element of the array, or -1 if the array is empty. You may assume any array you get is the result of JSON.parse, so it behaves like a normal array.

Examples

Example 1

Input: nums = [null, {}, 3] Output: 3 Explanation: nums.last() returns the last entry, 3.

Example 2

Input: nums = [] Output: -1 Explanation: nums.last() returns -1 because the array is empty.

Constraints

  • You must attach last to Array.prototype.
  • Calling last() must run in O(1) time.
  • If this.length === 0, return -1; otherwise return the last element.

Hints

  1. How do you extend all arrays at once in JavaScript?
  2. What does this refer to inside a function on Array.prototype?
  3. How can you grab the last element in constant time?

Approach 1 – Bracket Indexing on Prototype

Attach a regular function to Array.prototype and use this[this.length – 1].

Array.prototype.last = function() { // 'this' is the array on which .last() was called return this.length === 0 ? -1 : this[this.length - 1]; };

Code (Python equivalent demonstration)

Python3
Python3

. . . .

Code (Java demonstration utility)

Java
Java

. . . .

Approach 2 – Using ES2022 at()

Modern JavaScript offers Array.prototype.at(index), where a negative index counts from the end. We can write:

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

This is equally O(1) and perhaps more expressive.

Complexity Analysis

  • Time: O(1) per call, just a couple of property accesses.
  • Space: O(1) extra.

Step‑by‑Step Walkthrough

  1. Call [1,2,3].last() → inside the function, this points to [1,2,3].
  2. Check this.length===0? No (3).
  3. Return this[2] (or this.at(-1)), which is 3.

Calling [].last()this.length===0 is true, so return -1.

Common Mistakes

  • Arrow functions: Array.prototype.last = () => … will bind this incorrectly.
  • Forgetting -1 check: omitting the empty‑array guard leads to undefined.
  • Overwriting built‑ins: avoid reassigning Array.prototype wholesale.

Edge Cases

  • Arrays with holes (e.g. [1,,3]) – bracket indexing returns undefined at a hole, but the problem guarantees normal arrays from JSON.parse.
  • Non‑primitive entries (null, {}) still work since we return whatever sits at that index.
  • Very large arrays – still O(1).

Alternative Variations

  • first() – return the first element or -1.
  • last(n) – return an array of the last n elements.
  • safe chaining: Array.prototype.lastSafe = function() { … } to avoid collisions with future native methods.
  • Counter – augment built‑ins to maintain state.
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;