The Differences Between Two Objects interview question is a common task in frontend or full-stack engineering interviews (often in JavaScript). Given two objects (or arrays), you need to return a new object that represents the deep differences between them. If a key exists in both and the values are different, you include the key with an array [oldValue, newValue]. If the values are identical, or if a key exists in only one object, it should be ignored. The comparison must be recursive for nested objects and arrays.
Google and Couchbase use this to test a candidate's mastery of Recursion and their understanding of data types. It evaluations your ability to traverse complex, nested structures and handle different edge cases (like comparing an object to an array or handling primitive vs. non-primitive types). This is a practical problem that mirrors tasks like state reconciliation in React or implementing "undo/redo" and "diffing" algorithms.
This is a Recursive Traversal problem.
[val1, val2] pair if they differ.Object A: {"a": 1, "b": {"c": 2}}
Object B: {"a": 1, "b": {"c": 3}, "d": 4}
{"c": [2, 3]}.{"b": {"c": [2, 3]}}.typeof [] is "object").Understand the difference between Deep Equality and Deep Diffing. While they both use recursion, a diffing algorithm must build a new structure rather than just returning a boolean. Practice using Object.keys() and Array.isArray() to navigate dynamic JSON structures efficiently.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Adjacent Increasing Subarrays Detection II | Medium | Solve | |
| All Divisions With the Highest Score of a Binary Array | Medium | Solve | |
| All Paths from Source Lead to Destination | Medium | Solve | |
| Alphabet Board Path | Medium | Solve | |
| Ambiguous Coordinates | Medium | Solve |