Euclidean (L2) & Manhattan (L1) Distance Between Two Vectors

Basics
BasicsDistance MetricsKNNGoogleMetaAmazon

Problem

Given two 1D numeric vectors a and b, implement l2_and_l1_distance(a, b) returning (euclidean_distance, manhattan_distance).

Why It Matters: L2 (Euclidean) and L1 (Manhattan) distances power KNN, K-Means clustering, vector search, and regularization norms.

Sample Inputs & Expected Outputs

Example 1 — 3-4-5 right triangle

>>> l2_and_l1_distance([0, 0], [3, 4])
(5.0, 7.0)

L2 = sqrt(3² + 4²) = 5.0; L1 = |3| + |4| = 7.0.

Example 2 — Identical vectors

>>> l2_and_l1_distance([2, -1], [2, -1])
(0.0, 0.0)

Distance from a point to itself is 0.0.

Example 3 — 4D vectors

>>> l2_and_l1_distance([1, 2, 3, 4], [2, 4, 5, 6])
(3.6056, 7.0)

Diff = [-1, -2, -2, -2] -> L2 = sqrt(13) ≈ 3.6056, L1 = 7.0.

Constraints & Edge Cases

  • Return (euclidean, manhattan) as Python floats.

Constraints & Evaluation Criteria

  • Return `(euclidean, manhattan)` as Python floats.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[0,0] vs [3,4]
Expected Output / Criteria
(5.0, 7.0)