Trying Too Hard

In a post titled The Problem with the FizzBuzz Problem, Gayle Laakmann McDowell discussed the cognitive trap that a high performer can fall into when faced with a deceptively inelegant real world problem. She called it the “Smart Person’s Mirage.”

I was caught in this trap in an interview once myself. I was asked to write a function that took an integer parameter and wrote out an ascii art diamond pattern in the given size, like this:

  *  
 *** 
*****
 *** 
  *  

The symmetry cried out to me. There must be some way, I was sure, to leverage it in both directions at once. It seemed like an affront to the concept of elegance if I didn’t! Also, there were so many options. Use a buffer, or several, or none? Make nested loops? How should an even parameter be handled?

In a matter of minutes, I had pieces of at least three conflicting solutions scattered all over the whiteboard. That was when the interviewer let me know that that was exactly what the question was designed to test for.

The way out of this failure mode is to not try too hard. As tempting as it is to write code poetry and dazzle my interviewer, the bottom line is that it’s not required. Moving forward with something, anything, that actually completes the task is. Optimizaton, neatness and elegance (and sometimes insight) can come later.

Inspired by Gayle’s post, I revisited the ascii art diamond problem. By letting go of finding a “perfect” solution, I came up with a workable one in a couple of minutes. Here it is in all its kludgy, messy glory.

    /* diamond.c
     * utility to take an integer on the command line and
     * output an ascii art diamond in that size made of splats
     */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
    
    #define MAX_DIMENSION 41
    #define DEFAULT_DIMENSION 5
    
    int main (int argc, char **argv) {
    	char line[MAX_DIMENSION];
	static char on_char = '*';
	static char off_char = ' ';
	int dimension = DEFAULT_DIMENSION;
	int midpoint;
	int lineno = 1;
	int columnno = 1;
	int i, j;

	if (argc > 1) {
		dimension = atoi (argv[1]);
		/* even dimensions end up being treated as the
		 * next highest odd dimension (ex. 4 makes a 5x5)
		 */
    /*		if (dimension % 2 == 0)
			dimension += 1; */
		if (dimension > MAX_DIMENSION)
			dimension = DEFAULT_DIMENSION;
	}

	memset (line, off_char, MAX_DIMENSION * sizeof (char));
	midpoint = dimension / 2 + 1;

	/* print top half of lines to midline */
	for (i = 0; i < midpoint; i++) {
		printf ("%s\n", line);
		line[midpoint - i] = on_char;
		line[midpoint + i] = on_char;
	}
		
	/* print bottom lines */
	for (; i > -1; i--) {
		line[midpoint - i] = off_char;
		line[midpoint + i] = off_char;
		printf ("%s\n", line);
	}
    		
	return 0;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s