JavaScriptHardRecursion35 min

Compare two JSON values deeply

Return whether two JSON values are deeply equal. Object key order is irrelevant, array order matters, and primitive values must have the same type and value. All input is valid JSON satisfying the constraints; do not mutate the meaning of values through type coercion. Interviewers use this problem to assess compare types, own key sets and corresponding children recursively.

Input

A JSON array [firstValue, secondValue].

Output

Print one compact JSON value: a boolean.

Constraints

Values are JSON-compatible; total nodes <= 10000; depth <= 100; strings and property names length <= 100; numbers are integers with absolute value <= 1000000.

Example

Input

[{"a":[1,{"b":true}],"z":null},{"z":null,"a":[1,{"b":true}]}]

Output

true

Example 2 input

[{"__proto__":1},{"constructor":1}]

Output

false

Boundary case: Do not compare JSON.stringify results because object key order may differ

Public test cases (2)

  • Input

    [{"a":[1,{"b":true}],"z":null},{"z":null,"a":[1,{"b":true}]}]

    Expected output

    true
  • Input

    [[1,2],[2,1]]

    Expected output

    false
Hints (2)
  1. Handle identical primitives and null before inspecting keys
  2. Do not compare JSON.stringify results because object key order may differ

Target complexity: time O(n) expected, with bounded key lengths · space O(n) including keys and recursion

Your solution

Checking runner availability…

Reference solution

Sign in for solution
All JavaScript programs