Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-257

HNSW Layer Count Estimate

Problem

Estimate the number of HNSW layers for n_nodes nodes with level multiplier level_mult. The top layer satisfies P(level >= l) = level_mult ** l, giving 1 + floor(log(n_nodes) / log(1 / level_mult)) layers.

Return 0 for no nodes, and 1 when level_mult is outside (0, 1).

Hint

Use natural logs; the ratio is independent of the log base.

Test cases

case 1
in: [1000,0.25]
expected: 5
case 2
in: [16,0.5]
expected: 5
case 3
in: [4,0.5]
expected: 3
case 4
in: [0,0.5]
expected: 0
case 5
in: [100,1]
expected: 1
Starter codePython
import math
def hnsw_layer_count(n_nodes, level_mult):
    # Your code here
    pass

5 test cases · run your code against them in the editor