Mathematics
Sierpinski Triangle Feedback Loop
Thursday, June 18th, 2009How does a FOR loop work?
Wednesday, October 29th, 2008During a recent round of interviews we asked candidates to point out the problem with the following code snippet
for($i = 0; $i < 10; ++$i)
if ($i = 0)
print "yes";
This was intended as a simple, spot-the-typo, exercise. The problem is if ($i = 0), should read if ($i == 0)! As is, it's an infinite loop, and the follow up question is to ask if it's infinitely printing "yes" or not.
Relatively easy and straightforward. What happened in the actual interviews was anything but straightforward. Without fail, candidates kept pointing out the pre-increment ++$i. My first thought was to put them back on track, because "no, that has nothing to do with it." Instead I decided to explore.
"Wait, tell me why the pre-increment would be a problem?" I asked curiously.
Every candidate seemed to have a different explanation for why a pre-increment wouldn't work. I would then ask them to walk through the execution of the loop. And this was the mistake that each candidate shared, they didn't know how a standard three-expression for loop worked.
Stubbornly, many would insist that the pre-increment would be run first, before the assignment statement. I would ask, "so the execution order of the three expressions is dependent on the third expression?", hoping they would see the absurdity in such a claim. "Yes," was the confident reply. sigh
"No," I would explain (feeling a little defeated), "the first expression is always executed first and only once, the third expression is only executed after a loop iteration."
I would go on to explain that the pre-increment works perfectly fine. In fact, there is some argument that it is more efficient since it is not creating temporary variables (a post-increment will create a temporary variable to hold the original value that is returned from the statement). Either way, in the context of a three-expression for loop it makes no difference; whichever method lends itself to better readability in your code should be used.
None of the candidates who missed this question were considered for a position.
And what did I learn? That we should use foreach with Iterator objects, and I'm switching to Python!
Write 63 in Binary
Tuesday, September 2nd, 2008One of my coworkers likes to ask people to write 63 in binary when he interviews them.
Some of you might be thinking “that’s stupidly easy”, and many of you may be thinking “I have no idea and I hope I’m never asked that!”
There are many amusing answers to this request. Chief among them is to claim that they (in a computer or IT related field) have no need to know how to write a number in binary. That is the wrong answer. And shame on you for even considering such a thing — change professions immediately! If this question doesn’t appeal to you, then everything in Computer Science and Mathematics will seem like confusing magic and you’ll likely embarrass yourself on a daily basis. This is true for Project Managers, which is likely why so many PMs embarrass themselves all too often.
The correct answer, of course, is not to blurt out the binary string without even thinking, that would appear arrogant at best and cheating at worst. The correct answer is to either write or visibly start plotting out successive powers of 2, and upon reaching 64 (1000000) quickly slap your head at the realization that 63 is, in binary, 1 minus 1000000 therefor 0111111. You get partial credit for knowing, but demonstrating that you realized it was a silly question will get you full credit.
Now here is where the interview can get interesting. Why would a number like this be useful? What would happen if I bit-shift left or right? Suddenly a basic understanding of Boolean logic and Computer Science are uncovered! And at that point it becomes clear who is storing IP addresses as varchars and who is storing them as integers.
Rediscovering Fracal Visualization
Sunday, August 31st, 2008As I was cleaning up old files on my server I came across several old college projects. One of them contained several Java applets for fractal visualization — circa 2002. While I cringed slightly at the writing I was impressed with the mathematics that I have since forgotten! Like, what the hell is IFS and why am I calling it non-deterministic? Apparently, it’s a random iteration algorithm where I assign probabilities to each function in an IFS (Iterative Function System). An IFS is a set of affine transformations, and in this case each transformation is assigned a probability– Obviously!
There’s also some classic Mandelbrot and Julia Sets. Since several years have passed since this was all created, I’m interested in doing similar visualizations and not using Java. Java never really did catch on for client-side web applications. Although I do like the idea of doing the number crunching on the client side :) Maybe I’ll try it in Flash…
If interested, here is what is left of the old fractal page:



