Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Merge Similar Items
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible