Read the Final Value of an Array

Quick Overview

Add a JavaScript array method that returns the final value unchanged, preserving null and falsy elements and handling empty arrays.

Read the Final Value of an Array

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Extend JavaScript arrays with a method named `last`, so that calling `items.last()` returns the final element of `items`. If the array is empty, return the number `-1`. Implement `Array.prototype.last`. It takes no arguments; the array receiving the method call supplies the elements. Return the final value unchanged, including when it is `null`, `false`, `0` or an empty string. Reading the final element must not remove or replace array elements. ### Examples **Example 1:** ```javascript const items = JSON.parse('[8, "done", false]'); items.last(); // false ``` The final value is `false`; the array is not empty. **Example 2:** ```javascript const items = JSON.parse('[{"ready": true}, null]'); items.last(); // null ``` The final value is `null`, so that exact value is returned. An empty array would instead return `-1`. ### Constraints - Each receiver is an array produced by `JSON.parse` from valid JSON array text. - `0 <= items.length <= 1000`. - Elements may be JSON nulls, booleans, numbers, strings, arrays or objects. Nested JSON values are allowed. - The task does not require handling sparse arrays, `undefined` elements or non-array receivers. ### What a Strong Answer Covers - Make `last()` available through the required array method contract. - Distinguish an empty array from a nonempty array with a falsy final value. - Preserve array contents and return object or array elements themselves, without making copies. ### Follow-up Questions - If the final value is an object, what relationship should hold between the returned object and that array element? - If the final value is the number `-1`, can the return value alone distinguish that array from an empty one?

Overview: Add a JavaScript array method that returns the final value unchanged, preserving null and falsy elements and handling empty arrays.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Extend JavaScript arrays with a method named last, so that calling items.last() returns the final element of items. If the array is empty, return the number -1.

Implement Array.prototype.last. It takes no arguments; the array receiving the method call supplies the elements. Return the final value unchanged, including when it is null, false, 0 or an empty string. Reading the final element must not remove or replace array elements.

Examples

Example 1:

const items = JSON.parse('[8, "done", false]');
items.last(); // false

The final value is false; the array is not empty.

Example 2:

const items = JSON.parse('[{"ready": true}, null]');
items.last(); // null

The final value is null, so that exact value is returned. An empty array would instead return -1.

Constraints

  • Each receiver is an array produced by JSON.parse from valid JSON array text.
  • 0 <= items.length <= 1000 .
  • Elements may be JSON nulls, booleans, numbers, strings, arrays or objects. Nested JSON values are allowed.
  • The task does not require handling sparse arrays, undefined elements or non-array receivers.

What a Strong Answer Covers Guidance

  • Make last() available through the required array method contract.
  • Distinguish an empty array from a nonempty array with a falsy final value.
  • Preserve array contents and return object or array elements themselves, without making copies.

Follow-up Questions Guidance

  • If the final value is an object, what relationship should hold between the returned object and that array element?
  • If the final value is the number -1 , can the return value alone distinguish that array from an empty one?