← Questions

Count the words in a document

0
MetaMicrosoftCodingLast asked Sep 2025

A coding interview question asked in Meta's AI-enabled coding round and at Microsoft, last asked Sep 2025. It tests strings and parsing.

Given the text of a document, return how many words it contains. The code is short; the real work is the definition. Agree with the interviewer on exactly what counts as a word, state the rules, then write and test the counter.

Interface

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

  • count_words(text: str) -> int
  • A word is a maximal run of letters or digits (any script). An apostrophe or a hyphen between two such characters joins them into one word, so it's and well-known are one word each. Everything else, including punctuation, whitespace and emoji, separates words.

Example

count_words("It's a well-known fact: 3 cats.")   ->  6    # It's | a | well-known | fact | 3 | cats
count_words("  hello   world  ")                 ->  2
count_words("")                                  ->  0
count_words("e-mail, café")                      ->  2    # é is a letter

Constraints

  • The document may be far larger than memory, so the counter must also work on a stream of chunks.
  • Text is UTF-8.

Clarifying questions

  • Is it's one word or two? And don't, rock-'n'-roll?
  • Do numbers like 3, 3.14 or 1,000 count as words?
  • What about hyphens at a line break, URLs, email addresses and emoji?
  • Non-Latin scripts: is a run of Chinese characters one word?
  • Is the input a string, a file, or a stream?

What a strong answer covers

  • A written tokenization policy before any code, with the edge cases above decided.
  • A single pass with a small state machine (inside a word or not, plus the joiner character just seen), O(n) time and O(1) memory. Or a regular expression, together with its limits.
  • Streaming: read in chunks and carry the state across chunk boundaries, so a word split across two chunks is counted once.
  • Unicode: classify by character category, not by ASCII ranges, and decode at code-point boundaries.
  • Test cases for every rule: leading and trailing spaces, repeated punctuation, joiners at the edges ('quoted', -dash), an empty input.

Follow-ups

  • Count how often each word appears, case-insensitively, on a file of 100 GB.
  • Split the count across many machines. What happens to words cut at the split points?
  • Make the policy configurable without rewriting the scanner.

Where it's asked

  • Meta · AI-enabled coding · Last asked Sep 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
  • Microsoft · Coding · Last asked Aug 202560 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent

What it tests

strings · parsing

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