Retrieve Vectors by Cosine Similarity
Reported by candidates from Harvey's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Harvey reported this one in September 2026, and it looks fancier than it is. Retrieve vectors by cosine similarity sounds like ML, but it reduces to scoring every candidate, then picking the top k with a tie-break on index. If you've got an OA invite and 48 hours, that's the whole job. Compute the score, sort or heap, return indices. The traps are floating-point ties and the tie-break rule, not the math. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank on the details, but you probably won't need it once you see the shape.
The problem
Given a nonzero query vector and nonzero candidate vectors of the same dimension, return the indices of the k candidates with greatest cosine similarity to the query. Cosine similarity is dot(a,b) / (norm(a) * norm(b)). Sort by descending similarity and break exact ties by smaller candidate index. Function topKCosineMatches(query: double[], candidates: double[][], k: int) → int[] Examples Example 1 query = [1.0,0.0] candidates = [[1.0,0.0],[1.0,1.0],[-1.0,0.0]] k = 2 return = [0,1] The aligned vector ranks first, followed by the 45-degree vector. Example 2 query = [1.0,1.0] candidates = [[2.0,0.0],[0.0,2.0],[3.0,3.0]] k = 3 return = [2,0,1] Candidates zero and one tie, so the smaller index comes first. Example 3 query = [2.0] candidates = [[5.0],[-4.0]] k = 1 return = [0] Positive collinear vectors have cosine one. Constraints 1 <= candidates.length <= 100000. 1 <= query.length <= 200, and every candidate has that length. All coordinates are finite and have absolute value at most 10^6. The query and every candidate have positive Euclidean norm. Any two mathematically distinct cosine scores differ by more than 10^-9. 1 <= k <= candidates.length.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The query norm is constant across all candidates, so it doesn't change the ranking. Precompute it once, or skip it entirely for ordering. For each candidate, compute dot(query, c) and norm(c), then score = dot / (normQ * normC). That's O(n*d) with n up to 100000 and d up to 200, so about 20 million multiplications. Fine. For selection, either sort all n by (score descending, index ascending), or keep a size-k heap. Sorting is simpler and safe. The pitfall is ties. The constraints say distinct scores differ by more than 1e-9, so exact ties come from identical math, but floating-point can make equal scores differ in the last bits. Compare with an epsilon like 1e-12 and fall back to index. Don't use sqrt-free tricks that break sign. If you blank mid-OA, StealthCoder is the hedge for the comparator.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Retrieve Vectors by Cosine Similarity cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Harvey's OA.
Harvey reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Retrieve Vectors by Cosine Similarity FAQ
What's the trick in the Harvey cosine similarity problem?+
There's no deep trick. Compute cosine for each candidate, sort by score descending and index ascending, return the first k indices. The real work is the comparator. Treat scores within a tiny epsilon as tied so the smaller index wins, matching the examples.
Do I need a heap or is sorting enough?+
Sorting is enough. With 100000 candidates, an O(n log n) sort is trivial next to the O(n*d) scoring. A min-heap of size k gives O(n log k) and is a nice mention, but it's more code and more room for tie-break bugs. Pick sort unless k is tiny.
How do I handle floating-point ties?+
Equal mathematical scores can come out a hair apart after division. The constraints guarantee distinct scores differ by more than 1e-9, so compare with an epsilon around 1e-12. If the difference is below it, treat them as equal and order by smaller index.
Can I skip dividing by the query norm?+
Yes for ranking, since it's the same positive number for every candidate. It doesn't change the order. Keeping it is harmless and matches the formula, so do it if you want the actual cosine values. Never skip the candidate norm, because that varies.
How do I prepare for this in 48 hours?+
Write it once from scratch in your language. Practice a custom comparator with an epsilon tie-break, and test the three given examples, especially the tie in Example 2. Also check negative cosine and k equal to the candidate count. That covers every way this one breaks.