Interview questions · Salesforce
Validate a Generalized Word Abbreviation
Validate whether a lowercase word matches a generalized abbreviation whose positive numbers skip characters. Enforce exact letter matching, full consumption, multi-digit counts, and the prohibition on leading zeroes.
Problem
Determine whether an abbreviation validly represents a word. An abbreviation contains lowercase letters and positive decimal numbers. A number means “skip this many characters” in the word. Letters must match the next unskipped word character exactly.
A numeric token may not start with 0. Both the word and abbreviation must be consumed completely.
Function Contract
Implement isValidWordAbbreviation(word, abbreviation) and return a Boolean.
Constraints & Assumptions
1 <= len(word) <= 100,000.0 <= len(abbreviation) <= 100,000.wordcontains lowercase English letters.abbreviationcontains lowercase English letters and digits.- A multi-digit number is parsed as one skip count.
- Skip counts may be larger than the remaining word length; that makes the abbreviation invalid.
Clarifying Questions to Ask
- Is zero a legal skip? No, so any numeric token beginning with
0is invalid. - Are adjacent digits one number? Yes.
- Must letter comparison be case-sensitive? Inputs are lowercase and matching is exact.
- Is an empty abbreviation valid for a nonempty word? No.
Hint: Advance two pointers with two token types
On a letter, compare and advance both pointers by one. On a digit, parse the complete number and advance only the word pointer by that count.
Examples
word = "internationalization",abbreviation = "i18n"returnstrue.word = "substitution",abbreviation = "s10n"returnsfalse.word = "apple",abbreviation = "a3e"returnstrue.word = "apple",abbreviation = "a03e"returnsfalse.word = "word",abbreviation = "5"returnsfalse.
Evaluation Focus
- Rejects leading zeroes, oversize skips, mismatched letters, and partial consumption.
- Parses long numeric tokens without integer overflow by stopping once the skip exceeds the remaining length.
- Runs in
O(len(word) + len(abbreviation))time andO(1)auxiliary space.
Extensions to Discuss
- How would literal digits inside the word be escaped?
- Can the parser report the first failing abbreviation index?
- How would wildcard letters differ from numeric skips?
Community answers
Nothing here yet.
Join in on CSWork →