Difficulty: Medium Tags: Embeddings, Retrieval, Similarity Search
A retrieval service ranks document embeddings against a query. One code path uses raw dot product while another uses cosine similarity, causing inconsistent ranking when vector norms differ. Compute cosine similarity for each document vector.
For query q and document vector d_i:
s_i = (q . d_i) / (||q||_2 * ||d_i||_2)
If either vector has zero norm, define its similarity as 0.
query (length d), documents (n x d).
n cosine similarities, 6 decimal places.
1 <= n <= 10^4, 1 <= d <= 1000, component magnitudes at most 10^6.Input
query = [1, 0], documents = [[3, 0], [1, 1]]
Output
1.000000 0.707107
Explanation: The first document points in the same direction as the query (similarity 1). The second forms a 45-degree angle with it.
No results yet
Run your code to check it against the examples, or submit to run every test.