Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the function countWordsWithPrefix(words, prefix) do when given a list of words and a prefix?
easy
A. Counts how many words contain the prefix anywhere
B. Counts the total number of words in the list
C. Counts how many words end with the given prefix
D. Counts how many words start with the given prefix

Solution

  1. Step 1: Understand the function purpose

    The function checks each word to see if it starts with the given prefix.
  2. Step 2: Count matching words

    It counts only those words that begin exactly with the prefix, ignoring others.
  3. Final Answer:

    Counts how many words start with the given prefix -> Option D
  4. Quick Check:

    Starts with prefix = Count [OK]
Hint: Look for words starting exactly with prefix [OK]
Common Mistakes:
  • Counting words containing prefix anywhere
  • Counting words ending with prefix
  • Counting total words instead of filtered ones
2. Which of the following is the correct syntax to check if a string word starts with a prefix prefix in JavaScript?
easy
A. word.beginWith(prefix)
B. word.hasPrefix(prefix)
C. word.startsWith(prefix)
D. word.starts(prefix)

Solution

  1. Step 1: Recall JavaScript string methods

    JavaScript strings have a method called startsWith() to check prefix.
  2. Step 2: Match method name

    Only startsWith() is the correct method; others do not exist.
  3. Final Answer:

    word.startsWith(prefix) -> Option C
  4. Quick Check:

    Correct method name = startsWith [OK]
Hint: Use startsWith() method for prefix check [OK]
Common Mistakes:
  • Using non-existent methods like hasPrefix
  • Using incorrect method names like beginWith
  • Confusing startsWith with other string methods
3. What is the output of the following code?
const words = ['apple', 'app', 'application', 'banana'];
const prefix = 'app';
function countWordsWithPrefix(words, prefix) {
  let count = 0;
  for (const word of words) {
    if (word.startsWith(prefix)) {
      count++;
    }
  }
  return count;
}
console.log(countWordsWithPrefix(words, prefix));
medium
A. 3
B. 2
C. 1
D. 4

Solution

  1. Step 1: Check each word against prefix 'app'

    'apple', 'app', and 'application' all start with 'app'. 'banana' does not.
  2. Step 2: Count matching words

    There are 3 words starting with 'app', so count is 3.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Words starting with 'app' = 3 [OK]
Hint: Count words starting exactly with prefix [OK]
Common Mistakes:
  • Counting words containing prefix anywhere
  • Missing one of the matching words
  • Counting all words regardless of prefix
4. The following code is intended to count words starting with a prefix, but it has a bug. What is the bug?
function countWordsWithPrefix(words, prefix) {
  let count = 0;
  for (let i = 0; i <= words.length; i++) {
    if (words[i].startsWith(prefix)) {
      count++;
    }
  }
  return count;
}
medium
A. startsWith method is used incorrectly
B. Loop goes out of bounds causing an error
C. count variable is not initialized
D. Prefix is not passed as a parameter

Solution

  1. Step 1: Analyze loop condition

    The loop runs with i from 0 to words.length inclusive, but array indices go from 0 to words.length - 1.
  2. Step 2: Identify out of bounds access

    At i = words.length, words[i] is undefined, so calling startsWith causes an error.
  3. Final Answer:

    Loop goes out of bounds causing an error -> Option B
  4. Quick Check:

    Loop index must be less than length [OK]
Hint: Use i < words.length, not <= [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Not checking array bounds before access
  • Assuming startsWith works on undefined
5. Given the array words = ['Test', 'testing', 'Tester', 'tent', 'team'] and prefix 'te', which code snippet correctly counts words starting with the prefix ignoring case sensitivity?
hard
A. words.filter(w => w.toLowerCase().startsWith(prefix.toLowerCase())).length
B. words.filter(w => w.startsWith(prefix)).length
C. words.filter(w => w.includes(prefix)).length
D. words.filter(w => w.endsWith(prefix)).length

Solution

  1. Step 1: Understand case sensitivity issue

    Direct startsWith is case sensitive, so 'Test' and 'test' differ.
  2. Step 2: Use toLowerCase() to normalize

    Convert both word and prefix to lowercase before checking startsWith to ignore case.
  3. Final Answer:

    words.filter(w => w.toLowerCase().startsWith(prefix.toLowerCase())).length -> Option A
  4. Quick Check:

    Normalize case before prefix check [OK]
Hint: Convert both strings to lowercase before startsWith [OK]
Common Mistakes:
  • Ignoring case sensitivity
  • Using includes instead of startsWith
  • Using endsWith instead of startsWith
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard