← Questions

Count ways to spell a word with element symbols

0
GoogleCodingLast asked Apr 2026

A coding interview question asked in Google's onsite coding round, last asked Apr 2026. It tests dynamic programming and strings.

You get a list of chemical element symbols (one or two letters each) and a word. Count the ways to write the whole word as a sequence of symbols, ignoring case. Symbols may be reused, and different split points count as different spellings.

Interface

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

  • count_spellings(symbols: list[str], word: str) -> int — use only the given symbols, not a built-in periodic table. The empty word has one spelling (no symbols).

Example

count_spellings(["P","H","Y","Si","C","S","Cs","I"], "Physics")  ->  4
# P-H-Y-Si-C-S, P-H-Y-Si-Cs, P-H-Y-S-I-C-S, P-H-Y-S-I-Cs

count_spellings(["A", "Aa"], "aaa")  ->  3      # A-A-A, Aa-A, A-Aa
count_spellings(["B", "O"], "boa")   ->  0      # no symbol for "a"

Constraints

  • Up to 200 symbols, unique ignoring case, each one or two ASCII letters.
  • 0 <= len(word) <= 10^5, letters only.
  • The count can be huge: return an exact big integer.

Clarifying questions

  • Is matching case-insensitive?
  • Do we count spellings, or must we list them?
  • May symbols be longer than two letters?
  • Should the answer be exact or modulo some number?

What a strong answer covers

  • Lower-case the symbols into a set and the word once.
  • DP over prefixes: ways[i] = ways[i-1] * [word[i-1] is a symbol] + ways[i-2] * [word[i-2:i] is a symbol], with ways[0] = 1. O(n) time, and O(1) extra space by keeping only the last two values.
  • Why listing all spellings is exponential and must be avoided for long words.
  • Big-integer cost for very long words, and the modular variant.
  • Tests: empty word, a word with an unusable letter, overlapping one- and two-letter options.

Follow-ups

  • Return one spelling, or all of them for short words.
  • Symbols may have any length up to L: the DP looks back L positions, or uses a trie.
  • Return the spelling with the fewest symbols.

Where it's asked

  • Google · Onsite coding · Last asked Apr 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)

What it tests

dynamic programming · strings

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