Random-Hyperplane LSH Index With Exact Re-Ranking

Quick Overview

Asks for a random-hyperplane locality-sensitive hashing index with several hash tables, candidate retrieval from matching buckets and exact Euclidean re-ranking, plus brute-force kNN and recall@k. Tests vectorized bit packing, deterministic tie-breaking and how tables and bits trade recall against query cost.

Random-Hyperplane LSH Index With Exact Re-Ranking

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `LSHIndex` with `n_tables` hash tables of `n_bits` random hyperplanes each. Retrieve candidates from matching buckets and re-rank them by exact Euclidean distance. Also implement `brute_force_knn` and `recall_at_k`. The constructor is `LSHIndex(n_tables, n_bits, seed=0, planes=None)`. Requirements: - `fit(X)`: store `X_` and `planes_` with shape `(L, b, d)`, where L is `n_tables` and b is `n_bits`. Use `planes` if given, otherwise `np.random.default_rng(seed).standard_normal((L, b, d))`. Return `self`. - `hash_codes(Y)`: return an `(n, L)` int64 array. Bit `j` of table `t` is 1 if and only if `planes_[t, j] @ y >= 0`, and the code is $\sum_j \text{bit}_j \cdot 2^j$. - `candidates(q)`: return the union of matching buckets across all tables as a sorted int64 array. - `query(q, k)`: return `(indices, distances)` for the top-`k` candidates, sorted by distance with ties broken by lower index. Return fewer than `k` results if there are fewer candidates, and empty arrays if there are none. - `brute_force_knn(X, Q, k)` returns `(idx, dist)`, each of shape `(m, k)`, with ties broken by lower index. - `recall_at_k(index, Q, k)` returns the mean over queries of $|\text{approx} \cap \text{exact}| / k$ as a `float`. Example: ```python X = [[1,1], [-1,2], [2,-1], [-1,-1], [3,2]] planes = [[[1,0],[0,1]], [[1,1],[1,-1]]] idx = LSHIndex(2, 2, planes=planes).fit(X) idx.hash_codes(X) # [[3,3],[2,1],[1,3],[0,2],[3,3]] idx.candidates([2, 0.5]) # [0, 2, 4] idx.query([2, 0.5], 2) # ([0, 2], [sqrt(1.25), 1.5]) ``` ```hint Hash everything at once All points, tables and planes can be projected in one tensor operation. After that, each table needs one integer per point built from its b sign bits. ``` ```hint Boundary cases Check the example codes for rows 0 and 3 in the second table. One of their projections is exactly 0, and the comparison you choose decides their bucket. ``` ```hint Re-ranking order Two candidates can sit at the same distance. Pick a sort that orders by distance and then by index, so `query` and `brute_force_knn` agree on ties. ``` ### Constraints & Assumptions - `1 <= n <= 1000`, `1 <= d <= 64`, `1 <= n_bits <= 16`. - All inputs are finite floats, and `k <= n` for `brute_force_knn`. ### What a Strong Answer Covers - Plane setup from given planes or a seeded generator, with the expected `(L, b, d)` shape - Vectorized projection and bit packing that reproduce the example codes, with a projection of exactly 0 counted as bit 1 - Bucket tables built once at fit time, and a candidate union returned sorted - Exact re-ranking with ties by lower index, short results when candidates are few and empty results when there are none - How `n_tables` and `n_bits` trade recall against candidate count, and what a query costs next to brute force ### Follow-up Questions - How would you raise recall without adding more tables? - Random hyperplanes hash by angle. Why is that a mismatch for Euclidean distance, and which hash family would you use instead? - How would you choose `n_bits` and `n_tables` for a target recall?

Overview: Asks for a random-hyperplane locality-sensitive hashing index with several hash tables, candidate retrieval from matching buckets and exact Euclidean re-ranking, plus brute-force kNN and recall@k. Tests vectorized bit packing, deterministic tie-breaking and how tables and bits trade recall against query cost.

PracHub logo
General
Nov 24, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement LSHIndex with n_tables hash tables of n_bits random hyperplanes each. Retrieve candidates from matching buckets and re-rank them by exact Euclidean distance. Also implement brute_force_knn and recall_at_k. The constructor is LSHIndex(n_tables, n_bits, seed=0, planes=None).

Requirements:

  • fit(X) : store X_ and planes_ with shape (L, b, d) , where L is n_tables and b is n_bits . Use planes if given, otherwise np.random.default_rng(seed).standard_normal((L, b, d)) . Return self .
  • hash_codes(Y) : return an (n, L) int64 array. Bit j of table t is 1 if and only if planes_[t, j] @ y >= 0 , and the code is ∑jbitj⋅2j\sum_j \text{bit}_j \cdot 2^j .
  • candidates(q) : return the union of matching buckets across all tables as a sorted int64 array.
  • query(q, k) : return (indices, distances) for the top- k candidates, sorted by distance with ties broken by lower index. Return fewer than k results if there are fewer candidates, and empty arrays if there are none.
  • brute_force_knn(X, Q, k) returns (idx, dist) , each of shape (m, k) , with ties broken by lower index.
  • recall_at_k(index, Q, k) returns the mean over queries of ∣approx∩exact∣/k|\text{approx} \cap \text{exact}| / k as a float .

Example:

X = [[1,1], [-1,2], [2,-1], [-1,-1], [3,2]]
planes = [[[1,0],[0,1]], [[1,1],[1,-1]]]
idx = LSHIndex(2, 2, planes=planes).fit(X)
idx.hash_codes(X)          # [[3,3],[2,1],[1,3],[0,2],[3,3]]
idx.candidates([2, 0.5])   # [0, 2, 4]
idx.query([2, 0.5], 2)     # ([0, 2], [sqrt(1.25), 1.5])

Constraints & Assumptions

  • 1 <= n <= 1000 , 1 <= d <= 64 , 1 <= n_bits <= 16 .
  • All inputs are finite floats, and k <= n for brute_force_knn .

What a Strong Answer Covers Guidance

  • Plane setup from given planes or a seeded generator, with the expected (L, b, d) shape
  • Vectorized projection and bit packing that reproduce the example codes, with a projection of exactly 0 counted as bit 1
  • Bucket tables built once at fit time, and a candidate union returned sorted
  • Exact re-ranking with ties by lower index, short results when candidates are few and empty results when there are none
  • How n_tables and n_bits trade recall against candidate count, and what a query costs next to brute force

Follow-up Questions Guidance

  • How would you raise recall without adding more tables?
  • Random hyperplanes hash by angle. Why is that a mismatch for Euclidean distance, and which hash family would you use instead?
  • How would you choose n_bits and n_tables for a target recall?