Bird
Raised Fist0

Implement Cosine Similarity and Euclidean Distance

CODINGbeginnerInterview freq: very_high
Choose your preparation mode6 modes available
🤖
Implement Cosine Similarity and Euclidean Distance
Compute similarity and distance metrics from scratch using NumPy.
codingbeginnerFreq: very_highEvaluation Coding
Prerequisites:EmbeddingsK-Nearest Neighbors
📝
Problem Statement

Implement functions to compute Cosine Similarity and Euclidean Distance between two vectors.

Input:  Two numpy arrays, A and B, of the same length.
Output: Two floats: cosine similarity and euclidean distance.
{
  "input": "A = np.array([1, 2, 3]), B = np.array([4, 5, 6])",
  "expected_output": "Cosine Similarity: 0.9746318461970762, Euclidean Distance: 5.196152422706632"
}
🎯
Interview Angles
  • Testing understanding of vector operations and their applications in ML.
  • Evaluating ability to implement mathematical formulas using NumPy.
  • Assessing knowledge of vector norms and their role in similarity/distance metrics.
🔵
Naive Approach

Compute the cosine similarity and Euclidean distance using direct mathematical formulas.

Time: O(n) for both functions - each involves a single pass over the vectors for dot product and norm calculations., Space: O(1) - only a fixed amount of extra space is used for calculations.
Why Insufficient
O(n) is acceptable for small vectors, but the naive approach does not leverage any optimizations for batch processing or large datasets.
🐍
Naive Code
Python
import numpy as np

def cosine_similarity(A, B):
    dot_product = np.dot(A, B)
    norm_A = np.linalg.norm(A)
    norm_B = np.linalg.norm(B)
    return dot_product / (norm_A * norm_B)

def euclidean_distance(A, B):
    return np.sqrt(np.sum((A - B) ** 2))

# Driver example
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
print('Cosine Similarity:', cosine_similarity(A, B))
print('Euclidean Distance:', euclidean_distance(A, B))
💡
Key Insight
Utilize NumPy's vectorized operations to handle batch computations efficiently, reducing overhead from Python loops.
🐍
Optimized Code
Python
import numpy as np

def cosine_similarity_optimized(A, B):
    dot_product = np.dot(A, B)
    norm_A = np.linalg.norm(A)
    norm_B = np.linalg.norm(B)
    if norm_A == 0 or norm_B == 0:
        return 0.0  # Handle zero vector case
    return dot_product / (norm_A * norm_B)

def euclidean_distance_optimized(A, B):
    return np.linalg.norm(A - B)

# Driver example
A = np.array([1, 2, 3])
B = np.array([4, 5, 6])
print('Cosine Similarity:', cosine_similarity_optimized(A, B))
print('Euclidean Distance:', euclidean_distance_optimized(A, B))
🔍
Line-by-Line
dot_product = np.dot(A, B)
Computes the dot product of vectors A and B, a key component of cosine similarity.
norm_A = np.linalg.norm(A)
Calculates the magnitude of vector A, necessary for normalization.
norm_B = np.linalg.norm(B)
Calculates the magnitude of vector B, necessary for normalization.
return dot_product / (norm_A * norm_B)
Returns the cosine similarity by dividing the dot product by the product of the magnitudes.
return np.linalg.norm(A - B)
Computes the Euclidean distance using the L2 norm of the difference vector.
⚡
Complexity
Time: O(n) for both functions - each involves a single pass over the vectors for dot product and norm calculations., Space: O(1) - only a fixed amount of extra space is used for calculations.
Cosine Similarity: 0.9746318461970762, Euclidean Distance: 5.196152422706632
🔲
Edge Cases
Zero Vectors
Cosine similarity is undefined; Euclidean distance is zero.
if norm_A == 0 or norm_B == 0: return 0.0 # Handle zero vector case
Single Element Vectors
Both metrics are computed correctly as scalars.
np.dot(A, B) # Handles single element vectors correctly.
⚠️
Common Mistakes
Symptom: Results in values outside the range [-1, 1].
Symptom: Inefficient code that runs slowly on large datasets.
Symptom: Division by zero errors or undefined results.

Practice

(1/5)
1. What is the time complexity of a single iteration of gradient descent for linear regression with n samples and d features?
easy
A. O(n)
B. O(d)
C. O(nd)
D. O(n^2)

Solution

  1. Step 1: Identify operations in a single iteration

    Each iteration involves matrix multiplication and gradient computation.
  2. Step 2: Calculate complexity

    Matrix multiplication and gradient computation both require O(nd) operations.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Matrix operations scale with samples and features. [OK]
Hint: Matrix operations scale with samples and features. [OK]
Common Mistakes:
2. What is the time complexity of calculating the mean Average Precision (mAP) for a single image with N predicted bounding boxes and M ground truth boxes?
easy
A. O(N + M)
B. O(N^2)
C. O(N * M)
D. O(M^2)

Solution

  1. Step 1: Understand the Process

    For each predicted box, IoU is calculated with each ground truth box, leading to N * M comparisons.
  2. Step 2: Determine Complexity

    Each comparison is constant time, so the overall complexity is O(N * M).
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    IoU comparisons drive complexity [OK]
Hint: IoU comparisons drive complexity [OK]
Common Mistakes:
3. You implemented stratified K-Fold cross-validation but notice class imbalance in some folds. What is the most likely cause?
medium
A. Incorrect stratification parameter
B. Data shuffling before splitting
C. Incorrect number of splits
D. Too many folds for the dataset size

Solution

  1. Step 1: Identify stratification issue

    Stratification ensures class distribution is preserved in each fold.
  2. Step 2: Eliminate wrong options

    Incorrect number of splits and D relate to fold count, not class distribution. Data shuffling before splitting affects randomness, not stratification.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Check stratification parameter for class balance. [OK]
Hint: Check stratification parameter for class balance. [OK]
Common Mistakes:
4. You implemented gradient checking but the numerical and analytical gradients differ significantly. What is the most likely cause?
medium
A. The model is overfitting.
B. The step size for numerical gradient is too large.
C. The analytical gradient is incorrect.
D. The learning rate is too high.

Solution

  1. Step 1: Identify potential causes for gradient mismatch

    Significant differences often indicate errors in the analytical gradient.
  2. Step 2: Eliminate other options

    The step size for numerical gradient is too large. would cause small numerical errors, not significant differences. The learning rate is too high. and C are unrelated to gradient checking.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Mismatch often means analytical error. [OK]
Hint: Mismatch often means analytical error. [OK]
Common Mistakes:
5. In a text classification task using Multinomial Naive Bayes, which preprocessing step is crucial for improving model performance?
medium
A. Normalizing feature values
B. Scaling features to [0, 1]
C. Removing stop words
D. Applying PCA for dimensionality reduction

Solution

  1. Step 1: Understand text preprocessing needs

    In text classification, irrelevant words can add noise.
  2. Step 2: Evaluate preprocessing steps

    Removing stop words (Removing stop words) reduces noise and improves performance. Options A and C are less relevant as MultinomialNB doesn't require normalized or scaled features. Applying PCA for dimensionality reduction is unnecessary as PCA isn't typically used for text data.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Remove stop words to reduce noise in text data. [OK]
Hint: Remove stop words to reduce noise in text data. [OK]
Common Mistakes:
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard