Design a Service Metrics Ranking API

Design a ranking service for a metrics platform. Independent microservices report aggregated load deltas to a central aggregator, and engineers want to see which components currently dominate the aggregate so they can tune capacity.

Implement a Solution class that tracks the current running metric for every reporting component and answers top-k aggregate queries over that state.

The class supports three operations:

  • ingest(componentId, delta): Merge delta into the running metric already stored for component componentId. If the component has not reported yet, store its metric as delta. Each report is cumulative; later entries for the same component add to the earlier total.
  • topContributors(k): Return the sum of the k largest stored running metrics across all components. If fewer than k components currently have a stored metric, sum the metrics that do exist. When no component has a stored metric, return 0.
  • prune(componentId): Remove the stored running metric for componentId entirely, so it no longer counts toward rankings. Pruning a component that has no stored record is a no-op. A later ingest for the same component starts a fresh record from scratch.

Keep one map keyed by component id to its current running metric. topContributors then collects all running metrics, sorts them in descending order, and sums the largest k. Prune deletes a key instead of zeroing it, which means a pruned component both disappears from rankings and loses its accumulated total.

Examples
Input: [["Solution","ingest","ingest","ingest","topContributors","topContributors"],[[],[101,50],[202,30],[303,20],[1],[3]]]
Output: [null,null,null,null,50,100]
Hints
Related Problems

Design a Service Metrics Ranking API

Design a ranking service for a metrics platform. Independent microservices report