If I were interviewing you, I’d turn the question around and ask
you to explain why you’d one over the other.
Again, seeking your ability think things through, not accuracy.
And if I would be the one interviewed, I’d tell the interviewer
that the solution:
Is much more readable than the solution:
s = “1113wf”; puts ‘%s%s’ % [‘x’*(10-s.length),s]
If the interviewer favours the 2nd then I tell him to stop
being stupid and use perl instead. That way he can worship
random syntax being a valid program.
rjust() is just perfect for this and perfectly well suited
for the task.
I do not think there is a robust way because there is no (s)printf
flag that can change the padding character for strings. Maybe that was
the answer the interviewer was fishing for.
Perhaps, we can never know. Perhaps the interviewer was an idiot too,
we can’t be certain. I am assuming the latter currently.
The thing here is that .rjust() is the best answer by far. Any other
answer, as long as the code stinks as much as the above, is a much
worse answer, and it seems stupid to want to reason in favour of
code that is longer and less elegant. If there is a shorter variant
than .rjust then let’s have at it - I would be all in favour of that!
There is more than one way to do it but there are also clearly
dumber ways to do it. And just because you can swim in the Atlantic
Ocean does not mean you should have to when you could use a plane
instead (and the water is damn cold and there are polar bears out
there to get you, at least towards the north pole; I always
remember that the penguins are only at the south pole, and
they are mutually exclusive!).
I ask the candidate how they would go about
implementing String#% and String#rjust. I wouldn’t
necessarily expect them to know how, exactly, but
what I’m interested in is their approach.
rjust is easy but how would you want to implement %
without using sprintf()? You would have to parse the
whole input string and re-format it according to the
special tokens given. That reminds me of C, so I am
wondering what kind of crazy interviews you guys are
doing? And, can I just copy paste the actual C code
anyway? Well I guess it would serve as an answer, so
I would have to look it up ONCE, then I am prepared to
answer such stupid questions…
I’d actually want REAL questions, REAL answers
AND then whether you did decide to hire that guy or not,
because I am beginning to believe that there are a lot
of fake questions out there for no real merit to have.
You don’t need to know the alternatives to build useful
and great programs - you think someone who knows how to
implement % from scratch is a better ruby programmer
than someone who spent 10 years writing ruby code,
wrote a few hundred documented gems but simply can’t
be bothered to want to know every little unimportant
I would be SIGNIFICANTLY more interested in asking
unspecific questions such as buildup to specific
programs, how a candidate works, how he will ensure
that his code will still work even when he is no
longer in that company, and so on and so forth. And
of course what he did already build so far and whether
he is pleased with his own code quality or not.