Implement a map function that applies a named callback to every element of an array

Quick Overview

Write a map function from scratch that applies a named transformation such as square, double, increment or negate to every element of an integer array and returns a new array. Tests basic iteration, dispatching from a string to a function, and returning a fresh list without changing the input.

Implement a map function that applies a named callback to every element of an array

Company: Unity

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Implement a map function from scratch that applies a callback to every element in an array and returns a new array with the transformed values. You are given an array of integers `arr` and a string `callback` that identifies which transformation to apply. The supported callbacks are: - `"square"`: returns the element multiplied by itself - `"double"`: returns the element multiplied by 2 - `"increment"`: returns the element plus 1 - `"negate"`: returns the negation of the element Return a new array where each element is the result of applying the callback to the corresponding element in the input array. Do not use the built-in `map()` function. ### Function Signature ```python def custom_map(arr: list[int], callback: str) -> list[int]: ``` ### Rules - The output has the same length as `arr`, and position `i` of the output is the callback applied to `arr[i]`. - Callback names are matched exactly, in lowercase as listed above. - Return a new list and leave `arr` unchanged. ### Constraints - `1 <= len(arr) <= 10^4` - `-10^4 <= arr[i] <= 10^4` - `callback` is one of `"square"`, `"double"`, `"increment"`, `"negate"` ### Examples Example 1 ```text Input: arr = [1, 2, 3, 4, 5], callback = "square" Output: [1, 4, 9, 16, 25] ``` Each element is multiplied by itself: 1 to 1, 2 to 4, 3 to 9, 4 to 16, 5 to 25. Example 2 ```text Input: arr = [10, -3, 0, 7], callback = "double" Output: [20, -6, 0, 14] ``` Each element is multiplied by 2: 10 to 20, -3 to -6, 0 to 0, 7 to 14. Example 3 ```text Input: arr = [3, -2, 0], callback = "negate" Output: [-3, 2, 0] ``` Negation flips the sign of each element, and 0 stays 0.

Overview: Write a map function from scratch that applies a named transformation such as square, double, increment or negate to every element of an integer array and returns a new array. Tests basic iteration, dispatching from a string to a function, and returning a fresh list without changing the input.

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

Implement a map function from scratch that applies a callback to every element in an array and returns a new array with the transformed values.

You are given an array of integers arr and a string callback that identifies which transformation to apply. The supported callbacks are:

  • "square" : returns the element multiplied by itself
  • "double" : returns the element multiplied by 2
  • "increment" : returns the element plus 1
  • "negate" : returns the negation of the element

Return a new array where each element is the result of applying the callback to the corresponding element in the input array. Do not use the built-in map() function.

Function Signature

def custom_map(arr: list[int], callback: str) -> list[int]:

Rules

  • The output has the same length as arr , and position i of the output is the callback applied to arr[i] .
  • Callback names are matched exactly, in lowercase as listed above.
  • Return a new list and leave arr unchanged.

Constraints

  • 1 <= len(arr) <= 10^4
  • -10^4 <= arr[i] <= 10^4
  • callback is one of "square" , "double" , "increment" , "negate"

Examples

Example 1

Input: arr = [1, 2, 3, 4, 5], callback = "square"
Output: [1, 4, 9, 16, 25]

Each element is multiplied by itself: 1 to 1, 2 to 4, 3 to 9, 4 to 16, 5 to 25.

Example 2

Input: arr = [10, -3, 0, 7], callback = "double"
Output: [20, -6, 0, 14]

Each element is multiplied by 2: 10 to 20, -3 to -6, 0 to 0, 7 to 14.

Example 3

Input: arr = [3, -2, 0], callback = "negate"
Output: [-3, 2, 0]

Negation flips the sign of each element, and 0 stays 0.