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

Custom Bind Polyfill

EasyPython

Problem

Pocket FM uses callback-heavy interfaces for episode cards and playback controls. Implement a Python model of JavaScript's Function.prototype.bind that preserves a function's receiver context and supports partial application of arguments.

Because JSON cannot represent executable functions, the function is identified by a name. The supported functions are:

  • format_episode: reads this_arg["title"] and returns "<title>: <prefix><episode>".
  • progress_label: reads this_arg["duration"] and returns "<current>/<duration> <unit>".

Implement custom_bind(function_name, this_arg, bound_args, call_args). It must create a bound callable internally, prepend bound_args to call_args, invoke the selected function with this_arg as its receiver, and return the result.

Formal Specification

  • function_name: a string, either "format_episode" or "progress_label".
  • this_arg: a dictionary containing the context used by the selected function.
  • bound_args: a list of arguments captured when binding occurs.
  • call_args: a list of arguments supplied when the bound function is later called.
  • Return the value produced by the selected function.

Constraints

  • function_name is either "format_episode" or "progress_label"
  • this_arg contains the fields required by the selected function
  • 0 <= len(bound_args) <= 10
  • 0 <= len(call_args) <= 10
  • The combined arguments match the selected function's parameters

Function Signature

def custom_bind(function_name, this_arg, bound_args, call_args):
Interviewer

Your question is Custom Bind Polyfill. 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.