Simulate JavaScript map, bind and Promise.all polyfills in Python

Quick Overview

Simulate three JavaScript polyfills in Python: Array.prototype.map with named callbacks, Function.prototype.bind with pre-bound arguments, and Promise.all over resolved and rejected entries. It tests reading API semantics precisely, including argument order and how a rejection changes the result.

Simulate JavaScript map, bind and Promise.all polyfills in Python

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Implement a simulator that mimics the behavior of three JavaScript polyfills: `Array.prototype.map`, `Function.prototype.bind`, and `Promise.all`. You receive an operation name and its parameters, and must return the result that the corresponding JavaScript polyfill would produce. The three supported operations are: - `map` takes an array `arr` and an operation descriptor `op` (one of `"double"`, `"square"`, `"increment"`, `"negate"`, `"identity"`) and returns a new array where each element has the operation applied. - `bind` takes a function descriptor `fn` (one of `"add"`, `"multiply"`, `"subtract"`), an array of pre-bound arguments `boundArgs`, and an array of call-time arguments `callArgs`, and returns the result of invoking the function with the concatenation of `boundArgs` followed by `callArgs`. - `promiseAll` takes an array `promises` of `[status, value]` pairs and returns the list of values if all are `"resolved"`. Otherwise it returns a single-element list containing the value of the first `"rejected"` entry. Return the result as a list of integers in all cases. ### Function Signature ```python def run_polyfill(operation: str, payload: dict) -> list[int]: ``` ### Rules - `payload` holds the keys for its operation: `"arr"` and `"op"` for `map`; `"fn"`, `"boundArgs"` and `"callArgs"` for `bind`; `"promises"` for `promiseAll`. - The map descriptors mean `"double"` -> `x * 2`, `"square"` -> `x * x`, `"increment"` -> `x + 1`, `"negate"` -> `-x`, `"identity"` -> `x`. - For `bind`, the argument list is `boundArgs` followed by `callArgs`. `"add"` returns their sum, `"multiply"` returns their product, and `"subtract"` returns the first argument minus the second. For example, `boundArgs = [10]` and `callArgs = [3]` with `"subtract"` give `[7]`. The result is returned as a one-element list. - Each `promises` entry is `[status, value]`, where `status` is `"resolved"` or `"rejected"`. `value` is an integer, which may be given as a decimal string such as `"42"` or `"-3"`; return it as an int. "First rejected entry" means first in array order. - Use exact integer arithmetic. A result may fall outside the 32-bit range (for example, the square of a large element). ### Constraints - `operation` is one of `"map"`, `"bind"`, `"promiseAll"`. - For `map`: `1 <= len(arr) <= 10^4`; each element fits in a 32-bit signed integer. - For `bind`: total argument count `>= 1`; `subtract` always receives exactly 2 total arguments. - For `promiseAll`: `1 <= len(promises) <= 10^4`. ### Examples ```text Input: operation = "map", payload = {"arr": [1, 2, 3, 4, 5], "op": "square"} Output: [1, 4, 9, 16, 25] ``` Applying the `square` callback to each element of `[1, 2, 3, 4, 5]` gives `[1, 4, 9, 16, 25]`, the same array `Array.prototype.map` would build. ```text Input: operation = "bind", payload = {"fn": "add", "boundArgs": [10, 20], "callArgs": [5]} Output: [35] ``` `bind` pre-binds `10` and `20`, so calling with `5` invokes `add(10, 20, 5)`, which is 35, returned as `[35]`. ```text Input: operation = "promiseAll", payload = {"promises": [["resolved", "3"], ["rejected", "7"], ["rejected", "9"]]} Output: [7] ``` The second entry is the first rejection in array order, so the result is its value alone. The resolved value 3 and the later rejection 9 are not part of the output.

Overview: Simulate three JavaScript polyfills in Python: Array.prototype.map with named callbacks, Function.prototype.bind with pre-bound arguments, and Promise.all over resolved and rejected entries. It tests reading API semantics precisely, including argument order and how a rejection changes the result.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Implement a simulator that mimics the behavior of three JavaScript polyfills: Array.prototype.map, Function.prototype.bind, and Promise.all. You receive an operation name and its parameters, and must return the result that the corresponding JavaScript polyfill would produce.

The three supported operations are:

  • map takes an array arr and an operation descriptor op (one of "double" , "square" , "increment" , "negate" , "identity" ) and returns a new array where each element has the operation applied.
  • bind takes a function descriptor fn (one of "add" , "multiply" , "subtract" ), an array of pre-bound arguments boundArgs , and an array of call-time arguments callArgs , and returns the result of invoking the function with the concatenation of boundArgs followed by callArgs .
  • promiseAll takes an array promises of [status, value] pairs and returns the list of values if all are "resolved" . Otherwise it returns a single-element list containing the value of the first "rejected" entry.

Return the result as a list of integers in all cases.

Function Signature

def run_polyfill(operation: str, payload: dict) -> list[int]:

Rules

  • payload holds the keys for its operation: "arr" and "op" for map ; "fn" , "boundArgs" and "callArgs" for bind ; "promises" for promiseAll .
  • The map descriptors mean "double" -> x * 2 , "square" -> x * x , "increment" -> x + 1 , "negate" -> -x , "identity" -> x .
  • For bind , the argument list is boundArgs followed by callArgs . "add" returns their sum, "multiply" returns their product, and "subtract" returns the first argument minus the second. For example, boundArgs = [10] and callArgs = [3] with "subtract" give [7] . The result is returned as a one-element list.
  • Each promises entry is [status, value] , where status is "resolved" or "rejected" . value is an integer, which may be given as a decimal string such as "42" or "-3" ; return it as an int. "First rejected entry" means first in array order.
  • Use exact integer arithmetic. A result may fall outside the 32-bit range (for example, the square of a large element).

Constraints

  • operation is one of "map" , "bind" , "promiseAll" .
  • For map : 1 <= len(arr) <= 10^4 ; each element fits in a 32-bit signed integer.
  • For bind : total argument count >= 1 ; subtract always receives exactly 2 total arguments.
  • For promiseAll : 1 <= len(promises) <= 10^4 .

Examples

Input: operation = "map", payload = {"arr": [1, 2, 3, 4, 5], "op": "square"}
Output: [1, 4, 9, 16, 25]

Applying the square callback to each element of [1, 2, 3, 4, 5] gives [1, 4, 9, 16, 25], the same array Array.prototype.map would build.

Input: operation = "bind", payload = {"fn": "add", "boundArgs": [10, 20], "callArgs": [5]}
Output: [35]

bind pre-binds 10 and 20, so calling with 5 invokes add(10, 20, 5), which is 35, returned as [35].

Input: operation = "promiseAll", payload = {"promises": [["resolved", "3"], ["rejected", "7"], ["rejected", "9"]]}
Output: [7]

The second entry is the first rejection in array order, so the result is its value alone. The resolved value 3 and the later rejection 9 are not part of the output.