Screening a Vendor Identity-Check Response
Arrays & HashingAuto-graded · 22 test casesLast updated
Validate a nested vendor response with strict type checks, then apply ordered business rules to return the first failing reason code.
Screening a Vendor Identity-Check Response
A lending service receives identity-check results from an outside vendor. Before an applicant can move forward, the response must be checked for correct structure and then run through a fixed list of decision rules.
Implement check_identity_response(response, denied_codes), which returns a pair (approved, reason):
approvedis abool.reasonis one of the exact strings"INVALID_SCHEMA","STATUS_INCOMPLETE","BLOCKED_CODE","MATCH_FAILED", or"SUCCESS".
denied_codes is a collection of strings (you only need to test membership in it).
Step 1: Structure check
The response is well-formed only if all of the following hold:
- It is a dictionary.
- It has a
"status"key whose value is a string. - It has a
"matches"key whose value is a dictionary containing the keys"name","dob","ssn"and"address", each mapped to a real boolean (TrueorFalse). - It has a
"message_codes"key whose value is a list in which every element is a string.
Type checks are strict and use the exact type. Numbers such as 1 or 0 are not booleans, strings such as "true" are not booleans, and a tuple is not a list. Extra keys in the response or inside "matches" are allowed, and an empty "message_codes" list is allowed.
If any part of the structure check fails, return (False, "INVALID_SCHEMA"). This takes priority over every rule below, even if the status or matches would otherwise fail.
Step 2: Decision rules
For a well-formed response, apply these rules in order and return at the first one that fails:
- If
statusis not exactly"COMPLETE", return(False, "STATUS_INCOMPLETE"). - If any entry of
message_codesis indenied_codes, return(False, "BLOCKED_CODE"). - If any of the four fields
name,dob,ssn,addressinmatchesisFalse, return(False, "MATCH_FAILED"). - Otherwise return
(True, "SUCCESS").
All string comparisons are case-sensitive.
Constraints
- The response has at most 50 top-level keys.
denied_codeshas at most 1,000 strings.message_codeshas at most 50 strings.- The response may be any Python value, not necessarily a dictionary (for example
Noneor a list).
Examples
Example 1
Input: check_identity_response({'status': 'COMPLETE', 'matches': {'name': True, 'dob': True, 'ssn': True, 'address': True}, 'message_codes': ['ADDR_CONFIRMED', 'PHONE_VERIFIED']}, ['FRAUD_ALERT', 'DECEASED'])
Output: [True, 'SUCCESS']
Example 2
Input: check_identity_response({'status': 'COMPLETE', 'matches': {'name': True, 'dob': True, 'ssn': True, 'address': False}, 'message_codes': ['FRAUD_ALERT']}, ['FRAUD_ALERT', 'DECEASED'])
Output: [False, 'BLOCKED_CODE']
Hints
Hint 1
Split the work into two phases: first confirm the shape and types of everything you will read, then apply the rules. That way the rules can never crash on bad data.
Hint 2
In Python, isinstance(True, int) is True and a value like 1 passes many loose checks. Compare the exact type, e.g. type(x) is bool, to reject numbers posing as booleans.
Hint 3
Apply the rules in the stated order and return immediately on the first failure. Test membership of each message code in denied_codes directly rather than iterating over all denied codes.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Handle the problem in two phases.
- Structure check. Before reading any value for a decision, confirm the response is a dictionary, that
statusis a string, thatmatchesis a dictionary holding the four required keys with real boolean values, and thatmessage_codesis a list of strings. Any failure returns(False, "INVALID_SCHEMA"). - Decision rules. Run the rules in the stated order (status, denied codes, matches) and return on the first failure. If none fail, return success.
Types are compared with type(x) is ... instead of isinstance. This matters for booleans: True is an instance of int, and the reverse confusion lets values like 1 slip through loose checks. Exact-type checks also reject tuples posing as lists. Extra keys and empty code lists are simply ignored.
Why it is correct
- Every value the rules read has already been verified to exist and have the right type, so no rule can raise an exception or be fooled by a truthy non-boolean.
- Structure problems are reported before any rule, which matches the required priority.
- Rules return as soon as they fail and are checked in the required order, so the reported reason is always the highest-priority failure. For instance, a denied code beats a failed match.
(True, "SUCCESS")is only reachable when every earlier check has passed.
Complexity
Let K be the number of message codes (at most 50).
- Time: O(K). The structure check touches a constant number of keys plus each code once, and each denied-code test is a single membership lookup. We never iterate over
denied_codesitself. - Space: O(1) extra, since generators are used inside
anyandall.
Reference solution
from typing import Set, Tuple
def check_identity_response(response: dict, denied_codes: Set[str]) -> Tuple[bool, str]:
fields = ("name", "dob", "ssn", "address")
# Phase 1: structure and strict type checks
if not isinstance(response, dict):
return (False, "INVALID_SCHEMA")
if type(response.get("status")) is not str:
return (False, "INVALID_SCHEMA")
matches = response.get("matches")
if not isinstance(matches, dict):
return (False, "INVALID_SCHEMA")
for f in fields:
if f not in matches or type(matches[f]) is not bool:
return (False, "INVALID_SCHEMA")
codes = response.get("message_codes")
if type(codes) is not list or any(type(c) is not str for c in codes):
return (False, "INVALID_SCHEMA")
# Phase 2: decision rules in priority order
if response["status"] != "COMPLETE":
return (False, "STATUS_INCOMPLETE")
if any(c in denied_codes for c in codes):
return (False, "BLOCKED_CODE")
if not all(matches[f] for f in fields):
return (False, "MATCH_FAILED")
return (True, "SUCCESS")