As reported: Goldman Sachs Associate (SDE-II) CoderPad round 1 (2025): an easier version of the trie prefix-counting problem with only
insert(word)andcountWordsStartingWith(prefix).
Implement PrefixCounter:
insert(word)addsword(inserting the same word twice counts it twice);countWordsStartingWith(prefix)returns how many inserted words start withprefix(a word is a prefix of itself).
Example 1
["PrefixCounter","insert","insert","countWordsStartingWith","countWordsStartingWith","insert","countWordsStartingWith","countWordsStartingWith"]
[[],["apple"],["app"],["app"],["appl"],["apple"],["apple"],["b"]]
-> [null,null,null,2,1,null,2,0]
Constraints: words and prefixes are 1–10 lowercase letters; at most 2·10^4 calls.
Code runner format: LeetCode's design format — the first line lists the calls (starting with the constructor), the second their arguments; the harness prints one JSON array with each call's result (null for insert).