Deep Merge of Two Objects — LeetCode 2755 Python Solution

MediumLeetCode PremiumJavaScript
Problem
#2755
Reading time
2 min

The problem

Given two values obj1 and obj2, return a deepmerged value. Values should be deepmerged according to these rules: If the two values are objects, the resulting object should have all the keys that exist on either object.

Example

Input
obj1 = {"a": 1, "c": 3}, obj2 = {"a": 2, "b": 2}
Output
{"a": 2, "c": 3, "b": 2}
Explanation
The value of obj1["a"] changed to 2 because if both objects have the same key and their value is not an array or object then we change the obj1 value to the obj2 value. Key "b" with value was added to obj1 as it doesn't exist in obj1.

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2755. Deep Merge of Two Objects?
LeetCode 2755. Deep Merge of Two Objects is rated Medium on LeetCode.
What topics does LeetCode 2755. Deep Merge of Two Objects cover?
LeetCode 2755. Deep Merge of Two Objects is tagged JavaScript on LeetCode.
Is LeetCode 2755. Deep Merge of Two Objects a premium problem?
Yes. LeetCode 2755. Deep Merge of Two Objects is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview