Hedge Fund Coding Skill Screening: Part I

technology — oliver on February 12, 2008 at 11:06 pm

One of my friends recently interviewed with a hedge fund in New York. The fund conducted a brief phone interview and then sent an email with two interesting coding problems. You could choose any of the three major languages, i.e. C, C++, or Java. I like Java. They’re not all that challenging at the end of the day; however, with limited time, one may not find these assignments trivial. Since these are actual interview questions from yesterday, I’m not going to mention the fund’s name. In theory, although very unlikely, someone could find this while their taking the very same exam.

The first assignment:

Sentences such as “A quick brown fox jumps over the lazy dog”, contain every single letter in the alphabet. Such sentences are called pangrams. You are to write a method getMissingLetters, which takes a String, sentence, and returns all the letters it is missing (which prevent it from being a pangram). You should ignore the case of the letters in sentence, and your return should be all lower case letters, in alphabetical order. You should also ignore all non US-ASCII characters. The code you submit must contain a method that conforms to the expected method signature, as follows: public String getMissingLetters(String sentence).

Here are some examples:

1) "A quick brown fox jumps over the lazy dog"
Returns: ""

2) "A slow yellow fox crawls under the proactive dog"
Returns: "bjkmqz"

3) "Lions, and tigers, and bears, oh my!"
Returns: "cfjkpquvwxz"

4) ""
Returns: "abcdefghijklmnopqrstuvwxyz"

Here is my solution:

public String getMissingLetters(String sentence)
{
	boolean occurances[] = new boolean[26];

	for(int i = 0; i < sentence.length(); i++)
	{
		int c = (int)sentence.charAt(i);

		if(c >= 'A' && c <= 'Z')
			occurances[c-'A'] = true;
		else if(c >= ('A'+32) && c <= ('Z'+32))
			occurances[c-'A'-32] = true;
	}

	StringBuilder missing = new StringBuilder();

	for(int i = 0; i < occurances.length; i++)
		if(!occurances[i])
			missing.append((char)(i+'A'+32));

	return missing.toString();
}

The complete source code, with the test cases: MissingLetters.java.

2 Comments »

  1. Here’s a functional approach contributed by Mark Fitzgerald. Pretty cool–enjoy!

    private static String getMissingLetters(String sentence) {
    	Set<Character> missingCharSet = new TreeSet<Character>();
    	Set<Character> foundCharSet = new TreeSet<Character>();
    	StringBuffer missingCharsSB = new StringBuffer();
    
    	for (char c='a'; c<='z'; ++c)
    		missingCharSet.add(c);
    	for (char c: sentence.toLowerCase().toCharArray())
    		foundCharSet.add(c);
    
    	missingCharSet.removeAll(foundCharSet);
    
    	for (char c : missingCharSet)
    		missingCharsSB.append(c);
    
    	return missingCharsSB.toString();
    }
    
    Comment by oliver — January 16, 2010 @ 7:11 pm
  2. Also contributed by Mark Fitzgerald–the entire program, in-line and in Python 3. Shows the real power of functional scripting languages.

    import string
    
    for testSentence in [
        'A quick brown fox jumps over the lazy dog',
        'A slow yellow fox crawls under the proactive dog',
        'Lions, and tigers, and bears, oh my!',
        'abcdefghijklmnopqrstuvwxyz']:
        print(''.join(set(string.ascii_lowercase) - set(testSentence)))
    
    Comment by oliver — January 16, 2010 @ 7:26 pm

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

By Oliver Kaljuvee. 2007.