Find Duplicate Words (Medium)
Good morning! Here's our prompt for today.
You may see this problem at Cisco, Indeed, Linkedin, Wework, Intuit, Citadel, Snowflake, Robinhood, Red Hat, Canva, Mailchimp, Digitalocean, Elastic, and Dell.
A very common interview challenge is to determine how often words appear in a given string or set of strings. Here's a version of this: return a list of duplicate words in a sentence.

For example, given 'The dog is the best'
, returns ["the"]
.
Likewise, given 'Happy thanksgiving, I am so full'
, you would return an empty array. This is because there are no duplicates in the string.
Constraints
- The total length of the string <=
100000
- The string will consist of ASCII characters (may be some or all)
- Expected time complexity :
O(n)
wheren
are the number of words in the string - Expected space complexity :
O(n)
xxxxxxxxxx
30
var assert = require('assert');
function findDuplicates(str) {
const dupes = [];
// fill in
return dupes;
}
try {
assert.deepEqual(findDuplicates('The dog is the best'), ['the']);
console.log(
'PASSED: ' + "`findDuplicates('The dog is the best')` returns `['the']`"
);
} catch (err) {
console.log(err);
}
try {
assert.deepEqual(findDuplicates('Happy thanksgiving, I am so full'), []);
console.log(
'PASSED: ' +
"`findDuplicates('Happy thanksgiving, I am so full’)` returns `[]`"
);
} catch (err) {
OUTPUT
Results will appear here.