Reported September 2021
Duolingounion find

Count Distinct Word Meanings

Reported by candidates from Duolingo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Duolingo OA. Under 2s to a working solution.
Founder's read

The whole Duolingo question from September 2021 hinges on one data structure: a union-find over a hash set of the words. If you reach for pairwise string comparison, you'll time out on 20000 words. The task is to count connected groups where two words link if deleting one character from the longer gives the shorter. It reads like a string problem but it's really a graph problem in disguise. If you blank on the structure during the live OA, StealthCoder runs invisibly on your desktop and gives you the approach in real time.

The problem

You are given an array words of lowercase English strings.
Two different strings are directly related when their lengths differ by exactly 1 and deleting exactly one character from the longer string produces the shorter string. This relationship is undirected.
Two strings have the same meaning when they are identical or connected by a chain of directly related strings that appear in words. Repeated occurrences of the same string belong to one meaning and do not create additional groups.
Return the number of distinct meanings represented by words.

Function
countDistinctMeanings(words: String[]) → int

Examples
Example 1
words = ["caw","caaw","caww","hoot","hooot","chirp"]
return = 3
The groups are [caw, caaw, caww], [hoot, hooot], and [chirp]. Both four-letter words connect to caw after one deletion.
Example 2
words = ["abc","abd","abcd"]
return = 1
Deleting d from abcd produces abc, while deleting c produces abd. The shared longer word connects all three strings into one meaning.

Constraints
1 <= words.length <= 20000.
1 <= words[i].length <= 20.
Every string contains only lowercase English letters.
The sum of all input-string lengths is at most 200000.
Repeated identical strings are allowed.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Dedupe the words into a hash set first, since repeats don't add groups. Give each unique word a union-find node. For each word, generate every string made by deleting one character (at most 20 of them). If a deletion result exists in the set, union the two words. Finally, count the distinct roots. That's roughly total length times 20 operations, which fits the 200000 character sum easily. The common pitfall is comparing all pairs, which is quadratic and dies at 20000 words. Another trap is forgetting that two words of the same length never connect directly, only through a shared longer or shorter word, as in the abc, abd, abcd example. Duplicates in the array also inflate your count if you skip dedup. StealthCoder is your hedge if the union-find setup slips your mind mid-assessment.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Count Distinct Word Meanings cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Duolingo's OA.

Duolingo reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Distinct Word Meanings FAQ

What's the trick to Count Distinct Word Meanings?+

Don't compare pairs. For each word, build every one-deletion variant and check if it's in a hash set of all words. Each hit means an edge, so union the two words. The answer is the number of connected components at the end.

Should I use union-find or DFS here?+

Either works. Union-find is cleaner because you just union as you scan deletions. DFS needs an adjacency list, and the longer-to-shorter edges are easy to build in only one direction by mistake. Union-find avoids that, so it's the safer pick under pressure.

How do I handle duplicate words?+

Put all words in a set first and only build nodes for unique strings. The problem says repeated occurrences belong to one meaning. If you count roots over the raw array without deduping, you'll overcount or crash on mapping.

What's the time complexity I should aim for?+

About O(L * 20) hash lookups, where L is the sum of word lengths, plus near-constant union-find cost. Each deletion makes a string up to 20 characters, so hashing costs a bit more. With a 200000 length sum, this is comfortably fast.

How do I prepare for this in 48 hours?+

Write union-find with path compression from memory once. Then solve this problem end to end using a map from word to index. Test it on both examples, including abc, abd, abcd, which links through the longer word. That covers the main edge cases.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Duolingo.

OA at Duolingo?
Invisible during screen share
Get it