Command Palette

Search for a command to run...

Hectal
Phase 5: Vector Databases
RAG and Vector Search · Exercise 1 of 5Next
Beginner

Top-k search by dot product

vectors is a dictionary from id to a vector (a list of numbers). Write top_k(vectors, query, k) that returns the ids of the k vectors with the largest dot product with query, best first.

Equal scores keep the dictionary's order. If there are fewer than k vectors, return them all.

What Check tests

  1. The best two come back, best first
  2. A clear winner comes first
  3. k larger than the data returns everything
  4. An empty store returns an empty list

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.