← Questions

Group prompts into templates under concurrent ingestion

0
AnthropicCoding

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. gpt4 and v2 are not numbers.
  • TemplateIndex counts prompts per template while many threads call add at once, keeps the first 3 example prompts per template, and answers count(template), examples(template) and top(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_of as a fixed sequence of regex substitutions: quoted strings, then URLs, then emails, then UUIDs, then whole-token numbers (with word boundaries so gpt4 and v2 stay), 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) in O(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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding