DataLane

16. Cosine similarity

hardProNot started~12 minPythonImplement cosine_similarity()

Asked in screens shaped like: OpenAI, Google

Return cosine similarity of equal-length vectors a and b, rounded to 4 decimals. Assume they are non-zero.

Examples

  • Example 1

    Input: { "a": [1, 2, 3], "b": [4, 5, 6] }
    Output: 0.9746

Constraints

  • Vectors are non-zero and the same length

Python editor

Loading Python engine…

Ctrl/Cmd + Enter submits

Run your code to see stdout.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close