0
0
Prompt Engineering / GenAIml~20 mins

Vector similarity metrics in Prompt Engineering / GenAI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vector Similarity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Cosine Similarity

Which statement best describes what cosine similarity measures between two vectors?

AThe angle between the two vectors, indicating how similar their directions are regardless of length.
BThe difference in length between the two vectors, ignoring their direction.
CThe sum of the element-wise differences between the two vectors.
DThe Euclidean distance between the two vectors.
Attempts:
2 left
💡 Hint

Think about how cosine similarity relates to the angle formed by vectors.

Predict Output
intermediate
2:00remaining
Output of Euclidean Distance Calculation

What is the output of the following Python code that calculates Euclidean distance between two vectors?

Prompt Engineering / GenAI
import numpy as np
v1 = np.array([1, 2, 3])
v2 = np.array([4, 6, 3])
distance = np.linalg.norm(v1 - v2)
print(round(distance, 2))
A4.47
B5.0
C7.0
D3.0
Attempts:
2 left
💡 Hint

Calculate the square root of the sum of squared differences.

Metrics
advanced
2:00remaining
Choosing the Best Similarity Metric for Sparse Vectors

You have very sparse high-dimensional vectors representing text documents. Which similarity metric is generally best suited to compare these vectors?

AJaccard similarity, because it measures overlap of non-zero elements.
BEuclidean distance, because it measures absolute distance in space.
CManhattan distance, because it sums absolute differences.
DCosine similarity, because it focuses on the angle and ignores magnitude differences.
Attempts:
2 left
💡 Hint

Think about which metric handles sparse data and presence/absence well.

🔧 Debug
advanced
2:00remaining
Debugging Incorrect Cosine Similarity Calculation

What error does this code raise when calculating cosine similarity between two vectors?

Prompt Engineering / GenAI
import numpy as np
v1 = np.array([1, 0, 0])
v2 = np.array([0, 0, 0])
cos_sim = np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2))
print(cos_sim)
ATypeError because np.dot cannot multiply arrays.
BZeroDivisionError because one vector has zero length.
CValueError because vectors have different shapes.
DNo error, output is nan.
Attempts:
2 left
💡 Hint

Check what happens when a vector has zero length in the denominator.

Model Choice
expert
2:00remaining
Selecting a Similarity Metric for Image Feature Vectors

You have feature vectors extracted from images using a deep neural network. These vectors are dense and normalized to unit length. Which similarity metric is most appropriate to compare these vectors for image retrieval?

AJaccard similarity, because it measures overlap of features.
BEuclidean distance, because it measures absolute difference in features.
CCosine similarity, because vectors are normalized and direction matters most.
DManhattan distance, because it sums absolute differences.
Attempts:
2 left
💡 Hint

Consider the effect of normalization on distance metrics.