Read here. Code on desktop. The coding workspace opens on screens 1024px and wider.
JavaScript interview question
Fill
easy15 min
Write a range-fill utility that updates an array in place and returns that same array.
Requirements
Implement fill(array, value, start = 0, end = array.length). Replace positions from start inclusive to end exclusive.
Mutate the input array and return that exact array. Do not change its length or write outside the selected range.
Negative indices count from the end. Clamp indices to the valid range; truncate fractions toward zero. Treat NaN as zero and clamp infinities to the corresponding boundary.
When the normalized end is at or before start, leave the array unchanged. Omitted or undefined start and end use their defaults.
Fill sparse slots within the selected range. If value is an object, every filled slot holds that same reference.
Inputs are writable arrays and numeric indices. Implement the range loop yourself; do not call Array.prototype.fill.
Examples
Example 1
Input: const array = [1, 2, 3, 4]; fill(array, 0, 1, 3);
Output: [1, 0, 0, 4]; the returned value is array.
Write a range-fill utility that updates an array in place and returns that same array.
What the tests check
Fills the whole array by default and returns the original
Treats the end boundary as exclusive
Defaults the end boundary to array length
Supports negative boundaries
Clamps out-of-range boundaries without extending the array
Leaves reversed and zero-width ranges unchanged
Truncates fractional indices toward zero
Handles NaN and infinite boundaries
Undefined boundaries use defaults
Fills sparse slots only within the chosen range
Stores one object reference in every filled position