KatInterview

Resolve Callable Names from Argument Type Lists

Arrays & HashingAuto-graded · 24 test cases
MediumAuto-gradedArrays & Hashinghashingstringsvariadic-matching

Last updated

Match call argument types against registered signatures, some with a trailing repeatable parameter, and return the sorted names of every signature that fits each call.

Overloaded Call Resolver

A code editor keeps a catalog of function signatures. Each signature has a name, an ordered list of parameter types, and a boolean saying whether its last parameter is repeatable (like *args in Python or Type... in Java). For a call described only by the types of its arguments, you must report every catalog entry that could legally be invoked with those arguments.

The catalog is built once, and then a batch of calls is resolved against it.

Signature i consists of sig_names[i], sig_params[i] and sig_variadic[i]. Call j is the list of argument types calls[j].

Function

def resolve_calls(sig_names: List[str], sig_params: List[List[str]], sig_variadic: List[bool], calls: List[List[str]]) -> List[List[str]]:

Matching rules

  • Types are compared as plain strings, case-sensitively. There is no subtyping, conversion or generics, so "Integer" is different from "Number", "integer" and "Long".

  • Fixed signature (sig_variadic[i] is False) with parameter list p of length m: it accepts a call c exactly when len(c) == m and c[t] == p[t] for every position t. Count, types and order must all agree.

  • Repeatable-tail signature (sig_variadic[i] is True) with parameter list p of length m >= 1: the first m - 1 entries form a fixed prefix, and p[m - 1] is the repeatable type. It accepts a call c exactly when all of these hold:

    • len(c) >= m - 1,
    • c[t] == p[t] for every t < m - 1,
    • c[t] == p[m - 1] for every t >= m - 1.

    The repeatable parameter takes zero or more arguments, so the call may end right after the prefix.

  • A fixed signature with no parameters accepts only the empty call. A repeatable-tail signature with a single parameter accepts every call whose arguments all have that type, including the empty call.

  • Return one list per call, in the same order as calls. The list for calls[j] contains the names of all signatures that accept it, sorted ascending with Python's default string ordering (code point order, so uppercase letters come before lowercase). If nothing accepts the call, its list is empty.

Constraints

  • 1 <= n <= 1000, where n = len(sig_names) == len(sig_params) == len(sig_variadic)
  • 1 <= len(calls) <= 1000
  • Signature names are pairwise distinct, 1 to 20 characters long, made of ASCII letters, digits and _.
  • Every type string, in sig_params and in calls, is 1 to 20 ASCII letters or digits.
  • 0 <= len(sig_params[i]) <= 10, and len(sig_params[i]) >= 1 whenever sig_variadic[i] is True.
  • 0 <= len(calls[j]) <= 20
  • The total number of names across all returned lists is at most 1,000,000.

Examples

Example 1

Input: resolve_calls(['funcA', 'funcB'], [['String', 'Integer'], ['String', 'Integer', 'Integer']], [False, True], [['String', 'Integer'], ['String', 'Integer', 'Integer', 'Integer'], ['String']])

Output: [['funcA', 'funcB'], ['funcB'], []]

Example 2

Input: resolve_calls(['foo', 'bar'], [['String', 'Integer'], ['String', 'Integer']], [False, True], [['String'], ['String', 'Integer'], ['String', 'Integer', 'Integer'], ['String', 'Boolean'], ['Integer', 'String']])

Output: [['bar'], ['bar', 'foo'], ['bar'], [], []]

Example 3

Input: resolve_calls(['now', 'printAll', 'pair'], [[], ['String'], ['Integer', 'Integer']], [False, True, False], [[], ['String', 'String'], ['string'], ['Integer', 'Integer']])

Output: [['now', 'printAll'], ['printAll'], [], ['pair']]

Hints

Hint 1

Checking every signature against every call works within the limits, but think about how to turn each signature into a dictionary key so a call only needs a handful of lookups.

Hint 2

For a repeatable-tail signature, split the cases: zero repeated arguments means the call equals the fixed prefix exactly; one or more means the call is the prefix followed by copies of one type.

Hint 3

If at least one argument goes to the repeatable parameter, its type must equal the last argument of the call, and the repeated part lies inside the trailing run of equal types at the end of the call. Only the split points inside that run need to be tried.