Skip to content

ORIGINAL CODING QUESTION

Checksum of merged sorted feeds

Several feeds each deliver values in nondecreasing order, and some feeds may be empty. Merge all feeds into one nondecreasing sequence x1, x2, ..., xT and return the checksum 1·x1 + 2·x2 + ... + T·xT. An entirely empty merge has checksum 0.

Open practice workspace

Original exercise · medium. Editorial role relevance does not imply a company has asked this question.

Input and output

m, then m feeds. Each feed is its length L followed by L nondecreasing values.

Constraints

1 ≤ m ≤ 50; 0 ≤ L; at most 200 values in total; -100 ≤ value ≤ 100.

Example 1

Input

2 3 1 4 6 2 2 5

Output

67

The merge is 1, 2, 4, 5, 6, so the checksum is 1 + 4 + 12 + 20 + 30 = 67.

Example 2

Input

3 0 2 3 3 1 -2

Output

13

The empty feed adds nothing; the merge -2, 3, 3 gives -2 + 6 + 9 = 13.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

The next merged value is always the smallest unread head among the feeds.

Hint 2

A min-heap of (value, feed, position) finds that head quickly.

Hint 3

After taking a head, push the next value from the same feed.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →