Micro1 interview question

1 Implement a function that takes two strings as input: source text and pattern 2 Return the count of complete word matches of the pattern in the text 3 Handle edge cases like pattern at start/end of text 4 Consider spaces and punctuation marks (.,!?) as word boundaries 5 Maintain case sensitivity in pattern matching 6 Pattern length will be at least 1 character 7 Source text can be empty in nodejs

Interview Answer

Anonymous

28 Jan 2025

function countWordMatches(text, pattern) { if (text.length === 0) { return 0; } const regex = new RegExp(`\\b${pattern}\\b`, 'g'); const matches = text.match(regex); if (!matches) { return 0; } return matches.length; } const text = "The quick brown fox jumps over the lazy dog. The fox was quick!"; const pattern = "fox"; console.log(countWordMatches(text, pattern));

1