Skip to main content
All Questions

Build a Mention Autocomplete Engine

Hard
Unlock detailed company stats for this questionUpgrade

You're building the mention engine behind a team chat app's compose box. When the user types @, a suggestion list opens and filters as they keep typing. The arrow keys move the highlight, Enter or Tab inserts the highlighted user as a mention, and Escape closes the list. User lookups are asynchronous and debounced.

You build the engine only, in Python. There's no UI or DOM: the engine takes text changes and key presses and exposes its state, so every behavior can be tested directly.

Starter code and solution
Python 3.11+ (standard library)
Download code

What's in the codebase

  • mention_engine.py. Where you implement MentionEngine. Its docstring holds the full contract. This is the only file you change.
  • user_service.py. An asynchronous directory of 12 users. search_users(query) returns at most 5 users whose names contain the query as a case-insensitive substring. An empty query returns the first 5.
  • test_runner.py. Nine tests covering triggers, insertion, dismissal, navigation, and debouncing.
  • demo.py. Plays through a typing session so you can watch the engine's state change.
  • instructions.md. The run commands.

The tests fail until you implement the engine.

Your task

Implement MentionEngine(user_service, debounce_ms=150) with two methods, handle_text_change(text, cursor) and handle_key(key). The engine exposes text, cursor, active, query, suggestions, and selected_index. key is one of "ArrowUp", "ArrowDown", "Enter", "Tab", and "Escape".

The engine follows these rules:

  • A session starts when @ is at the start of the text or right after whitespace, and the cursor is in the query that follows it. An @ inside a word, as in an email address, doesn't start one.
  • The query is the run of letters, digits, and underscores between the @ and the cursor. It can be empty, in which case the list shows the first 5 users.
  • Enter or Tab replaces the @ and the query with @Name followed by one space, and moves the cursor to just after it. If the character after the cursor is already a space, it's consumed, so the text never gets two spaces in a row.
  • Typing a space or punctuation after the query ends the session, and so does Escape.
  • Lookups run after a 150 ms debounce, so a burst of keystrokes produces one lookup, for the final query. A result is applied only if its query is still current and the session is still active.
  • ArrowUp and ArrowDown move selected_index by one and wrap at both ends. They apply only when there are suggestions.
  • handle_key returns True only when an active session consumed the key.
"Hello @al", cursor 9 -> active, suggestions include Alice Enter -> "Hello @Alice ", cursor 13, inactive "Hi @al bye", cursor 6 -> Enter -> "Hi @Alice bye", cursor 10 "@a", "@al", "@ali" -> typed with no pause: one lookup, for "ali" "email@al", cursor 8 -> inactive

handle_text_change schedules its lookup with asyncio.create_task, so call it from async code. You're done when all nine tests pass.

Follow-up

Once the engine works, the interviewer adds three requirements:

  • A mention is an atomic rich-text chip, not plain text. One press of Backspace deletes the whole token.
  • Screen reader users need the suggestion list announced as it changes, through an ARIA live region.
  • The directory has 10,000 users, not 12. The lookup, the filtering, and the rendering must stay fast at that size.

Describe how the design changes for each: where the cursor can rest relative to a chip, which changes the live region announces, and what breaks first at 10,000 users.

Using AI on this problem

This problem is usually run at the limited-context level. Use AI to check a regex for capturing @ tokens against specific inputs, or to compare your debounce with standard asyncio patterns once you've designed it. Don't have it generate the engine. The state transitions and the cursor math are what the interviewer is grading, and they're easy to get subtly wrong in generated code.

Related questions

Design autocomplete.

Related courses

Course

AI Engineering Interview Prep

Ace the AI engineering interview at top companies like Google, Anthropic, Meta, and OpenAI. Learn from detailed frameworks, example questions, and mock interviews with senior engineers. Practice system design, AI/ML concepts, AI-assisted coding, and behavioral rounds.

Course

Generative AI Interviews

Ace your interviews at AI-first and AI-powered companies. Learn to explain how Gen AI really works, where it fails, and how to use it responsibly. Understand how top tech companies build and deploy AI products and how generative AI works in production, from LLMs and RAG systems to real-world deployment.