Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement batched query retrieval to find the top‑(k) most similar items (by cosine similarity) in a small embedding index for each query. You’ll take queries and documents as NumPy arrays and return, for every query, the indices of the best matches.
The cosine similarity between vectors (q) and (d) is:
Implement the function
Rules:
Output:
| Argument | Type |
|---|---|
| k | int |
| docs | np.ndarray |
| queries | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Use NumPy; avoid nested query×doc loops.
Stable tie-break: smaller index first.
Return indices only; length k per query.
Compute all dot products at once with Q @ D.T.
Cosine similarity needs norms: compute q_norms = norm(Q, axis=1) and d_norms = norm(D, axis=1), then divide by q_norms[:,None] * d_norms[None,:].
For top‑k with tie-breaking by smaller index, use a stable sort: np.argsort(-sims, axis=1, kind='mergesort'), then take [:, :k].