cswork / libraryOpen practice workspace

Coding questions · Shopify

Implement Cosine Similarity Function for String Vectors

This question evaluates understanding of vector-space similarity and text representation by asking for a cosine similarity computation over string-derived vectors, testing coding and algorithmic skills relevant to data science and text processing.

Scenario

Technical phone screen in Python; assess ability to implement similarity metric.

Question

Implement a Python function that computes the cosine similarity between two strings (treat each string as a bag-of-words vector).

Hints

Tokenize, count word frequencies, form vectors, dot product divided by magnitudes; handle empty inputs.

Cosine Similarity of String Vectors

Implement a function that computes the cosine similarity between two strings, treating each string as a bag-of-words vector.

Tokenize each string into lowercase alphanumeric words, count word frequencies to form a term-frequency vector, then return the dot product of the two vectors divided by the product of their magnitudes. Normalize by lowercasing and ignoring punctuation so that "Hello, World!" and "hello world" are identical.

If either string contains no words (e.g. empty or punctuation-only), return 0.0. Round the result to 6 decimal places.

Examples:

  • solution("the cat", "the dog") -> 0.5 (vectors share only the word "the")
  • solution("data science", "data science is fun") -> 0.707107
  • solution("", "hello world") -> 0.0

Constraints

  • 0 <= len(a), len(b) <= 10^5
  • Strings may contain letters, digits, punctuation, and whitespace.
  • Tokenization is case-insensitive and considers maximal runs of [a-z0-9] as words.
  • Return a float rounded to 6 decimal places; return 0.0 when either string yields no tokens.

Hints

  1. Tokenize each string into lowercase words (a simple approach: split on non-alphanumeric characters and drop empties), then build a frequency Counter for each.
  2. Cosine similarity = dot(u, v) / (|u| * |v|). The dot product only needs the words common to both vectors; the magnitude is sqrt(sum of squared counts).
  3. Guard the empty/no-token case explicitly to avoid dividing by a zero magnitude, and round the final value to 6 decimal places.