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

## Interview Question

Software Development Engineer Interview London, England

# 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

1

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

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

Anonymous on 30 Apr 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 &lt; str.length(); e++){
if(str.charAt(e) != ' '){
count++;
while(str.charAt(e) != ' ' &amp;&amp; e &lt; str.length()-1){
e++;
}
}
}
return count;
}

Anonymous on 30 Apr 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 17 Dec 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 22 Jan 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&amp; 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&#039;s Simple on 26 Feb 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 5 Jan 2015
1

Wait..Am I missing something?

string.Length

will do the job?

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

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

Rabeel on 13 Oct 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 18 Mar 2016
0

I would do this:

public static int countWords(String sentence){

String noSpace;

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

return noSpace.length
}

Sergio on 22 Jul 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 27 Nov 2018

One or more comments have been removed.