Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Polyfill for Bind
00:00
5 left

Polyfill for Bind

HardPython

Problem

Implement a polyfill for Function.prototype.bind.

For this Python exercise, represent the target function with a JSON object containing an operation, and implement bind_polyfill(function_spec, this_arg, bound_args, call_args) to model binding and invocation. The result must apply this_arg, prepend bound_args before call_args, and support regular calls only; for example, a sum operation with base 10, bound arguments [5], and call arguments [3] returns 18, while a context-returning operation with null returns null.

Constraints

  • function_spec is a dictionary with an operation from sum_context, format_context, collect, return_context, or multiply_context.
  • this_arg is a JSON value, including a dictionary, string, number, boolean, or null.
  • bound_args and call_args are lists of JSON values.
  • The target is invoked as a regular function. Constructor behavior and JavaScript primitive boxing are out of scope.
  • The total number of bound and call arguments is at most 10^4.

Function Signature

def bind_polyfill(function_spec, this_arg, bound_args, call_args):
Interviewer

Your question is Polyfill for Bind. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.