FastPrepFlat JSON Value Differences

Flat JSON Value Differences

Chubb logoChubb● EasyNEW GRADOA
Learn

Problem statement

Each input is a valid JSON object with unique alphanumeric keys and string values. Return all keys present in both objects whose values differ.

Return the keys in ascending lexicographic order. Keys appearing in only one object are ignored.

Function

differentCommonKeys(firstJson: String, secondJson: String) → String[]

Examples

Example 1

firstJson = "{\"name\":\"John\",\"age\":\"30\",\"status\":\"active\"}"secondJson = "{\"name\":\"John\",\"age\":\"31\",\"status\":\"inactive\",\"role\":\"admin\"}"return = ["age","status"]

Only common keys with unequal string values survive, then lexicographic sorting determines the result.

Example 2

firstJson = "{}"secondJson = "{}"return = []

Only common keys with unequal string values survive, then lexicographic sorting determines the result.

Example 3

firstJson = "{\"a\":\"1\"}"secondJson = "{\"a\":\"1\"}"return = []

Only common keys with unequal string values survive, then lexicographic sorting determines the result.

Constraints

  • Each JSON string has length at most 100000.
  • Objects are flat and contain only unique ASCII alphanumeric keys and ASCII alphanumeric string values.
  • Whitespace may appear outside quoted strings.

Source note: Author-written prompt excerpt retained for source fidelity.

More Chubb problems

See Chubb hiring insights
public String[] differentCommonKeys(String firstJson, String secondJson) {
    // Write your code here.
}
firstJson"{\"name\":\"John\",\"age\":\"30\",\"status\":\"active\"}"
secondJson"{\"name\":\"John\",\"age\":\"31\",\"status\":\"inactive\",\"role\":\"admin\"}"
expected["age", "status"]
Checking account…