Backward-Fill Nulls

EasyData EngineeringArray

Description

Given an array that may contain null entries, fill each null with the next non-null value that appears after it. If a null has no non-null value after it, leave it null.

Examples

Input:[1,null,null,4,null,6]
Output:[1,4,4,4,6,6]
Explanation:

Each missing entry takes the next available value to its right; a trailing gap with nothing after it stays empty.

Input:[null,2,3]
Output:[2,2,3]
Explanation:

Each missing entry takes the next available value to its right; a trailing gap with nothing after it stays empty.

Input:[1,2,3]
Output:[1,2,3]
Explanation:

Each missing entry takes the next available value to its right; a trailing gap with nothing after it stays empty.

Constraints

  • 1 ≤ array length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.