Your question is Memory-Efficient Code Optimization. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Intercom's support team uses ticket-embedding similarity to surface related past tickets while an agent is typing a reply. Below is the function an ML engineer wrote to find the most similar tickets to a given one.
import numpy as np
def find_similar_tickets(ticket_id, all_tickets):
embeddings = []
ids = []
for t in all_tickets:
embeddings.append(np.array(t["embedding"], dtype=np.float64))
ids.append(t["id"])
target_index = ids.index(ticket_id)
target = embeddings[target_index]
similarities = []
for i in range(len(embeddings)):
sim = np.dot(target, embeddings[i]) / (np.linalg.norm(target) * np.linalg.norm(embeddings[i]))
similarities.append((ids[i], sim))
similarities.sort(key=lambda x: x[1], reverse=True)
return similarities[:10]
This is called on every keystroke in the agent console against a corpus of millions of past tickets. Optimize this code for memory efficiency, explaining what you'd change and why.