AlgoMaster Logo
AlgoMasterControl Metric Label Cardinalitymedium

Control Metric Label Cardinality

medium

Every distinct combination of metric label values creates a separate time series. A few individually reasonable labels can multiply into an unsafe cardinality.

Design a MetricCardinalityPlanner class:

  • MetricCardinalityPlanner() creates a stateless planner.
  • int seriesCount(int[] cardinalities) returns the product of all label cardinalities.
  • String[] labelsToDrop(String[] labels, int[] cardinalities, int maxSeries) returns the fewest labels whose removal reduces the product to at most maxSeries.

Label values vary independently. To minimize the number of removals, drop larger cardinality factors first. When equal factors compete for selection, choose the alphabetically smaller label first. Return selected labels alphabetically, regardless of selection order.

Example 1:

Input:

Output:

Explanation: Removing route divides series count by 100 and leaves 5 x 3 = 15, below the budget.

Example 2:

Input:

Output:

Explanation: The largest factor, endpoint, leaves 200 series. Removing service next leaves 20. The returned names are alphabetized.

Constraints

  • 1 <= labels.length == cardinalities.length <= 12
  • Label names are unique non-empty lowercase strings.
  • 1 <= cardinalities[i] <= 10^6
  • 1 <= maxSeries <= 2 * 10^9
  • The product of all cardinalities fits in a signed 32-bit integer.
  • Return dropped labels in ascending alphabetical order.
  • At most 100 total method calls are made.
Hints

Loading...
CallReturns
new MetricCardinalityPlanner()null
seriesCount([100,5,3])1500
labelsToDrop(["route","status","region"], [100,5,3], 500)["route"]

The metric creates 100 x 5 x 3 = 1500 series. Dropping route leaves only 15 series.

Run checks these cases. Submit also runs a larger hidden set.