Implement map, filter and reduce from scratch on an integer array

Quick Overview

Write your own map, filter and reduce for an integer array: double every element, keep the values strictly above a threshold, and sum the array from an initial value of zero. It tests loop and accumulator patterns and running three independent passes over the same input.

Implement map, filter and reduce from scratch on an integer array

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Implement custom versions of three basic array methods from scratch: map, filter and reduce. You must not use the built-in equivalents; implement the logic manually using loops and accumulators. Given an array of integers `nums` and an integer `threshold`, perform the following three operations: - `custom_map`: apply a transformation that doubles each element in `nums`. Return the resulting array. - `custom_filter`: keep only the elements in `nums` that are **strictly greater than** `threshold`. Return the resulting array. - `custom_reduce`: compute the sum of all elements in `nums` using an accumulator pattern with an initial value of `0`. Return the single integer result. Return a list containing three items: `[mapped_result, filtered_result, reduced_result]`. ### Function Signature ```python def custom_array_methods(nums: list[int], threshold: int) -> list: ``` ### Rules - Each of the three operations reads the original `nums`. The filter and the sum do not use the doubled values. - `filtered_result` keeps the elements in their original order and can be empty. - `reduced_result` is a plain integer, not a list. ### Constraints - `1 <= len(nums) <= 10^4` - `-10^5 <= nums[i] <= 10^5` - `-10^5 <= threshold <= 10^5` ### Examples ```text Input: nums = [1,2,3,4,5], threshold = 3 Output: [[2,4,6,8,10],[4,5],15] ``` Doubling gives `[2,4,6,8,10]`. Only 4 and 5 are greater than 3 (3 itself is excluded because the comparison is strict), and `1+2+3+4+5 = 15`. ```text Input: nums = [-2,0,5,-1,10], threshold = 0 Output: [[-4,0,10,-2,20],[5,10],12] ``` Doubling gives `[-4,0,10,-2,20]`. Only 5 and 10 are greater than 0, and `-2+0+5-1+10 = 12`. ```text Input: nums = [3,3], threshold = 3 Output: [[6,6],[],6] ``` No element is strictly greater than 3, so the filtered list is empty. The sum is `3+3 = 6`.

Overview: Write your own map, filter and reduce for an integer array: double every element, keep the values strictly above a threshold, and sum the array from an initial value of zero. It tests loop and accumulator patterns and running three independent passes over the same input.

ServiceNow logo
ServiceNow
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Implement custom versions of three basic array methods from scratch: map, filter and reduce. You must not use the built-in equivalents; implement the logic manually using loops and accumulators.

Given an array of integers nums and an integer threshold, perform the following three operations:

  • custom_map : apply a transformation that doubles each element in nums . Return the resulting array.
  • custom_filter : keep only the elements in nums that are strictly greater than threshold . Return the resulting array.
  • custom_reduce : compute the sum of all elements in nums using an accumulator pattern with an initial value of 0 . Return the single integer result.

Return a list containing three items: [mapped_result, filtered_result, reduced_result].

Function Signature

def custom_array_methods(nums: list[int], threshold: int) -> list:

Rules

  • Each of the three operations reads the original nums . The filter and the sum do not use the doubled values.
  • filtered_result keeps the elements in their original order and can be empty.
  • reduced_result is a plain integer, not a list.

Constraints

  • 1 <= len(nums) <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • -10^5 <= threshold <= 10^5

Examples

Input: nums = [1,2,3,4,5], threshold = 3
Output: [[2,4,6,8,10],[4,5],15]

Doubling gives [2,4,6,8,10]. Only 4 and 5 are greater than 3 (3 itself is excluded because the comparison is strict), and 1+2+3+4+5 = 15.

Input: nums = [-2,0,5,-1,10], threshold = 0
Output: [[-4,0,10,-2,20],[5,10],12]

Doubling gives [-4,0,10,-2,20]. Only 5 and 10 are greater than 0, and -2+0+5-1+10 = 12.

Input: nums = [3,3], threshold = 3
Output: [[6,6],[],6]

No element is strictly greater than 3, so the filtered list is empty. The sum is 3+3 = 6.