PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of string manipulation, algorithm design for combinatorial search, dynamic programming/memoization concepts, and the ability to analyze time and space complexity.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Determine string buildability from dictionary

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a non-empty string s and a list of non-empty words dict, determine whether s can be formed by concatenating words from dict with unlimited reuse allowed. If possible, return one valid decomposition as a list of words in order; otherwise, return an empty list. Implement an efficient solution and analyze its time and space complexity, considering very long s and large dictionaries.

Quick Answer: This question evaluates understanding of string manipulation, algorithm design for combinatorial search, dynamic programming/memoization concepts, and the ability to analyze time and space complexity.

Given a non-empty string `s` and a list of non-empty words `words` (a dictionary), determine whether `s` can be formed by concatenating words from `words`, with unlimited reuse of each word allowed. If it is possible, return ONE valid decomposition as a list of words in order (any valid decomposition is accepted). If it is not possible, return an empty list. Aim for an efficient solution that scales to very long `s` and large dictionaries. Examples: - `s = "leetcode"`, `words = ["leet", "code"]` -> `["leet", "code"]` - `s = "applepenapple"`, `words = ["apple", "pen"]` -> `["apple", "pen", "apple"]` - `s = "catsandog"`, `words = ["cats", "dog", "sand", "and", "cat"]` -> `[]` (cannot be segmented) Note: When multiple decompositions exist, returning any one of them is correct.

Constraints

  • 1 <= len(s) <= 10^4
  • 1 <= len(words) <= 10^4
  • 1 <= len(word) <= 100 for each word in words
  • s and all words consist of lowercase English letters
  • Each word may be reused an unlimited number of times
  • If multiple valid decompositions exist, any one is accepted

Examples

Input: ("leetcode", ["leet", "code"])

Expected Output: ["leet", "code"]

Explanation: "leetcode" splits cleanly into "leet" + "code", both in the dictionary.

Input: ("applepenapple", ["apple", "pen"])

Expected Output: ["apple", "pen", "apple"]

Explanation: "apple" is reused; decomposition is apple + pen + apple.

Input: ("catsandog", ["cats", "dog", "sand", "and", "cat"])

Expected Output: []

Explanation: No combination of dictionary words covers the whole string (the trailing 'og' cannot be formed), so an empty list is returned.

Input: ("aaaa", ["a", "aa"])

Expected Output: ["aa", "aa"]

Explanation: Multiple decompositions exist; the solution returns one valid split. "aa" + "aa" covers "aaaa".

Input: ("a", ["a"])

Expected Output: ["a"]

Explanation: Single-character string equal to a single dictionary word.

Input: ("b", ["a"])

Expected Output: []

Explanation: 'b' is not in the dictionary, so it cannot be built; empty list.

Input: ("abcd", ["ab", "cd", "abc", "d"])

Expected Output: ["ab", "cd"]

Explanation: Although 'abc' + 'd' is also valid, the solution returns one valid decomposition: 'ab' + 'cd'.

Hints

  1. Use dynamic programming over prefixes: let reachable[i] mean s[:i] can be segmented into dictionary words. reachable[0] is True (empty prefix).
  2. For each end index i, look back to a start index j where reachable[j] is True and s[j:i] is in the dictionary. Bound j by the maximum word length so you don't scan the whole prefix.
  3. To reconstruct the actual decomposition, store the chosen split point (choice[i] = j) for each reachable i, then walk backwards from n to 0 and reverse the collected words.
  4. Put the dictionary in a hash set for O(L) average-time membership checks; precompute the max word length to limit the inner loop.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)