Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Merge Similar Items

Problem Statement

Given two 2D arrays of item-value pairs, named items1 and items2.

  • item[i] = [value<sub>i</sub>, weight<sub>i</sub>], where each value<sub>i</sub> in these arrays is unique within its own array and is paired with a weight<sub>i</sub>.

Combine these arrays such that if a value appears in both, its weights are summed up. The final merged array should be sorted based on the value<sub>i</sub>.

Examples

  1. Example 1:
    • Input: items1 = [[1,2],[4,3]], items2 = [[2,1],[4,3],[3,4]]
    • Expected Output: [[1,2],[2,1],[3,4],[4,6]]

.....

.....

.....

Like the course? Get enrolled and start learning!
Nel Tu

Nel Tu

· 2 years ago

The problem description says

"Combine these arrays such that if an item appears in both, its values are summed up."

Isn't it rather the "weights" that are summed up ? That's what the examples show.

It's not the first confusing problem description that I come across.

Show 1 reply