Check a Call Against a Catalog of Function Signatures
Arrays & HashingAuto-graded · 23 test casesLast 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 supplieskof them, it supplies the firstkof the list."variadic": eithernull(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:
- The call has at least as many arguments as there are required parameters.
- If the function has no variadic parameter, the call has no more arguments than
len(required) + len(optional). - 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). - If the function has a variadic type, every argument beyond the
required + optionalportion 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Check every signature in the catalog independently. The call is supported if any one signature accepts it.
For one signature:
- Build
fixed = required + optional, the ordered list of non-variadic parameter types. - Reject if the call has fewer arguments than there are required parameters.
- Reject if there is no variadic type and the call has more arguments than
len(fixed). - Compare the call with
fixedposition by position over the firstmin(len(call), len(fixed))positions. Any mismatch rejects the signature. Because optional parameters can only be dropped from the end, a call that supplieskoptional arguments must match the firstkoptional types, which is exactly what this prefix comparison checks. - If a variadic type exists, every remaining argument after
fixedmust equal it. Zero remaining arguments is accepted. - If none of the checks rejected the signature, return
True.
If no signature accepts the call, return False. This also covers an empty catalog.
Why it is correct
The matching rules say a call is a prefix-compatible assignment to required + optional, followed by a run of variadic-typed arguments. The length checks guarantee the required parameters are all covered and that no argument is left over when no variadic type exists. The prefix comparison verifies exact types for all fixed positions that are used. The tail check verifies the variadic portion. Together these are exactly the stated conditions, so the function returns True precisely when some signature accepts the call.
Complexity
Let S be the number of signatures, P the maximum number of fixed parameters, and A the number of call arguments.
- Time: O(S * (P + A)) in the worst case. Length checks often reject early.
- Space: O(P) per signature for the concatenated list, which is not retained across signatures.
Reference solution
from typing import Dict, List
def is_call_supported(catalog: List[Dict], call_args: List[str]) -> bool:
n = len(call_args)
for sig in catalog:
required = sig["required"]
optional = sig["optional"]
variadic = sig["variadic"]
fixed = required + optional
if n < len(required):
continue
if variadic is None and n > len(fixed):
continue
overlap = min(n, len(fixed))
if any(call_args[i] != fixed[i] for i in range(overlap)):
continue
if variadic is not None and any(t != variadic for t in call_args[len(fixed):]):
continue
return True
return False