0
0
DSA Javascriptprogramming~20 mins

Count Words with Given Prefix in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of prefix count function with simple input
What is the output of the following JavaScript code that counts words starting with a given prefix?
DSA Javascript
function countWordsWithPrefix(words, prefix) {
  let count = 0;
  for (const word of words) {
    if (word.startsWith(prefix)) {
      count++;
    }
  }
  return count;
}

const words = ['apple', 'app', 'application', 'banana', 'apex'];
console.log(countWordsWithPrefix(words, 'app'));
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Count how many words start exactly with 'app'.
🧠 Conceptual
intermediate
1:30remaining
Understanding prefix matching in strings
Which of the following statements correctly describes how to check if a word starts with a given prefix in JavaScript?
AUse word.indexOf(prefix) === word.length - prefix.length to check prefix.
BUse word.includes(prefix) to check if the prefix is anywhere in the word.
CUse word.endsWith(prefix) to check if the word starts with the prefix.
DUse word.startsWith(prefix) to check if the word begins with the prefix.
Attempts:
2 left
💡 Hint
Think about the method that checks the start of a string.
🔧 Debug
advanced
2:00remaining
Identify the error in prefix counting code
What error will the following code produce when run, and why?
DSA Javascript
function countPrefix(words, prefix) {
  let count = 0;
  for (let i = 0; i <= words.length; i++) {
    if (words[i].startsWith(prefix)) {
      count++;
    }
  }
  return count;
}

console.log(countPrefix(['cat', 'car', 'dog'], 'ca'));
ASyntaxError due to missing semicolon.
BReturns 0 because prefix is not found.
CTypeError: Cannot read property 'startsWith' of undefined because loop goes out of bounds.
DWorks correctly and returns 2.
Attempts:
2 left
💡 Hint
Check the loop condition and array indexing.
🚀 Application
advanced
2:00remaining
Count words with prefix ignoring case
Which code snippet correctly counts words starting with a prefix ignoring case differences?
Awords.filter(word => word.toLowerCase().startsWith(prefix.toLowerCase())).length
Bwords.filter(word => word.startsWith(prefix)).length
Cwords.filter(word => word.includes(prefix.toLowerCase())).length
Dwords.filter(word => word.toUpperCase() === prefix.toUpperCase()).length
Attempts:
2 left
💡 Hint
Convert both word and prefix to the same case before checking.
🧠 Conceptual
expert
1:30remaining
Time complexity of counting words with prefix
What is the time complexity of counting how many words start with a given prefix in a list of n words, each of average length m?
AO(n * m) because each word is checked character by character for the prefix.
BO(n^2) because nested loops are needed.
CO(m) because only prefix length matters.
DO(n + m) because prefix check is constant time.
Attempts:
2 left
💡 Hint
Consider how many words and how many characters per word are checked.