How to

When solving these problems, learning by doing is much better than learning by reading. I encourage to you read only as far in the solution as you need, then trying to solve the problem. If you get stuck, try reading a little further. And of course, let me know if you find a better solution!

Tuesday, February 28, 2012

What goes with if?

Problem:  Write what goes inside of the if statement to produce, "Hello World.\n"

void printHelloWorld () {
   
    if(...) {
        printf("Hello ");
    } else {
        printf("World.\n");
    }
}


Solution:  Let's start by trying some of the obvious conditions that could go inside the if statement.  The most obvious are true and false.


Clearly, neither of these works.  If we use true, the output is only "Hello".  And, if we do false, the output is only "World.\n".  


So this is tricky, which it would almost have to be to be an interview question.  


One thing that we know about an if statement is that 0 evaluates to false, and everything else to true.  Can we use this fact?  Well, we could put in something that could be contingent, but ultimately, the if statement would evaluate to true of false, so this doesn't help us to have both the clause after if and the clause after else print.


But, that's not to say that it won't help us.  We can have something print in the if statement.  For example, we can have a function in the if statement, which would both print and return a boolean value.  If we do this, we can then print part of the if/else statement.  


And, since the evaluation part comes first, we would want to print the first part of the "Hello World\n." statement and then trigger the second half (We can't do it backwards by printing World\n. last).  


And, we'll need a function that always returns false, but that is easy too.


Now, we can code up just such a function as:


bool printHelloAndReturnFalse () {


    printf("Hello ");
    return (false);
}


Now, let's test our function and make sure if works.   When we call PrintHelloWorld(), the function will call what we wrote above and print, "Hello ".  The false will trigger the else clause and print, "World.\n".  This is exactly what we want.


Now, this is a good solution, but try not to stop here.  Other than a programming exercise, can you think of a time when you may have to do something like this?  (Even if you're not asked this, it's good to think about it.)


Well, we may need an evaluator function for certain packages.  And, we may want the evaluator function to do more than just evaluate (e.g., print to a log, etc.).  In this case, we would want to make sure that an evaluator did more than just return a boolean value, and it's a case of where we would write such a function.


This is a good example of a problem where it's important to try (and quickly realize that they don't work) obvious answers, then think about the challenges to the question and how it can be overcome.  Write a solution (pretty easy) -- and then stretch your answer to show your interviewer that you're always looking for an even better solution.


/*Please let me know if there are bugs or if you find a better solution.  Thank you.  Noah */



No comments:

Post a Comment