Rule References: Validity and Eligibility
Problem statement
Each row of rules is [name, result]. A result is the terminal pass, the terminal fail, or a rule name to follow.
Implement the following two predicates over the complete input:
isValid(rules)is true exactly when every result ispass,fail, or a name defined inrules. This checks reference existence only: a self-reference or a closed cycle of defined names is still structurally valid.isEligible(rules)is true exactly when the chain starting from every rule eventually reachespass. A chain ending atfail, referring to a missing name, or repeating a name without reaching a terminal makes the complete input ineligible.
The callable adapter validateRuleChains(rules) returns the two Boolean decisions in the fixed order [isValid(rules), isEligible(rules)]. Rule order does not affect the result, and a reference may point to a later row.
For this exercise, assume names are unique and case-sensitive, and the terminals are exactly lowercase pass and fail. For this exercise, assume an empty input returns [true,true]. In particular, [["A","B"],["B","A"]] returns [true,false]: its references exist, but neither rule ever reaches pass.
Function
validateRuleChains(rules: String[][]) → boolean[]Examples
Example 1
rules = [["rule_A","pass"],["rule_B","rule_C"],["rule_C","pass"]]return = [true,true]Every result is a terminal or a defined rule name. The chains are rule_A -> pass, rule_B -> rule_C -> pass, and rule_C -> pass. All three reach pass, so both predicates are true.
Example 2
rules = [["rule_A","pass"],["rule_B","rule_C"],["rule_C","fail"]]return = [true,false]All references exist, so isValid is true. Both rule_B and rule_C reach fail, while rule_A reaches pass. Since not every rule passes, isEligible is false.
Example 3
rules = [["rule_A","rule_X"]]return = [false,false]rule_X is not defined, so isValid is false. The only chain cannot reach pass, so isEligible is also false.
Constraints
- For this exercise, assume
0 <= rules.length <= 200. - Every row contains exactly two strings.
- For this exercise, assume each string has between
1and32ASCII letters, digits, or underscores. - Rule names are distinct and are never the reserved lowercase terminals
passorfail. A result may be any string in the stated alphabet, including a name that is not defined.