Problem · Array

Match a Variadic Function Signature

Learn this problem
EasyConfluent logoConfluentFULLTIMEPHONE SCREEN

Problem statement

You are given the declared parameter type tokens of one function in parameterTypes, a flag isVariadicFunction, and the ordered argument type tokens of one call in argumentTypes.

Return true if the call matches the declared signature. Otherwise, return false.

Matching rules

  • Type tokens are opaque strings. Two tokens match only when they are exactly equal, including case. There is no trimming, aliasing, coercion, subtyping, or wildcard behavior.
  • When isVariadicFunction is false, the call must have exactly the same number of arguments as the signature, and every token must match at the same position.
  • When isVariadicFunction is true, parameterTypes is non-empty. Its final token is the variadic type, while all earlier tokens form the fixed prefix.
  • The variadic type may occur zero or more times. Therefore, a variadic signature with m declared tokens accepts calls with at least m - 1 arguments.
  • Every argument after the fixed prefix must exactly equal the final declared variadic token.

Function

matchesFunctionSignature(parameterTypes: String[], isVariadicFunction: boolean, argumentTypes: String[]) → boolean

Examples

Example 1

parameterTypes = ["String","Integer"]isVariadicFunction = falseargumentTypes = ["String","Integer"]return = true

This is a fixed signature. The call has the same arity, and both type tokens match at their positions.

Example 2

parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String","Integer","Integer","Integer"]return = true

String is the fixed prefix. Every remaining argument exactly matches the variadic type Integer.

Example 3

parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String"]return = true

The fixed prefix matches, and the final variadic type is allowed to occur zero times.

Example 4

parameterTypes = ["String","Integer"]isVariadicFunction = trueargumentTypes = ["String","Integer","Double"]return = false

The last argument is Double, which does not exactly match the variadic type Integer.

Constraints

  • 0 <= parameterTypes.length <= 10^5
  • 0 <= argumentTypes.length <= 10^5
  • If isVariadicFunction is true, then parameterTypes.length >= 1.
  • Every type token is a non-empty ASCII string of at most 100 characters.

More Confluent problems

drafts saved locally
public boolean matchesFunctionSignature(String[] parameterTypes, boolean isVariadicFunction, String[] argumentTypes) {
    // Write your code here.
}
parameterTypes["String","Integer"]
isVariadicFunctionfalse
argumentTypes["String","Integer"]
expectedtrue
checking account