Array Prototype ForEach — LeetCode 2804 Python Solution

EasyLeetCode PremiumJavaScript
Problem
#2804
Reading time
2 min

The problem

Write your version of method forEach that enhances all arrays such that you can call the array.forEach(callback, context) method on any array and it will execute callback on each element of the array. Method forEach should not return anything.

Example

Input
arr = [1,2,3],
Output
[2,4,6]
Explanation
arr.forEach(callback, context)

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2804. Array Prototype ForEach?
LeetCode 2804. Array Prototype ForEach is rated Easy on LeetCode.
What topics does LeetCode 2804. Array Prototype ForEach cover?
LeetCode 2804. Array Prototype ForEach is tagged JavaScript on LeetCode.
Is LeetCode 2804. Array Prototype ForEach a premium problem?
Yes. LeetCode 2804. Array Prototype ForEach is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview