Problem · Array

Deep Merge with a Conflict Resolver

Learn this problem
MediumAutodesk logoAutodeskFULLTIMEONSITE INTERVIEW

Problem statement

Two nested objects are represented by flattened integer leaves in the form path=value. A path is one or more dot-separated keys. Paths within one object are unique, and no path is a prefix of another path.

Deep-merge first and second. Keep every path that appears in only one object. When a path appears in both, apply resolver to the first and second values:

  • SUM: add them.
  • MAX: take the larger.
  • MIN: take the smaller.
  • SECOND: take the second object's value.

Return every merged path=value entry sorted lexicographically by path.

Function

deepMerge(first: String[], second: String[], resolver: String) → String[]

Examples

Example 1

first = ["user.age=30","user.score=5","version=1"]second = ["user.score=7","user.name=2","version=3"]resolver = "SUM"return = ["user.age=30","user.name=2","user.score=12","version=4"]

Unique paths survive unchanged, while SUM resolves the overlapping score and version paths.

Example 2

first = ["a.b=-2","z=4"]second = ["a.b=5","m=8"]resolver = "MIN"return = ["a.b=-2","m=8","z=4"]

MIN keeps -2 at the shared path a.b, and the output paths are sorted.

Constraints

  • 0 <= first.length, second.length <= 100000.
  • Each path has between 1 and 200 ASCII letters, digits, underscores, or dots and is valid under the documented nesting rules.
  • Each value is a signed 32-bit integer, and every SUM result fits in a signed 64-bit integer.
  • resolver is one of SUM, MAX, MIN, or SECOND.

More Autodesk problems

drafts saved locally
public String[] deepMerge(String[] first, String[] second, String resolver) {
    // Write your code here.
}
first["user.age=30","user.score=5","version=1"]
second["user.score=7","user.name=2","version=3"]
resolver"SUM"
expected["user.age=30", "user.name=2", "user.score=12", "version=4"]
checking account