KatInterview

Check a Call Against a Catalog of Function Signatures

Arrays & HashingAuto-graded · 23 test cases
EasyAuto-gradedArrays & Hashingsimulationarray-scanningsignature-matching

Last updated

Decide whether an ordered list of argument types fits any function in a catalog, given required, trailing optional, and variadic parameters with exact type matching.

Signature Catalog Lookup

You are given a catalog of function signatures and a single call described only by the types of its arguments. Decide whether at least one function in the catalog could be invoked with that call.

Signature format

Each catalog entry is a dictionary with three keys:

  • "required": a list of type names. These parameters must all be supplied, in order, at the start of the call.
  • "optional": a list of type names. These parameters come right after the required ones. They may be omitted, but only from the end. If the call supplies k of them, it supplies the first k of the list.
  • "variadic": either null (None) or a single type name. If it is a type name, the function accepts any number of extra arguments (zero or more) of exactly that type, placed after all required and optional parameters.

So the full parameter order of a function is: required parameters, then optional parameters, then the variadic parameter.

Matching rules

A call (a list of type names) matches a function when all of the following hold:

  1. The call has at least as many arguments as there are required parameters.
  2. If the function has no variadic parameter, the call has no more arguments than len(required) + len(optional).
  3. Each argument is compared by position with the concatenated list required + optional. For every position that exists in both the call and that list, the type names must be identical (case-sensitive string equality; there is no coercion or subtyping).
  4. If the function has a variadic type, every argument beyond the required + optional portion must be exactly that type. Zero such arguments is fine.

Return True if at least one function in the catalog matches the call, otherwise False. An empty catalog never matches.

Task

Implement is_call_supported(catalog, call_args), where catalog is the list of signature dictionaries and call_args is the list of argument type names. Return a boolean.

Constraints

  • 0 <= len(catalog) <= 10^4
  • Each signature has at most 50 required and 50 optional parameters.
  • 0 <= len(call_args) <= 200
  • Type names are non-empty strings.
  • Matching is exact string equality with no implicit conversions.

Examples

Example 1

Input: is_call_supported([{'required': ['int', 'str'], 'optional': [], 'variadic': None}], ['int', 'str'])

Output: True

Example 2

Input: is_call_supported([{'required': ['int'], 'optional': ['str'], 'variadic': None}], ['int'])

Output: True

Example 3

Input: is_call_supported([{'required': ['int'], 'optional': [], 'variadic': 'float'}], ['int', 'float', 'float'])

Output: True

Hints

Hint 1

Handle each catalog entry independently and return True as soon as one entry accepts the call. If none do, return False.

Hint 2

Cheap length checks first: too few arguments for the required list, or too many when there is no variadic type, rule an entry out immediately.

Hint 3

Concatenate required and optional into one list and compare it position by position with the call over the shorter length. Any arguments past that point must all equal the variadic type, if there is one.