AlgoMaster Logo
AlgoMasterMerge HyperLogLog Sketchesmedium

Merge HyperLogLog Sketches

medium

HyperLogLog sketches can be combined without replaying their original streams. This makes it possible to maintain one sketch per shard, region, or time window and later obtain a sketch for the union.

Design a HyperLogLogMerger class:

  • HyperLogLogMerger() creates a stateless merger.
  • int[] merge(int[] a, int[] b) returns a new sketch containing the union of a and b.

The arrays have the same length and therefore the same precision. Register i stores the largest rank observed for hash bucket i. The union's value at that position is max(a[i], b[i]).

Do not mutate either input. Each call is independent.

Example 1:

Input:

Output:

Explanation: Register 2 takes rank 5 from b, register 4 keeps rank 3 from a, and every other position follows the same maximum rule.

Example 2:

Input:

Output:

Explanation: The first sketch supplies positions 0 and 2; the second supplies position 1.

Constraints

  • 0 <= a.length == b.length <= 10^5
  • 0 <= a[i], b[i] <= 64
  • Both arrays were produced with the same HyperLogLog precision and hash function.
  • At most 100 calls are made to merge.
Hints

Loading...
CallReturns
new HyperLogLogMerger()null
merge([0,2,0,1,3,0,0,4], [1,0,5,1,0,2,0,3])[1,2,5,1,3,2,0,4]

Each position keeps the larger rank observed by either sketch.

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