Code RoomCosine similarity
EasyPrep Room Coding #1537

Cosine similarity

CodingML systemsEntry–Mid~15 min

Implement cosine similarity between two equal-length numeric vectors from scratch. Cosine similarity is the dot product of the two vectors divided by the product of their L2 (Euclidean) norms. Both inputs are non-empty lists of numbers of the same length. If either vector has zero magnitude, return 0.0. Round the result to 6 decimal places.

Implement
cosine_similarity(a: list[float], b: list[float]) → float
Examples
in[[1,0,0],[1,0,0]]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 15 min
InputExpectedGot
[[1,0,0],[1,0,0]]1not run yetsample