Longest Chain of Search Queries Where Each Adds Exactly One Letter

Last updated: November 29, 2026

Quick Overview

Given a set of search queries, a chain is a sequence where each query is formed by inserting exactly one character anywhere into the previous one. Return the length of the longest chain.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a set of search queries, a chain is a sequence where each query is formed by inserting exactly one character anywhere into the previous one. Return the length of the longest chain.

This Google onsite favorite tests DP over a hash map with a length-based ordering, the same shape as a DAG longest path. Interviewers check that you generate predecessors by deletion rather than comparing every pair.

What the Interviewer Expects
  • Sort queries by length so predecessors come first
  • Define chain[w] as the longest chain ending at w
  • Find predecessors by deleting each character
  • Explain the O(n L^2) cost and why it beats pairwise comparison
  • Handle duplicate queries
Key Topics to Cover
1D dynamic programming
Hash map DP
DAG longest path
String predecessors
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
  • Return the chain itself.
  • Allow insertion or substitution of one letter.
  • Queries are up to 1000 characters long. What becomes the bottleneck?
  • Count how many longest chains exist.
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: list words of lowercase strings. Word a is a predecessor of b if inserting one character into a gives b. Output the maximum length of a chain w1, w2, ... where each is a predecessor of the ne...

High-Level Design

Process words by increasing length. For each word w, every predecessor has length len(w) - 1 and must equal w with one character deleted. Generate those L deletions and look them up in a dictionary ...


Submit Your Answer
Markdown supported