Quick Overview

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.

Implement Cosine Similarity Function for String Vectors

Company: Shopify

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

##### 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.

Quick Answer: 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.

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.

Examples

Input: ("the cat sat", "the cat sat")

Expected Output: 1.0

Explanation: Identical bag-of-words vectors point in the same direction, so cosine similarity is exactly 1.0.

Input: ("the cat", "the dog")

Expected Output: 0.5

Explanation: Both vectors are [1,1] over their union {the,cat,dog}; they share only "the". dot=1, magnitudes=sqrt(2) each, so 1/2 = 0.5.

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.

Loading coding console...