Katkat

Compute Sentence Similarity

ByteDanceMachine Learning Engineer · Technical Screen

Last updated

This question evaluates understanding of sentence-level and token-level embedding techniques, text preprocessing, similarity metrics, edge-case handling for empty or unknown tokens, and trade-offs between pretrained sentence encoders and averaged word embeddings.

Given two text inputs, design and implement a method to compute their semantic similarity.

You may use either of the following approaches:

  1. Encode each sentence into a single embedding using a pretrained sentence encoder, then compute cosine similarity.
  2. Convert each token to a word embedding, average the token embeddings for each sentence, then compute cosine similarity between the two averaged vectors.

Your answer should describe:

  • How the text is preprocessed
  • How embeddings are produced
  • How cosine similarity is computed
  • How to handle empty text or unknown tokens
  • The trade-offs between sentence-level encoders and average word embeddings

If coding is requested, provide clear pseudocode or implementation-level steps.