Group prompts into templates under concurrent ingestion
An API receives prompts from many threads. Most come from a few templates filled with different values: Summarize ticket 48213 for alice@acme.com and Summarize ticket 1007 for bo@x.io share the template Summarize ticket <NUM> for <EMAIL>.
-
template_of(prompt)replaces quoted strings, URLs, emails, UUIDs and whole-token numbers with<STR>,<URL>,<EMAIL>,<UUID>and<NUM>, and collapses whitespace.gpt4andv2are not numbers. -
TemplateIndexcounts prompts per template while many threads calladdat once, keeps the first 3 example prompts per template, and answerscount(template),examples(template)andtop(k)(most frequent first, ties alphabetical). - Counts must be exact, and threads adding different templates should rarely wait for each other.
def template_of(prompt): ...
class TemplateIndex:
def __init__(self, shards=64, examples_per_template=3): ...
def add(self, prompt): ...
def count(self, template): ...
def examples(self, template): ...
def top(self, k): ...
def total(self): ...
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
