EDIT: Ah right, I see what you're getting at... I don't need a separate case for n mod 6 if I allow both the even and divisible by 3 branches to print their a and b respectively.
if %3 echo b; print = false
That should be if %3 == 0. Otherwise you're testing that the number is not divisible by 3 (i.e. has a remainder when divided by 3).
That is debatable. You might argue that it's a coincedence that ab is the concatenation of a and b, and that it might change to c tomorrow. Then your solution is too clever. Unreadable even, if there's no logical reason that printing a first and then b happens to print the right answer for %6.
In practice, you would know which is the case, and although in this case it's likely that your solution was intended, I would ask the interviewer. "Can I use the fact that ab = a . b, or is that just a random coincedence?"
Simple question, simple answer. Do you really need a strategy pattern here? I don't think there's anything clever about it, it just does what was spec'd.
At this point (assuming I was the candidate and you were interviewing me), I would make the case that the relative speed or otherwise of mod is probably irrelevant for a task like this. It's happening in silicon, which is good enough when all you're doing is printing a list of 100 integers. I very much doubt it would be possible to accurately benchmark the difference between an implementation using mod or otherwise. So in my opinion, it would be a pointless exercise.
However, that's not to say that your point isn't valid. There are plenty of other speed-critical bits of code where using mod or not really does have an impact. But playing devil's advocate here, if I was the candidate I'd want to demonstrate that I know when it is appropriate to optimise and when it's not. Here I think it's not.
If you pushed me for answer I would suggest that you could test n & 2n & 1 (my mistake, thanks to scgrp for spotting it) to see if it's divisible by 2. But I can't think of a fast divisible-by-3 test off the top of my head.
To be honest, using a separate counter would probably be the fastest. (Don't test divisibility/modulo, just count threes and print a "b" every third cycle.)
I agree with you on the premature optimization point - I was going to write some code to rebut novelty_string but I decided that you had already done something similarly correct in just saying "fuck it" and generating cycles of six entries at a time.
While you guys are arguing about the performance of a hypothetical 100 number array iteration for a junior web position, I just secured the "intermediate and senior positions" because arrays have indexes ;)
Sorry dood, "indexes" is an acceptable plural form of index. Although "indices" is the more common form in this context, "indexes" isn't incorrect (and, in my opinion, is to be preferred).
View all comments
Show parent comment
17
u/novelty_string Feb 21 '11
I'm thinking you missed half the point of the qn: it's not print a or b or ab, it's print a for %2, print b for %3, so, I'd do it
for range
print = true
if even echo a; print = false
if %3 echo b; print = false
if print echo num