Count the number of occurrences of a word

  • I was selected for the third round of MS internships for third years. We were surprisingly asked a very easy question: “Make a program that counts the number of times the WORD "a" or "A" occurs”. I wrote the code below and was rejected from attending the final interview.

    What is wrong with my code? Please tell me how to improve it. People who used Char[] instead of string and those who didn't check for first and last words to be "A" were all selected. The condition for commas before and after 'a' was also ignored. What is the error?
    Is not good enough...? I know even if we use str[i] it is interpreted as str.operator[](i), so I'm preventing the overhead conversion, right?


    using namespace std;

    int main()

    string str;


    int i;
    int count=0;
    int l=str.length();

    if(' ' &&' ')

    if(toupper('A' &&' ')

    if(toupper('A' &&' ')

    cout<<"Count is "<<count<<endl;
    return 0;


    What version of C++ are you using? I'm not familiar with the `included` statement.

    One problem I see is: what happens if your input string is just 'A'?

    "a" & 'a' are different, 'a' is a single character whereas "a" is a *string* i.e. 'a','\0'

    What I would have done is use a `std::stringstream`. I think that is the easiest possible way.

    I would have tried a state machine. It is probably quite simple to implement and have a single forwarding loop over the input.

    `using namespace std;` - I'd probably stop reading there, TBH.

  • Sulthan

    Sulthan Correct answer

    9 years ago

    First of all, these interview questions are often a trick. It doesn't really matter if your code works for all cases (everybody makes mistakes). What does matter is how you write the code.

    Even if they ask specifically for 'a' or 'A', you are not supposed to hardcode these values. They are a parameter. Understanding what is the input to your program is always the first task. If they ask you "Make a program that counts the number of times the letter 'a' occurs in 'Example'", the correct answer won't be return 1;. They also ask for words, you shouldn't assume that the program should search only for words withs 1 letter.

    Second - words are not usually delimited only by a space. You should consider all whitespace and punctuation characters. Or just declare a function isWordDelimiter(char) and don't implement it.

    Third - your code is not easily readable. An if inside another if in a for? Use functions. Example (pseudocode - I am not C++ programmer and I forgot STL):

    while ((word = getNextWord(input)) != NULL) {
    if (word is in set of searched words) { //replace this with STL set

    Even on a very simple program, they can see how much experienced you are. Good developer won't just write something that works. He has to think how the problem will evolve in the future (different parameters), how the program will be maintained (write readable code). Good developers also write programs from top to bottom - first define the structure using high level functions, then write the implementation of the functions.

    I wouldn't call such questions a "trick" - instead, they are seemingly-simple questions that can be attempted by anybody, but can also be ramped up to exercise even outstanding candidates (*"How do you identify word boundaries? In Japanese?"*). Also, in an interview (unlike a written test), they give the candidate an opportunity to show how they gather requirements ("*What size input are you giving me? Do I need to case-fold in a specific locale?*").

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM