Data Structures & AlgorithmsHardProduct Company SDE · Amazon/Microsoft-style OA

Problem: Given n ≤ 10⁵ words, find the longest chain where each word is the previous word with exactly one letter inserted (word length ≤ 16). What is the intended complexity?

  1. A. O(n²) – compare every pair of words
  2. B. O(n log n) – LIS by binary search on lengths
  3. C. O(n · L²) – sort by length, and for each word try deleting each letter and look up the predecessor in a hash map (DP)
  4. D. O(2^L · n) – generate all subsequences

Correct answer: C. O(n · L²) – sort by length, and for each word try deleting each letter and look up the predecessor in a hash map (DP)

Each word has only L predecessors (one per deleted letter), each built in O(L) and checked in O(1) average: O(n·L²) ≈ 2.6 × 10⁷. Pairwise comparison is 10¹⁰. Plain LIS on lengths ignores the one-letter relation.

More Coding Interview Problems questions

Exams that ask this topic