Return Top Three Friend Name Suggestions After Each Keystroke

Last updated: November 29, 2026

Quick Overview

Given a list of friend names and the string a user types into the Messenger search box, return after each keystroke the three lexicographically smallest names that start with the typed prefix. If fewer than three match, return all matches; if none match, return an empty list.

Meta
Coding & Algorithms
Frontend Engineer
Meta
November 29, 2026
Frontend Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of friend names and the string a user types into the Messenger search box, return after each keystroke the three lexicographically smallest names that start with the typed prefix. If fewer than three match, return all matches; if none match, return an empty list.

Typeahead is everywhere at Meta (Messenger, Facebook search, tagging people in a post), so a prefix-suggestion warm-up is a natural screen question. The interviewer wants to see you pick a structure that answers each keystroke without rescanning the whole list, and reason about precomputation versus per-query cost.

What the Interviewer Expects
  • Clarify ordering, duplicates, and what happens once the prefix has no match
  • Propose a trie or sorted array with binary search and compare them
  • Precompute the top three at each trie node so a keystroke is O(1) after the step
  • Stop walking once the prefix falls off the trie instead of erroring
  • State build and query complexity in terms of total characters
Key Topics to Cover
trie
prefix search
typeahead
sorting
precomputation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you rank by message recency instead of alphabetical order?
  • How would you support a user deleting a character (backspace) efficiently?
  • What changes if the friend list has 5,000 names and updates live?
  • How would you match on last name as well as first name?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: names (strings, lowercase letters), typed (string).
  • Output: a list with one entry per prefix typed[:1], typed[:2], and so on.
  • Each entry holds up to three matching names in lexico...
High-Level Design

Build a trie over the names, inserting them in sorted order. While inserting, each node on the path appends the name to its top list if that list has fewer than three entries. Because names arri...


Submit Your Answer
Markdown supported