Group prompts into templates under concurrent ingestion
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Anthropic's coding round. It tests hash map, regex and concurrency.
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): ...
Clarifying questions
- In what order are the patterns applied when they overlap, such as a number inside a URL or an email?
- Does a quoted string use single quotes, double quotes, or both, and can quotes be escaped?
- Is a number with a decimal point or a sign (
-3.5) one<NUM>? - Which three examples are kept when many threads add to one template at once: the first by arrival under the lock?
- Must
top(k)be exact at the instant it runs, or is a consistent snapshot per shard enough?
What a strong answer covers
-
template_ofas a fixed sequence of regex substitutions: quoted strings, then URLs, then emails, then UUIDs, then whole-token numbers (with word boundaries sogpt4andv2stay), then whitespace collapsed. - Sharding by
hash(template) % shards, each shard with its own lock and dict of count and examples, so threads that add different templates rarely contend; normalization done outside the lock. - Exact counts because every increment happens under its shard's lock; examples appended only while fewer than three.
-
top(k)gathering each shard under its lock and selecting with a heap on(-count, template)inO(T log k). - Tests: each placeholder type, numbers inside words, a prompt that is only a number, and a stress test where many threads add the same and different prompts and the totals match.
Follow-ups
- Templates differ in small wording; cluster near-identical templates.
- Millions of templates: keep only the heavy hitters in bounded memory (count-min sketch, space-saving).
- Learn which tokens are variables from the data instead of fixed regexes.
- Expose counts over a sliding time window.
Where it's asked
- Anthropic · Coding55 min · Live coding on a realistic, multi-part problem, a different one from the screen · No AI
What it tests
hash map · regex · concurrency · llm
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
