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 array0 <= arr.length <= 1000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the last element is simple: check every element in the sequence one at a time. Stop only when you reach the very end, and then you have found the last element. It is similar to manually looking at each item until you find the very last one.
Here's how the algorithm would work step-by-step:
def find_last_element_brute_force(sequence_of_items): last_element = None
# Start at the beginning of the sequence and go element by element
for current_index in range(len(sequence_of_items)):
# This loop finds the last element by iterating to the end
last_element = sequence_of_items[current_index]
# Return the last element found at the end of the sequence
return last_elementWe want to add a new capability that gets the last item of a collection directly from the collection itself. Instead of picking values one by one, we need to access this last item directly through an extension.
Here's how the algorithm would work step-by-step:
def array_prototype_last(input_array):
# Handle empty arrays, prevents index errors.
if not input_array:
return None
# Access the last element, ensuring correctness.
last_element_index = len(input_array) - 1
# Return the element at the calculated last index.
return input_array[last_element_index]| Case | How to Handle |
|---|---|
| Empty array | Return -1 immediately when the array's length is zero. |
| Array with one element | Return the single element in the array. |
| Array with a large number of elements (performance) | The solution has O(1) time complexity and O(1) space complexity, making it efficient regardless of the array size. |
| Array containing only null or undefined values | The last element of the array, even if null or undefined, should be returned as is. |
| Array containing mixed data types (numbers, strings, objects) | The solution should return the last element regardless of its data type without causing errors. |
| Array with extreme numeric values (very large or very small numbers) | The solution is designed to work with any valid number type, without being affected by numerical limits (within Javascript limitations). |
| Modifying the array prototype should not affect other parts of the program negatively. | Adding a method to Array.prototype can affect enumeration so consider potential problems when iterating. |
| What if 'last' property already exists on the Array prototype? | The solution needs to check if 'last' exists before overriding it to prevent unintended side effects; if it exists, either return an error or rename the method. |