Bookshop Blurb Match
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
a = [1, 2, 0] b = [2, 4, 0]
1
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.
a = [3, 0] b = [0, 5]
0
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.
a = [1, 1] b = [-2, -2]
-1
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- Hint 1
In the first example,
[2, 4, 0]is[1, 2, 0]doubled. Their dot product is 10. What would the dot product be if you tripled the second vector instead? - Hint 2
The dot product grows when either vector gets longer. To measure only direction, divide the length back out.
- Hint 3
Divide the dot product by both lengths. Each length is the square root of the sum of that vector's squared entries.
- Hint 4
One loop: keep three running sums,
dot,a·aandb·b. At the end check for a zero length, then returndot / (√(a·a) · √(b·b)).
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.
How to solve it
The dot product measures how much two vectors point the same way, times how long they are. Dividing by both lengths leaves only the direction.
Trace a = [1, 2, 0], b = [2, 4, 0]:
- At position 0, dot increases by 2, a·a by 1 and b·b by 4.
- At position 1, dot increases by 8, a·a by 4 and b·b by 16.
- At position 2 nothing changes, because both entries are 0.
- The totals are dot = 10, a·a = 5 and b·b = 20. Neither length is zero, so the score is 10 / (√5 · √20) = 10 / 10 = 1.
Check for a zero length before dividing. An all-zero vector would cause a division of 0 by 0, which gives NaN in JavaScript and an error in Python.
function blurbSimilarity(a, b) {
let dot = 0, lenA = 0, lenB = 0;
for (let i = 0; i < a.length; i++) {
dot += a[i] * b[i];
lenA += a[i] * a[i];
lenB += b[i] * b[i];
}
if (lenA === 0 || lenB === 0) return 0; // no direction to compare
return dot / (Math.sqrt(lenA) * Math.sqrt(lenB));
}import math
def blurb_similarity(a, b):
dot = len_a = len_b = 0.0
for x, y in zip(a, b):
dot += x * y
len_a += x * x
len_b += y * y
if len_a == 0 or len_b == 0:
return 0.0 # no direction to compare
return dot / (math.sqrt(len_a) * math.sqrt(len_b))Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.