JSON Deep Equal - Problem

Given two values o1 and o2, return a boolean value indicating whether the two values are deeply equal.

For two values to be deeply equal, the following conditions must be met:

  • If both values are primitive types (string, number, boolean, null), they are deeply equal if they pass the === equality check
  • If both values are arrays, they are deeply equal if they have the same elements in the same order, and each element is also deeply equal according to these conditions
  • If both values are objects, they are deeply equal if they have the same keys, and the associated values for each key are also deeply equal according to these conditions

Note: You may assume both values are the output of JSON.parse. In other words, they are valid JSON.

Constraint: Please solve it without using lodash's _.isEqual() function.

Input & Output

Example 1 — Basic Object Comparison
$ Input: o1 = {"name": "John", "age": 25}, o2 = {"name": "John", "age": 25}
Output: true
💡 Note: Both objects have same keys ("name", "age") with identical values ("John", 25), so they are deeply equal
Example 2 — Array with Different Elements
$ Input: o1 = [1, 2, 3], o2 = [1, 2, 4]
Output: false
💡 Note: Arrays have same length but different elements at index 2: 3 ≠ 4, so not deeply equal
Example 3 — Nested Structure Match
$ Input: o1 = {"user": {"id": 1, "pets": ["cat"]}}, o2 = {"user": {"id": 1, "pets": ["cat"]}}
Output: true
💡 Note: Both have same nested structure: user.id = 1 and user.pets = ["cat"] in both objects

Constraints

  • Both values are valid JSON (output of JSON.parse)
  • Objects can have nested properties of any depth
  • Arrays can contain mixed types (numbers, strings, objects, arrays)
  • Cannot use lodash's _.isEqual() function

Visualization

Tap to expand
JSON Deep Equal - Recursive Comparison INPUT Object o1: { "name": "John", "age": 25 } Object o2: { "name": "John", "age": 25 } Compare these two objects for deep equality 2 keys 2 keys ALGORITHM STEPS 1 Type Check Both objects? Continue Primitives? Use === 2 Key Count Check o1 keys: ["name","age"] o2 keys: ["name","age"] Length match: OK 3 Recurse on Values "name": "John"="John" OK "age": 25 === 25 OK 4 Early Termination If mismatch found, return false immediately All checks passed! No early termination --> return true FINAL RESULT true Comparison Summary: [OK] Same type (object) [OK] Same key count (2) [OK] name: "John"="John" [OK] age: 25 === 25 Output: true Key Insight: Optimized recursive comparison with early termination stops as soon as any mismatch is found, avoiding unnecessary comparisons. Handle primitives with ===, arrays with index matching, and objects with key-value recursive comparison. Time complexity: O(n) where n is total elements. TutorialsPoint - JSON Deep Equal | Optimized Recursive with Early Termination
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen