Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Retrieve Top-K Documentation Snippets

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Retrieve Top-K Documentation Snippets. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Intercom Fin can use vector embeddings to retrieve relevant Help Center documentation for a customer's query. Given one query embedding and a collection of documentation snippets with precomputed embeddings, return the IDs of the k most relevant snippets.

Use cosine similarity as the relevance score. Return IDs ordered by decreasing similarity. If multiple snippets have exactly the same score, order them by their original input position. You may assume every embedding is nonzero and all embeddings have the same dimension.

Formal Specification

Implement retrieve_top_k(query_embedding, snippets, k).

  • query_embedding is a list of floats.
  • snippets is a list of objects, each containing an id and an embedding list of floats.
  • k is a positive integer no greater than the number of snippets.
  • Return a list of exactly k snippet IDs.

Constraints

  • 1 <= k <= len(snippets) <= 10^5
  • 1 <= len(query_embedding) <= 512
  • Every embedding has the same dimension as the query embedding
  • Every embedding is nonzero
  • Embedding values are finite floating-point numbers

Function Signature

def retrieve_top_k(query_embedding, snippets, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output