Problem · String

JSON Identity Verification

Learn this problem
MediumKikoff logoKikoffFULLTIMEPHONE SCREEN

Problem statement

Implement identity verification for one provider response. The array expected contains exactly four strings in this order: first name, last name, date of birth, and Social Security number.

The string providerJson must be a valid JSON object with this required structure:

{"response":{"identity":{"first_name":"...","last_name":"...","date_of_birth":"...","ssn":"..."},"error_codes":["..."]}}

Object keys may appear in any order, insignificant JSON whitespace is allowed, and unrelated extra fields are ignored. Each required identity value must be a JSON string, and error_codes must be an array containing only strings.

Compare all four identity values exactly as supplied. Do not trim whitespace, fold letter case, or reformat dates or Social Security numbers.

Return one canonical JSON string with a boolean verified field followed by a reasons array. If either the JSON syntax or any required path or type is invalid, return only the reason malformed_json. Otherwise append applicable reasons in this exact order:

  1. first_name
  2. last_name
  3. date_of_birth
  4. ssn
  5. provider_error, once when error_codes is non-empty

Set verified to true exactly when the reason list is empty. The returned string contains no spaces.

Function

verifyIdentity(expected: String[], providerJson: String) → String

Examples

Example 1

expected = ["Ada","Lovelace","1815-12-10","123-45-6789"]providerJson = "{\"response\":{\"identity\":{\"first_name\":\"Ada\",\"last_name\":\"Lovelace\",\"date_of_birth\":\"1815-12-10\",\"ssn\":\"123-45-6789\"},\"error_codes\":[]}}"return = "{\"verified\":true,\"reasons\":[]}"

Every required provider value exactly matches the corresponding expected string, and the provider supplied no error code, so verification succeeds.

Example 2

expected = ["Grace","Hopper","1906-12-09","111-22-3333"]providerJson = "{\"response\":{\"error_codes\":[\"UPSTREAM_TIMEOUT\"],\"identity\":{\"ssn\":\"999-88-7777\",\"date_of_birth\":\"1906-12-09\",\"last_name\":\"hopper\",\"first_name\":\"Grace\"}}}"return = "{\"verified\":false,\"reasons\":[\"last_name\",\"ssn\",\"provider_error\"]}"

The last name differs because comparison is case-sensitive, the Social Security number differs, and the non-empty provider error list adds one final provider_error reason.

Example 3

expected = ["Alan","Turing","1912-06-23","222-33-4444"]providerJson = "{\"response\":{\"identity\":{\"first_name\":\"Alan\"},\"error_codes\":[]}}"return = "{\"verified\":false,\"reasons\":[\"malformed_json\"]}"

The JSON syntax is valid, but required identity fields are missing. The response therefore follows the malformed-input result and does not attempt partial comparison.

Constraints

  • expected.length == 4.
  • Each expected identity string has length from 0 through 100.
  • 1 <= providerJson.length <= 10^5.
  • providerJson contains valid Unicode text and may contain any JSON value before structural validation.
  • A structurally valid provider response has unique object keys, the required object path shown above, four required string identity values, and a string-only error_codes array.
  • The canonical result contains only the fixed reason codes defined above.
drafts saved locally
public String verifyIdentity(String[] expected, String providerJson) {
    // Write your code here.
}
expected["Ada","Lovelace","1815-12-10","123-45-6789"]
providerJson"{\"response\":{\"identity\":{\"first_name\":\"Ada\",\"last_name\":\"Lovelace\",\"date_of_birth\":\"1815-12-10\",\"ssn\":\"123-45-6789\"},\"error_codes\":[]}}"
expected"{\"verified\":true,\"reasons\":[]}"
checking account