Bookshop Blurb Match

Easylinear algebraAlso known as Cosine Similarity

A second-hand bookshop wants a "you might also like" shelf. An embedding model has turned every back-cover blurb into a list of numbers. Blurbs about similar things point in similar directions, even if one is long and gushing and the other short and dry.

The shop wants a score that depends only on direction: 1 for the same direction, 0 for unrelated vectors, and -1 for opposite directions. Retrieval systems for language models rank documents with this same score.

Your task

Write blurbSimilarity(a, b). Return the cosine similarity of the two vectors: their dot product divided by the product of their lengths.

  • Dot product: a[0]·b[0] + a[1]·b[1] + …
  • Length of a: √(a[0]² + a[1]² + …)

If either vector is all zeros it has no direction, so return 0.

blurbSimilarity(a: float[], b: float[]) → float Numbers are accepted within 1e-6.

Examples

Example 1
Input
a = [1, 2, 0]
b = [2, 4, 0]
Output
1
Why

The dot product is 1·2 + 2·4 + 0·0 = 10. The lengths are √5 and √20, and √5 · √20 = √100 = 10. The score is 10 / 10 = 1, because the second vector is the first one doubled and so points in the same direction.

Example 2
Input
a = [3, 0]
b = [0, 5]
Output
0
Why

The dot product is 3·0 + 0·5 = 0, so the score is 0. Each book is entirely about a different feature, so they are unrelated.

Example 3
Input
a = [1, 1]
b = [-2, -2]
Output
-1
Why

The dot product is -4. The lengths are √2 and √8, and their product is 4. The score is -4 / 4 = -1, which means the vectors point in exactly opposite directions.

Constraints

  • 1 ≤ a.length = b.length ≤ 1,000
  • -1,000 ≤ a[i], b[i] ≤ 1,000
  • Answers within 0.000001 of the expected value are accepted.
Start with brute force

A natural first version normalises both vectors (divides each by its length) and then takes the dot product. It is correct, but it makes three passes and builds two extra lists.

The tempting wrong shortcut is the raw dot product: longer vectors give bigger dot products, so long, wordy blurbs outrank short ones on the same topic. Keeping three running sums gives the normalised answer in a single pass.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code