Resolve Callable Names from Argument Type Lists
Arrays & HashingAuto-graded · 24 test casesLast 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]isFalse) with parameter listpof lengthm: it accepts a callcexactly whenlen(c) == mandc[t] == p[t]for every positiont. Count, types and order must all agree. -
Repeatable-tail signature (
sig_variadic[i]isTrue) with parameter listpof lengthm >= 1: the firstm - 1entries form a fixed prefix, andp[m - 1]is the repeatable type. It accepts a callcexactly when all of these hold:len(c) >= m - 1,c[t] == p[t]for everyt < m - 1,c[t] == p[m - 1]for everyt >= 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 forcalls[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, wheren = 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_paramsand incalls, is 1 to 20 ASCII letters or digits. 0 <= len(sig_params[i]) <= 10, andlen(sig_params[i]) >= 1wheneversig_variadic[i]isTrue.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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Checking each signature against each call costs roughly n * Q * L comparisons, which is feasible here but wasteful. Instead, convert signatures into dictionary keys so that each call only probes a few candidate keys.
Build three dictionaries from the catalog:
- Fixed map: key is the full parameter tuple, value is the list of names. It holds fixed (non-repeatable) signatures. A call matches one exactly when its tuple equals the key.
- Zero-repeat map: key is the prefix
p[:-1]of a repeatable-tail signature. It handles the case where the repeatable parameter receives no arguments, so the call must equal the prefix. - Some-repeat map: key is
(prefix, repeat_type). It handles one or more repeated arguments.
For the third case, suppose a repeatable-tail signature with prefix length k accepts a call c with at least one repeated argument. Then c[:k] is the prefix, c[k:] is non-empty, and every element of c[k:] is the repeat type. So the repeat type must be c[-1], and k must lie in the trailing run of elements equal to c[-1]. For each call, start at split n - 1 and move left while the previous element equals the last one, probing (c[:split], c[-1]) at each step.
For each call, gather the hits from the fixed map, the zero-repeat map and the some-repeat probes, then sort.
Why it is correct
- Every accepted call is found. A fixed match is the first probe. A repeatable match with no repeats is the second probe. A repeatable match with
j >= 1repeats has repeat typec[-1]and splitn - jinside the trailing run, which the loop visits. - Nothing wrong is reported. Each probe's key encodes the whole acceptance condition, and the loop only reaches a split if everything after it equals the last argument.
- No duplicates. Fixed and repeatable signatures live in different maps. A repeatable signature has one fixed prefix length
k, so it can only be hit at splitk, and it is either a zero-repeat hit (n == k) or a some-repeat hit (n > k), never both. Names are distinct. - Default
sort()yields the required code point order.
Complexity
Let m <= 10 be the maximum parameter count and L <= 20 the maximum call length.
- Building the maps:
O(n * m). - Per call: at most
L + 2probes, each hashing a tuple of length up toL, soO(L^2)plusO(r log r)to sortrresults. - Total time:
O(n*m + Q*L^2 + R log R)whereRis the total output size. - Space:
O(n*m)for the maps plus the output.
Reference solution
from typing import List
def resolve_calls(sig_names: List[str], sig_params: List[List[str]], sig_variadic: List[bool], calls: List[List[str]]) -> List[List[str]]:
fixed_map = {} # full parameter tuple -> names (fixed signatures)
tail_zero = {} # prefix tuple -> names (repeatable tail, zero repeats)
tail_some = {} # (prefix tuple, repeat type) -> names (repeatable tail, >= 1 repeats)
for name, p, var in zip(sig_names, sig_params, sig_variadic):
if var:
prefix = tuple(p[:-1])
tail_zero.setdefault(prefix, []).append(name)
tail_some.setdefault((prefix, p[-1]), []).append(name)
else:
fixed_map.setdefault(tuple(p), []).append(name)
answer = []
for call in calls:
n = len(call)
tc = tuple(call)
found = []
found.extend(fixed_map.get(tc, ()))
found.extend(tail_zero.get(tc, ()))
if n > 0:
last = call[-1]
split = n - 1
while True:
found.extend(tail_some.get((tc[:split], last), ()))
if split == 0 or call[split - 1] != last:
break
split -= 1
found.sort()
answer.append(found)
return answer