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?
- A. O(n²) – compare every pair of words
- B. O(n log n) – LIS by binary search on lengths
- 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)
- 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
- Problem: A string of length n = 10⁵; answer q = 10⁵ queries 'are substrings [l1, r1] and [l2, r2] equal?'. Which technique fits?
- Problem: n ≤ 40 items with weights up to 10⁹; count subsets with total weight ≤ W. Why is meet-in-the-middle preferred here?
- Problem: For each element of an array of n = 10⁵, count how many elements to its right are smaller. What is the best complexity?
- Problem: A grid of R × C = 10³ × 10³ heights; find the minimum possible 'maximum step height' along any path from top-left to…
- Problem: Count distinct values in every window of size k over n = 10⁶ values. What is the optimal complexity?
- Problem: n = 10⁵ cities connected by roads that are added one at a time; after each addition report the number of connected components…