Forgot your password?
typodupeerror
Programming

tomhudson's Journal: Sample programming test question. 5

Journal by tomhudson

One of my former co-workers had 2 job interviews yesterday. One of the test questions was the "write some php code that, for a sequence of numbers from 1 to 100, if it's divisible by 3, print "BING!", if it's divisible by 5, print "BANG", but if it's divisible by both, print 'BOOM!"

These people can't come up with something more original?

Anyway, I woke up this morning, and said to myself - "How would *I* answer that question?" Obviously, in the interest of runtime efficiency, I don't want to use the modulus operator, or a for loop, or a switch statement, or a bunch of ifs ... so, with apologies for the weird formatting ...:

<?php /*
** the problem:
** for the numbers 1 through 100
** if the number is divisible by 3 but not 5, print BING! + a newline
** if the number is divisible by 5 but not 3, print BANG! + a newline
** if the number is divisible by both 3 and 5, print BOOM! + a newline
*/ /*
** my solution
** f*ck the foor loops and using the modulus operator - they're slow.
** the problem is easily solved by introspection.
** the natural period of the function is 15 (it repeats every 15 digits)
** the "interesting numbers" that trigger events: 3, 5, 6, 9, 10, 12, 15
*/
$a="BING!\n";
$b="BANG!\n";
$c="BOOM!\n";
$d="$a$b$a$a$b"; // 3, 5, 6, 9, 10
$e="$a$c"; // 12, 15
$f="$d$e"; // 3, 5, 6, 9, 10, 12, 15
$tro = "$f$f$f$f$f$f"; // 1 to 90
$ll=$d; // 3, 5, 6, 9, 10 - takes us from 91 to 100

echo "$tro$ll"; /* yes, I could have coded echo "$f$d";
** but that wouldn't have been as readable
** (for some values of "readable" :-)
*/

This discussion has been archived. No new comments can be posted.

Sample programming test question.

Comments Filter:
  • Obviously, in the interest of runtime efficiency, I don't want to use the modulus operator, or a for loop, or a switch statement, or a bunch of ifs ...

    Seems like mod + for loop would make for the quickest and easiest code. Why make something complex when there is no requirement to do such a thing? This could be a process that runs on its own machine in the middle of the night where those 'wasted' cpu cycles wouldn't matter...
    • by Qzukk (229616)

      I give it points for creativity, amusement ($tro$ll) and insight, but subtract points for flexibility ("sequence" does not inherently imply "ascending order", though for the purpose of this test it probably did).

      The quoted problem does remind me of the kind of assignments I was given in high school Computer Science I. If this was the most the company expected out of people working there, I'd run the other way. When I was in college, I interviewed for a position with National Instrument's LabView group, an

      • but subtract points for flexibility ("sequence" does not inherently imply "ascending order", though for the purpose of this test it probably did).

        Clearly, the answer is to write a script that writes scripts of the sort.

      • The killer is the unwritten part two of the question: "Now patch your program to write Bing for multiples of seven, Bang for multiples of eleven, and Boom for multiples of thirteen across the range 1 to 1000."

I'd rather just believe that it's done by little elves running around.

Working...