Unfortunately I ran into a question that wasn't posted here before. This was a few weeks ago, and the interviewer was a woman.
Question: Chain Validation
The system has a set of validation rules. Each rule has two fields:
[rule name, result]
The result can be one of three things:
"pass" — the rule passes
"fail" — the rule fails
the name of another rule — meaning the final result of the current rule depends on that other rule
For example:
rules = [ ["rule_A", "pass"], ["rule_B", "rule_C"], ["rule_C", "fail"] ]
Here:
rule_B -> rule_C -> fail
so rule_B's final result is also fail.
You need to implement two functions.
- isValid
Check whether the input is valid.
isValid(rules) -> boolean
For each rule [name, result], result must be:
"pass", "fail", or the name of another rule that exists in the input.
Return true if every rule satisfies this; otherwise return false.
For example:
[ ["rule_A", "pass"], ["rule_B", "rule_C"], ["rule_C", "fail"] ]
returns:
true
While:
[ ["rule_A", "rule_X"] ]
returns false, since rule_X doesn't exist.
- isEligible
Check whether the entire input ultimately resolves to all pass.
isEligible(rules) -> boolean
If a rule's result is another rule, you need to keep following the reference until you get either:
pass or fail.
Only return true if every rule ultimately resolves to pass.
For example:
[ ["rule_A", "pass"], ["rule_B", "rule_C"], ["rule_C", "pass"] ]
Resolves to:
rule_A -> pass
rule_B -> rule_C -> pass
rule_C -> pass
So: isValid -> true, isEligible -> true
If instead:
[ ["rule_A", "pass"], ["rule_B", "rule_C"], ["rule_C", "fail"] ]
then:
rule_B -> rule_C -> fail
so: isValid -> true, isEligible -> false
Addendum (2026-08-20 01:21 +08:00): I interviewed for the mid-level role. The first two rounds were coding, and another round was a question that's already been posted here before.
Discussion
Loading comments…