Implement and Evaluate Pin Similarity in Python

Quick Overview

Practice a DS-focused Python similarity question using Pin representations, cosine similarity, top-k ranking, and explicit edge-case handling. Extend the implementation discussion to vectorized scaling, approximate retrieval, offline ranking metrics, leakage checks, diversity, and online evaluation.

Implement and Evaluate Pin Similarity in Python

Company: Pinterest

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: hard

Interview Round: Technical Screen

You are asked to compare Pin similarity in Python. Begin by clarifying how a Pin is represented and what “similar” should mean. Then assume the interviewer provides one query Pin embedding and a collection of candidate Pin embeddings. Explain how you would return the top `k` candidates by cosine similarity and how you would evaluate whether the similarity is useful. ### Constraints & Assumptions - Each valid embedding is a one-dimensional numeric vector with the same dimension. - Candidate identifiers are unique, and the query itself should be excluded if it appears among the candidates. - Define behavior for zero vectors, missing values, invalid `k`, and tied scores. - Treat this as a DS Python and evaluation question, not a production retrieval-system design exercise. ### Clarifying Questions to Ask - Are Pins represented by dense embeddings, tags, user co-engagement, or another feature set? - Is similarity intended to capture visual, topical, or behavioral relatedness? - How large is the candidate set, and is exact retrieval required? - What labeled or behavioral outcome will define a good neighbor? ### What a Strong Answer Covers - Why the representation and similarity measure must match the product meaning of similarity. - Correct cosine-similarity logic with validation, deterministic ranking, and explicit edge-case behavior. - Computational cost and when vectorization or approximate nearest-neighbor retrieval becomes necessary. - Offline ranking metrics, slice analysis, leakage checks, and an online decision metric. ### Follow-up Questions 1. How would your solution change if each Pin were represented by a set of tags? 2. How would you support millions of candidates with a latency requirement? 3. How would you prevent near-duplicate results from reducing recommendation diversity?

Overview: Practice a DS-focused Python similarity question using Pin representations, cosine similarity, top-k ranking, and explicit edge-case handling. Extend the implementation discussion to vectorized scaling, approximate retrieval, offline ranking metrics, leakage checks, diversity, and online evaluation.

Community answers

Answer by tmm86nn

from collections import defaultdict def similarity(boards, given_pin): sim = defaultdict(int) for board, pins in boards.items(): if given_pin in pins: for pin in pins: if pin != given_pin: sim[pin] += 1 return sorted( sim.items(), key=lambda x: (-x[1], x[0]) )
|Home/Data Manipulation (SQL/Python)/Pinterest
Pinterest logo
Pinterest
May 17, 2026
hardData ScientistTechnical ScreenData Manipulation (SQL/Python)
10
0

You are asked to compare Pin similarity in Python. Begin by clarifying how a Pin is represented and what “similar” should mean. Then assume the interviewer provides one query Pin embedding and a collection of candidate Pin embeddings. Explain how you would return the top k candidates by cosine similarity and how you would evaluate whether the similarity is useful.

Constraints & Assumptions

  • Each valid embedding is a one-dimensional numeric vector with the same dimension.
  • Candidate identifiers are unique, and the query itself should be excluded if it appears among the candidates.
  • Define behavior for zero vectors, missing values, invalid k , and tied scores.
  • Treat this as a DS Python and evaluation question, not a production retrieval-system design exercise.

Clarifying Questions to Ask Guidance

  • Are Pins represented by dense embeddings, tags, user co-engagement, or another feature set?
  • Is similarity intended to capture visual, topical, or behavioral relatedness?
  • How large is the candidate set, and is exact retrieval required?
  • What labeled or behavioral outcome will define a good neighbor?

What a Strong Answer Covers Guidance

  • Why the representation and similarity measure must match the product meaning of similarity.
  • Correct cosine-similarity logic with validation, deterministic ranking, and explicit edge-case behavior.
  • Computational cost and when vectorization or approximate nearest-neighbor retrieval becomes necessary.
  • Offline ranking metrics, slice analysis, leakage checks, and an online decision metric.

Follow-up Questions Guidance

  1. How would your solution change if each Pin were represented by a set of tags?
  2. How would you support millions of candidates with a latency requirement?
  3. How would you prevent near-duplicate results from reducing recommendation diversity?
Loading comments...