Merge And Sort Segments — Problem Statement & Solution Guide

ArraysMediumNew or Existing ID
TimeO(N + m log m)
|
SpaceO(m)

Quick Answer & Algorithm Key Takeaway

Master coding challenges related to Arrays and solve the Merge And Sort Segments problem optimally.

TopicArrays
PatternNew or Existing ID
TimeO(N + m log m)
SpaceO(m)

Problem Description

You are given an array of segment objects. Each segment contains a unique integer id and an array scores holding zero or more integer values. Your task is to produce a new array where each element consists of the original id and a field total representing the sum of all integers in its scores array. The resulting array must be sorted in descending order by total. If two segments share the same total, preserve their relative order from the input. For an empty input array, return an empty array. All calculations should use 64‑bit signed integer arithmetic to avoid overflow.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Merge And Sort Segments"

medium

WHY DOES IT MATTER?

Reducing a collection to a single metric and then ranking it is a ubiquitous pattern in analytics, recommendation engines, and financial reporting; mastering it lets you turn raw logs into actionable insights efficiently.

OPTIMIZATION CHALLENGE

The key insight is to separate concerns: compute all sums in a single linear pass (avoiding repeated scans) and then apply a proven O(m log m) sort, rather than nesting the two operations.

REAL-WORLD CONNECTION

Think of a distributed log‑aggregation system where each microservice emits latency samples; you sum per service to get total latency and then sort services to spot the slowest – exactly the same reduction‑then‑sort pipeline.

When coding, first build a simple map of id→total using a for‑each loop; only after the map is complete, extract entries into an array and call the language’s built‑in sort with a custom comparator – this avoids accidental O(n^2) comparisons.

COMPLEXITY AT A GLANCE

⏱ Time:O(N + m log m)
💾 Space:O(m)

Core Theory — Why This Approach?

The problem is a classic example of a reduction followed by a sort. First, each segment’s scores array is reduced to a single scalar – the sum – which is an O(k) operation per segment where k is the length of its scores list. After reduction, we have a flat list of (id,total) pairs that must be ordered by total in descending order, which is a standard comparison‑based sort with O(m log m) time where m is the number of segments. A naive solution might recompute sums repeatedly or use nested loops to compare every pair, leading to O(m^2) or O(m·k) extra work, which quickly becomes prohibitive for large inputs. The optimal paradigm combines a linear pass to compute all totals (O(N) where N is the total number of scores across all segments) and then leverages an efficient sort, achieving overall O(N + m log m) time while using only O(m) auxiliary space.

Interview Questions on This Problem

Q1How would you handle segments that have an empty scores array when computing the total?

Treat an empty scores array as a sum of zero; during the reduction step, initialize the accumulator to 0 and add each score, so empty arrays naturally yield a total of 0.

Q2If the interview asks for the top‑k segments by total instead of the full ordering, what change would you make?

Replace the full sort with a min‑heap of size k (or use QuickSelect) to keep only the k largest totals, reducing the time to O(N + m log k) and space to O(k).

Q3Can you modify the solution to be stable when two segments have the same total?

Yes—include the original index or id as a secondary key in the comparator (e.g., sort by total descending, then by id ascending) so equal totals preserve a deterministic order.

Examples

Example 1

Input

[{"id":1,"scores":[5,3,2]},{"id":2,"scores":[4,4]},{"id":3,"scores":[]}]

Output

[{"id":1,"total":10},{"id":2,"total":8},{"id":3,"total":0}]

Explanation: Segment 1: 5+3+2=10; Segment 2: 4+4=8; Segment 3: no scores => 0. Sorting by total gives 10,8,0, so the order remains 1,2,3.

Example 2

Input

[{"id":10,"scores":[-1,2]},{"id":5,"scores":[0]},{"id":7,"scores":[3,3,3]}]

Output

[{"id":7,"total":9},{"id":10,"total":1},{"id":5,"total":0}]

Explanation: Segment 10: -1+2=1; Segment 5: 0=0; Segment 7: 3+3+3=9. Sorted descending yields totals 9,1,0, so ids appear as 7,10,5.

Example 3

Input

[]

Output

[]

Explanation: The input contains no segments, so the output is also an empty array.

Constraints

  • 1 <= number of segments <= 10^5
  • 0 <= length of scores array for each segment <= 10^4
  • -10^9 <= each score <= 10^9
  • All ids are distinct 32‑bit signed integers
  • Sum of scores for a segment fits in a 64‑bit signed integer

Optimal Approach & Strategy

Compute all totals in one pass, store them, and then sort the resulting list with a standard O(m log m) algorithm.

Brute Force Approach

For each segment, recompute its total by scanning its scores each time you need to compare two segments, leading to nested loops and O(m^2) comparisons.

Code Solutions

JavaScript Solution
Time: O(N + m log m)
function mergeAndSortSegments(segments) {
    let results = segments.map(seg => {
        let total = seg.scores.reduce((a, b) => a + b, 0);
        return { id: seg.id, total: total };
    });
    results.sort((a, b) => b.total - a.total);
    return results;
}

function main() {
    const fs = require('fs');
    const input = fs.readFileSync(0, 'utf8');
    const segments = JSON.parse(input);
    const results = mergeAndSortSegments(segments);
    console.log(JSON.stringify(results));
}

main();

Asked in Top Tech Interviews

Accenture

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.