Skip to the solution
EasyDSA6 min read

Implement Your Own Array.prototype.reduceRight()

379 views

The problem

Problem, as an interviewer would state it: "Implement Array.prototype.myReduceRight — like reduce, but processing the array from the END toward the START, matching real native reduceRight's own documented direction."

Examples:

Code
["a","b","c"].myReduceRight((acc, v) => acc + v, ""); // "cba" -- right to left

Clarifying questions expected:

  • Is the real, defining direction difference from reduce() the whole point being tested here?
  • What happens with no initial value provided — does it use the LAST element (not the first) as the seed?
  • Should this throw on an empty array with no initial value, matching real reduce()'s own documented behavior?

Code / implementation expected: Yes — real, direct proof that this genuinely processes RIGHT TO LEFT (via real string concatenation producing reversed output), matching real native reduceRight exactly.

  • polyfill
  • array

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read · 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, direct proof: reduceRight genuinely processes right to left, confirmed via real string concatenation producing reversed output matching native exactly

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 39 of 154 in JavaScript coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

Build a Toast/Notification Queue That Shows One at a Time With Auto-Dismiss

Continue