← Questions

Is every 3+ letter piece of a string a dictionary word?

0
GoogleCodingLast asked Mar 2026

A coding interview question asked in Google's first round (coding), last asked Mar 2026. It tests recursion, strings and hash maps.

You get a lowercase string s and a dictionary of valid English words. Check whether every piece of s of length 3 or more is acceptable, first as an exact word and then, in the follow-up, as an anagram of a word. A piece here is a contiguous substring; confirm that with the interviewer.

Part 1: every piece is a word

Unless you agree on a different interface with the interviewer, assume:

  • all_pieces_are_words(s: str, words: list[str]) -> bool — True if every substring s[i:j] with j - i >= 3 is in words. If len(s) < 3 there is nothing to check, so return True.
all_pieces_are_words("cats", ["cat", "ats", "cats"])  ->  True     # cat, ats, cats
all_pieces_are_words("cats", ["cat", "cats"])         ->  False    # "ats" is missing
all_pieces_are_words("ab", [])                        ->  True

Part 2: every piece is an anagram of a word

  • all_pieces_are_anagrams(s: str, words: list[str]) -> bool — True if every substring of length 3 or more can be rearranged into some dictionary word (same letters, same counts).
all_pieces_are_anagrams("act", ["cat", "dog"])            ->  True     # act -> cat
all_pieces_are_anagrams("abca", ["cab", "abca", "zzz"])   ->  True     # abc, bca -> cab; abca itself
all_pieces_are_anagrams("abca", ["cab"])                  ->  False    # nothing matches "abca"

Constraints

  • 0 <= len(s) <= 300.
  • Up to 5,000 dictionary words, at most 200,000 letters in total; all lowercase a-z.

Clarifying questions

  • Contiguous substrings, or subsequences? (The number of subsequences is exponential, so ask.)
  • Is the dictionary given as a list, a set, or an API I call per word?
  • Do pieces of length 1 and 2 matter?

What a strong answer covers

  • There are about n^2 / 2 pieces. Part 1: a hash set of words and a check of each piece, O(n^3) with string slicing, or O(n^2) with a trie walked from each start (stop early on the first miss).
  • Part 2: canonical keys for anagrams, either the sorted letters or a 26-count tuple. Precompute the set of keys for the dictionary, grouped by length.
  • Sliding a window per length so each next piece's letter count is updated in O(1) instead of rebuilt: O(26 * n^2) overall.
  • Early exit on the first failing piece, and a quick reject when the dictionary has no word of some needed length.
  • Tests: len(s) < 3, the full string missing, repeated letters.

Follow-ups

  • Return the first failing piece.
  • Count how many pieces are acceptable instead of requiring all of them.
  • Pieces are subsequences instead of substrings: what is feasible?

Where it's asked

  • Google · First round: coding · Last asked Mar 202645 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

recursion · strings · hash maps

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding