KatInterview

Screening a Vendor Identity-Check Response

Arrays & HashingAuto-graded · 22 test cases
EasyAuto-gradedArrays & Hashingvalidationhash-maptype-checking

Last 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):

  • approved is a bool.
  • reason is 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 (True or False).
  • 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:

  1. If status is not exactly "COMPLETE", return (False, "STATUS_INCOMPLETE").
  2. If any entry of message_codes is in denied_codes, return (False, "BLOCKED_CODE").
  3. If any of the four fields name, dob, ssn, address in matches is False, return (False, "MATCH_FAILED").
  4. Otherwise return (True, "SUCCESS").

All string comparisons are case-sensitive.

Constraints

  • The response has at most 50 top-level keys.
  • denied_codes has at most 1,000 strings.
  • message_codes has at most 50 strings.
  • The response may be any Python value, not necessarily a dictionary (for example None or 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.