FastPrepMatch a Variadic Function Signature
Problem · Array

Match a Variadic Function Signature

Learn this problem
EasyConfluent logoConfluentFULLTIMEPHONE SCREEN

Problem statement

You are building a function library. Each registered function has a unique name, an ordered list of parameter-type tokens, and an isVariadic flag.

The four parallel inputs describe the library and one lookup:

  • functionNames[i] is the name of function i.
  • functionArgumentTypes[i] is its declared parameter-type list.
  • isVariadic[i] says whether its final declared parameter is variadic.
  • callArgumentTypes is the ordered argument-type list to match.

Return the names of all matching functions, in their original functionNames order.

Matching rules

  • Type tokens match by exact, case-sensitive string equality.
  • A non-variadic function matches only when its declared list has exactly the same length as the call and every token matches at the same position.
  • For a variadic function, the final declared type must occur one or more times. The call must therefore contain at least as many arguments as the declared list.
  • For a variadic declaration of length m, positions 0 through m - 2 must match the fixed prefix. Every call position from m - 1 onward must equal the final declared type.

For example, the variadic declaration [String, Integer] matches [String, Integer] and [String, Integer, Integer], but it does not match [String].

Function

findMatchingFunctions(functionNames: String[], functionArgumentTypes: String[][], isVariadic: boolean[], callArgumentTypes: String[]) → String[]

Examples

Example 1

functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["String","Integer","Integer"]return = ["FuncA","FuncB"]

FuncA is an exact three-parameter match. FuncB also matches because its final Integer parameter appears twice. Their names are returned in registration order.

Example 2

functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["Integer"]return = ["FuncC","FuncG"]

FuncC is variadic and its required final Integer appears once. FuncG is the exact one-parameter match.

Example 3

functionNames = ["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]functionArgumentTypes = [["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]isVariadic = [false,true,true,true,false,false,false]callArgumentTypes = ["String"]return = ["FuncF"]

FuncF is the only exact match. FuncB does not match because a variadic final parameter must occur at least once.

Constraints

  • 1 <= functionNames.length == functionArgumentTypes.length == isVariadic.length <= 20000.
  • Function names are unique non-empty ASCII strings of at most 50 characters.
  • 1 <= functionArgumentTypes[i].length, and the total number of declared type tokens is at most 200000.
  • 0 <= callArgumentTypes.length <= 200000.
  • Every type token is a non-empty ASCII string of at most 50 characters.
  • Different functions may have identical signatures and flags; every matching name must still be returned.

More Confluent problems

drafts saved locally
public String[] findMatchingFunctions(String[] functionNames, String[][] functionArgumentTypes, boolean[] isVariadic, String[] callArgumentTypes) {
    // Write your code here.
}
functionNames["FuncA","FuncB","FuncC","FuncD","FuncE","FuncF","FuncG"]
functionArgumentTypes[["String","Integer","Integer"],["String","Integer"],["Integer"],["Integer","Integer"],["Integer","Integer","Integer"],["String"],["Integer"]]
isVariadic[false,true,true,true,false,false,false]
callArgumentTypes["String","Integer","Integer"]
expected["FuncA", "FuncB"]
checking account