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): Mergedeltainto the running metric already stored for componentcomponentId. If the component has not reported yet, store its metric asdelta. Each report is cumulative; later entries for the same component add to the earlier total.topContributors(k): Return the sum of theklargest stored running metrics across all components. If fewer thankcomponents currently have a stored metric, sum the metrics that do exist. When no component has a stored metric, return0.prune(componentId): Remove the stored running metric forcomponentIdentirely, so it no longer counts toward rankings. Pruning a component that has no stored record is a no-op. A lateringestfor 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.
Design a Service Metrics Ranking API
Design a ranking service for a metrics platform. Independent microservices report