Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs

Back to Questions

116. Vector normalization

easy
GeneralGeneral
senior

Vector normalization makes embedding-based retrieval more reliable by ensuring similarity depends on direction, not magnitude. In this task, you’ll normalize a batch of embedding vectors so you can safely use cosine similarity later.

x^=x∥x∥2,where ∥x∥2=∑i=1dxi2\hat{x} = \frac{x}{\|x\|_2}, \quad \text{where } \|x\|_2 = \sqrt{\sum_{i=1}^{d} x_i^2}

Requirements

Implement the function

python

Rules:

  • Normalize each vector independently (each row gets its own L2 norm).
  • Return a NumPy array.
  • Do not use any prebuilt normalization utilities (e.g., sklearn.preprocessing.normalize).
  • Keep the output in the same order as the input.
  • Handle the zero-vector case safely (e.g., avoid division by zero).

Example

python

Output:

python
Input Signature
ArgumentType
embeddingsnp.ndarray
Output Signature
Return NameType
valuenp.ndarray

Constraints

  • Return NumPy array.

  • Compute row-wise L2 norms (axis=1).

  • No sklearn/normalization utilities allowed.

Hint 1

Compute a per-row L2 norm: norms = np.sqrt(np.sum(embeddings**2, axis=1, keepdims=True)).

Hint 2

Normalize with broadcasting: embeddings / norms. Use keepdims=True in summation to ensure shapes match.

Hint 3

Handle division by zero (if any norm is 0) by setting that norm to 1 or using np.maximum.

Roles
ML Engineer
AI Engineer
Companies
GeneralGeneral
Levels
senior
entry
Tags
numpy
l2-normalization
cosine-similarity
vectorization
13 people are solving this problem
Python LogoPython Editor
Ln 1, Col 1

Input Arguments

Edit values below to test with custom inputs

You need tolog in/sign upto run or submit