Implement Array.fill

Easy
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Implement Array.fill

Approach

Iterate over the specified range within the array, replacing each element with the given value. Normalize negative start/end indices by adding the array length, and clamp bounds to prevent out-of-range writes. The operation modifies the array in place and returns it.

Complexity Analysis

Time
O(n)
Space
O(1)

Pattern

Array Polyfill

Why It Works

Negative index normalization and bounds clamping ensure the fill range is valid, while in-place mutation avoids allocating a new array.

Updated Feb 2026