Replace each array element with the product of the next two elements

Quick Overview

Build an array where each position holds the product of the two elements after it in the input, with zeros where fewer than two elements follow. It tests careful index bounds, handling of arrays shorter than three elements, and clean array construction with negative values.

Replace each array element with the product of the next two elements

Company: Supercell

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an integer array `nums`, return a new array `result` of the same length where each element at index `i` is replaced by the product of the elements at indices `i+1` and `i+2` in the original array. If either `i+1` or `i+2` is out of bounds, set `result[i]` to `0`. ### Function Signature ```python def product_of_next_two(nums: list[int]) -> list[int]: ``` ### Rules - Every product uses values from the original `nums`, never values already written to `result`. - When `nums` has at least two elements, the last two entries of `result` are always `0`. When it has one element, `result` is `[0]`. ### Constraints - `1 <= nums.length <= 10^4` - `-10^3 <= nums[i] <= 10^3` ### Examples ```text Input: nums = [1,2,3,4,5] Output: [6,12,20,0,0] ``` `result[0] = 2 * 3 = 6`, `result[1] = 3 * 4 = 12` and `result[2] = 4 * 5 = 20`. Indices 3 and 4 have fewer than two elements after them, so they are 0. ```text Input: nums = [2,3,4] Output: [12,0,0] ``` Only index 0 has two elements after it, and `3 * 4 = 12`. ```text Input: nums = [4,-1,-6,2] Output: [6,-12,0,0] ``` `result[0] = (-1) * (-6) = 6` and `result[1] = (-6) * 2 = -12`. The last two entries are 0.

Overview: Build an array where each position holds the product of the two elements after it in the input, with zeros where fewer than two elements follow. It tests careful index bounds, handling of arrays shorter than three elements, and clean array construction with negative values.

Supercell logo
Supercell
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array nums, return a new array result of the same length where each element at index i is replaced by the product of the elements at indices i+1 and i+2 in the original array.

If either i+1 or i+2 is out of bounds, set result[i] to 0.

Function Signature

def product_of_next_two(nums: list[int]) -> list[int]:

Rules

  • Every product uses values from the original nums , never values already written to result .
  • When nums has at least two elements, the last two entries of result are always 0 . When it has one element, result is [0] .

Constraints

  • 1 <= nums.length <= 10^4
  • -10^3 <= nums[i] <= 10^3

Examples

Input: nums = [1,2,3,4,5]
Output: [6,12,20,0,0]

result[0] = 2 * 3 = 6, result[1] = 3 * 4 = 12 and result[2] = 4 * 5 = 20. Indices 3 and 4 have fewer than two elements after them, so they are 0.

Input: nums = [2,3,4]
Output: [12,0,0]

Only index 0 has two elements after it, and 3 * 4 = 12.

Input: nums = [4,-1,-6,2]
Output: [6,-12,0,0]

result[0] = (-1) * (-6) = 6 and result[1] = (-6) * 2 = -12. The last two entries are 0.