Interview question: Find the most used character using a Hashtable

In my last post I discussed how to find the most used character in an ASCII string using only a INT[]. An interviewer would mostly ask you about that scenario to test your knowledge of programming to see if you knew of how to implement the algorithm using the least amount of memory. In this example, the immediate resources are not the biggest concern and we want to be able to handle Unicode characters. The interviewer is wanting to test your knowledge of the types the framework provides to you.
The method for doing this will be similar to the previous post accept we're using a Hashtable this time. An advantage of this will be that we won't have to determine the number of keys before we start parsing the string as the Hashtable object will handle this automatically. This may mean that more memory will be wasted and that there maybe more overhead needed when a resizing of the table is needed. But, this isn't the concern right now. Just make sure to mention this and that a way to keep the resizing overhead to a minimum would be to indicate the initial number of keys needed when initializing the Hashtable. In this example we're only worried about English alphabetical characters, so a size of 32 would be large enough (*why 32 was used i explained below).
Overall the algorithm will function the same as the FindMostUsedUnicodeChar() method we wrote in the last post. We're going to have 2 FOR loops with the first parsing the string and counting the number of instances for each character. The second loop will go through the Hashtable to determine the character that had the most instances. In the end, the character with the most hits will be returned.
/**
* Determines the first alphabetical character that has the most instances within the string.
* The search is case insensitive.
* @param givenString The string.
* @return The character or null if none are found.
* @throws IllegalArgumentException
*/
public static char FindMostUsedUnicodeChar(final String givenString)
throws IllegalArgumentException {
// Verifying the string is not NULL.
if (givenString == null)
throw new IllegalArgumentException("The parameter givenString cannot be null.");
// At this time we're only concerned with the letters A-Z., the 26 letter in the English
// alphabet, for this example. This is why we're initializing the hashtable to a size of 32;
// the smallest 2^Nth value that's greater than 26.
Hashtable<Character, Integer> hashtable = new Hashtable<Character, Integer>(32);
// Looping through the string and counting the number of times a alphabetical character
// appears. We're doing a case insensitive search.
final char givenChars[] = givenString.toUpperCase().toCharArray();
for (int index = 0; index < givenChars.length; index++) {
final char key = givenChars[index];
if (key < 'A' || 'Z' < key) continue;
int charCount = 1;
if (hashtable.containsKey(key)) charCount = hashtable.get(key) + 1;
hashtable.put(key, charCount);
}
// Finding the first letter to have the highest use count by the order that they appeared.
char mostUsedChar = '0';
int highestCharCount = 0;
for (int index = 0; index < givenChars.length; index++) {
char key = givenChars[index];
if (key < 'A' || 'Z' < key) continue;
int currentCount = hashtable.get(key);
if (highestCharCount < currentCount) {
mostUsedChar = key;
highestCharCount = currentCount;
}
}
// Done
return mostUsedChar;
}
As before, you may want to ask what behavior is expected if there are an equal number of 'a' and 'c' characters. This implementation, like the last, will return the first letter found in the string with the highest count. You will need to know that this algorithm is O(n) because the FOR loops are in series, not nest like you would see with O(n^2) solutions. Some test cases you will want to consider are how to handle a null string, an empty string, and a string with no alphabetical characters.
Is there anything you would change in this method? Have any tips from interviews you've had in which this question was asked? If so, share your experience and leave a comment!
Full source code here.
* I used 32 instead of 26 because memory is allocated in blocks of 2^Nth. It's the smallest 2^Nth value that's large enough to contain the 26 keys.

