Code Logo

Daily Sales Rollup with TreeMap

Published at22 Apr 2026
Collections Framework Medium 0 views
Like0

A kiosk records sales notes during the week, but the notes are written as separate small entries throughout the day. That means the same date can appear several times in the raw list. Before the manager sends the report, those entries need to be merged into one total per day.

Each record is a string in the form "date:amount", where the date uses the format YYYY-MM-DD. Your task is to combine amounts that belong to the same date and return the final report as a list of strings in the format "date=total".

The output must be sorted by date from earliest to latest. Because the date strings already use year-month-day order, their natural string order matches calendar order, which makes sorted map behavior especially convenient here.

For example, if the input is ["2026-04-20:5", "2026-04-18:3", "2026-04-20:2"], the result should be ["2026-04-18=3", "2026-04-20=7"]. The two entries from April 20 are added together, and the final list is ordered by date.

This is a medium challenge because the solution does more than a single lookup. You need to parse each record, accumulate totals, and then produce a sorted summary. A TreeMap is a strong fit because it keeps keys ordered automatically while you build the totals.

Example Input & Output

Example 1
Input
entries = ["2026-04-22:2", "2026-04-21:5", "2026-04-22:1", "2026-04-21:3"]
Output
["2026-04-21=8", "2026-04-22=3"]
Explanation

Both dates are merged, then shown in ascending order.

Example 2
Input
entries = ["2026-04-20:5", "2026-04-18:3", "2026-04-20:2"]
Output
["2026-04-18=3", "2026-04-20=7"]
Explanation

The same date is summed, and the final report is sorted by date.

Example 3
Input
entries = ["2026-04-21:4"]
Output
["2026-04-21=4"]
Explanation

A single date becomes a one-line report.

Algorithm Flow

Recommendation Algorithm Flow for Daily Sales Rollup with TreeMap
Recommendation Algorithm Flow for Daily Sales Rollup with TreeMap

Solution Approach

The problem has two connected goals: merge repeated dates and keep the final result sorted. A TreeMap handles both nicely because it stores key-value pairs like a normal map but also keeps the keys in their natural sorted order.

The first step is to scan the input list and parse each date:amount record. Once you have the date string and numeric amount, update the running total for that date inside the map. If the date has appeared before, add to its existing total. If not, start a new total.

That accumulation step usually looks like this:

TreeMap<String, Integer> totals = new TreeMap<>();
for (String entry : entries) {
    String[] parts = entry.split(":");
    String date = parts[0];
    int amount = Integer.parseInt(parts[1]);
    totals.put(date, totals.getOrDefault(date, 0) + amount);
}

After that, the map already holds dates in sorted order. So the final step is just to walk through the entries and build strings like date + "=" + total into an output list.

This design is easier to reason about than collecting everything in an unsorted structure and sorting later. The TreeMap keeps the report organized as the data is being processed.

The runtime is O(n log k), where k is the number of distinct dates, because each map insertion or update in a tree-based structure costs logarithmic time. The main idea to learn is that some collections are valuable not just for storage, but for the order guarantees they provide while the algorithm runs.

Best Answers

java - Approach 1
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

class Solution {
    public static List<String> dailySalesRollup(List<String> entries) {
        TreeMap<String, Integer> totals = new TreeMap<>();
        for (String entry : entries) {
            int colon = entry.indexOf(':');
            String date = entry.substring(0, colon);
            int amount = Integer.parseInt(entry.substring(colon + 1));
            Integer current = totals.get(date);
            totals.put(date, current == null ? amount : current + amount);
        }
        List<String> result = new ArrayList<>();
        for (String date : totals.keySet()) {
            result.add(date + "=" + totals.get(date));
        }
        return result;
    }
}