Google Interview Question: The questions were not very d... | Glassdoor

Interview Question

Software Development Engineer Interview London, England (UK)

The questions were not very difficult but you really need

  to have all the concepts crystal-clear and be ready to apply them successfully. One of the questions was "how to count the letters in this string:" "The quick brown fox jumps over the lazy dog";
Tags:
algorithm, count, letters
Answer

Interview Answer

12 Answers

1

public static int countWords(String str){
        if(str == null || str.isEmpty())
            return 0;
        int count = 0;

        for(int e = 0; e < str.length(); e++){
            if(str.charAt(e) != ' '){
                count++;
                while(str.charAt(e) != ' ' && e < str.length()-1){
                    e++;
                }
            }else{
                e++;
            }
        }
        return count;
    }

Anonymous on Apr 30, 2013
1

Sorry, the above version has an error!!!!!!!!!!!!!!!!!!!!!!!!
concider this one:

public static int countWords(String str){
        if(str == null || str.isEmpty())
            return 0;
        int count = 0;

        for(int e = 0; e < str.length(); e++){
            if(str.charAt(e) != ' '){
                count++;
                while(str.charAt(e) != ' ' && e < str.length()-1){
                    e++;
                }
            }
        }
        return count;
    }

Anonymous on Apr 30, 2013
1

sorry u need to give input ;)
------
len(re.findall('[a-zA-Z]', "The quick brown fox jumps over the lazy dog"))

kmonsoor on Dec 17, 2013
1

It depends on what has to be considered a word. For example, if we consider a word any string between spaces, we can write it in a more compact way:

public static int countWords(String str){
        if(str == null || str.isEmpty())
            return 0;

        return str.split(" ").length;
}

Anonymous on Jan 22, 2014
1

The question is how to count the characters in the string. If we can assume that the input is ASCII - always clarify first - then we know that the character range is 0-255.

void count(char* str, int counts[255]) {
  if (str == 0) return;
  if (counts == 0) return;
  for (char *c = str; c != 0; c++) {
    counts[*c]++;
  }
}

First we assert the input is valid. Note that we take an additional parameter - an array of the count of each ASCII character.

We loop through the string until we reach the null terminator, and we iterate a char pointer through each character in the string. For each iteration, we increment the counter in the array.

Memory complexity is O(1), runtime complexity is O(n).

If the input must be unicode, then we may consider alternatively using a hash table.

void count(wchar_t* str, std::unordered_map& counts) {
  if (str == 0) return;
  for (char *c = str; c != 0; c++) {
    counts[*c]++;
  }
}

We still arrive at an O(1) memory complexity and O(n) runtime complexity (although it is worth noting that despite a hash table lookup takes O(1) like an array, the fixed cost of each lookup is higher for the hash table due to the hashing function, and in the worst case a lookup can be O(n)).

It's Simple on Feb 26, 2014
1

The question asks to count the characters, without using String.length() you can do this:

public static Pair countLetters(String s) {
        //If the string is null or it is empty then it will have no character
        if (s == null || s.isEmpty()) {
            //So return the pair with 0 and 0
            return new Pair(0, 0);
        }
        //If we should still run the loop.
        boolean run = true;
        //The count of characters
        int count = 0;
        //The count of characters without spaces
        int countWithoutSpace = 0;
        //While we are still running (we are at a valid index)
        while (run) {
            //Then try to
            try {
                //Get the character at the current count
                char c = s.charAt(count);
                //Add one to the count as we have a valid character
                count++;
                //If the character is not a space
                if (c != ' ') {
                    //Then add one to the count without spaces.
                    countWithoutSpace++;
                }
                //If we get a StringIndexOutOfBooundsException it means that the current count is outside the length of the string.
            } catch (StringIndexOutOfBoundsException e) {
                //So stop the loop.
                run = false;
            }
        }
        //And return the pair with the count and the count without spaces.
        return new Pair(count, countWithoutSpace);
    }

This returns both the count with and without spaces and does not use a built in length function.

Anonymous on Jan 5, 2015
1

Wait..Am I missing something?

string.Length

will do the job?

Count letters in a string???? on Feb 14, 2015
0

Yeah i think string.length() will do the job.

Rabeel on Oct 13, 2015
0

failed! String.length returns the length of the String. Google asked you how many letters - in other words you cannot count the spaces. String. length returns number of unicode characters -and so includes spaces

Anonymous on Mar 18, 2016
0

I would do this:

public static int countWords(String sentence){

String noSpace;

//REMOVE SPACE
noSpace = sentence.replaceAll(" ", "");

return noSpace.length
}

Sergio on Jul 22, 2016
0

And in javascript
var str = "The quick brown fox jumps over the lazy dog" ;
var length = str.replace(/\s/g, "").length;

Tim on Nov 27, 2018

One or more comments have been removed.
Please see our Community Guidelines or Terms of Service for more information.

Add Answers or Comments

To comment on this, Sign In or Sign Up.