Loading…
Cosine Similarity is a easy coding interview question covering math, vector search, machine learning — the kind asked at FAANG (Facebook/Meta, Apple, Amazon, Netflix, Google) and AI-first companies like Anthropic, OpenAI, and Nvidia.
Implement cosine similarity between two vectors. Cosine similarity is widely used in NLP to compare word embeddings, sentence embeddings, and document vectors.
cosine_similarity(a, b) = dot(a, b) / (||a||_2 * ||b||_2)
Returns a value in [-1, 1] where 1 = same direction, 0 = orthogonal, -1 = opposite.
Example: [1,0,1] and [1,1,0] -> 0.5
Return 0.0 if either vector is a zero vector.
Solution walkthrough on Pro
Upgrade to Pro to unlock the guided hints, the step-by-step walkthrough, solutions in Python, C++, Java, and Go, and complexity analysis.
What's inside this guide
Longer written walkthroughs of the ideas behind it.
Fully unlocked, no account needed — solutions, walkthroughs and follow-up answers included.
Or start the Study Plan — a week-by-week order for your loop, and week 1 is entirely free.
A full interview loop is more than one round. These are the others, each with what is on the site for it.