Back to Practice
#0200
Pairwise L1 Distance
MediumNumPy25 min20 XP
Problem
Return a matrix where result[i, j] is the Manhattan distance between A[i] and B[j].
Why This Matters
Pairwise distances power nearest neighbors, clustering, retrieval, and recommendation prototypes.
Function Signature
def pairwise_l1(A, B):
Examples
Example 1
InputA = [[1,2],[3,4]], B = [[1,1],[5,4]]
Output[[1, 6], [5, 2]]
Distance from [1,2] to [1,1] is 1; from [1,2] to [5,4] is 6.
Constraints
- Return the exact requested result.
- Handle common edge cases.
- Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
InputA = [[1,2],[3,4]], B = [[1,1],[5,4]]
Expected[[1, 6], [5, 2]]
Distance from [1,2] to [1,1] is 1; from [1,2] to [5,4] is 6.
Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input