3 ms·
I never looked at the memcached source, and I'm not even an especially experienced C programmer, but I'm pretty sure I can do it in well under 3 hours. 1. grep
by arp242 3y ago
I never looked at the memcached source, and I'm not even an especially experienced C programmer, but I'm pretty sure I can do it in well under 3 hours.
1. grep -r '"append"' to find where it does "switch (cmd) { .. }" – append seems reasonably unique, but maybe there's a better command.
2. Modify switch to recognize "mult" command.
3. Find "append" function that does the work; copy/paste, modify to do multiplication.
4. Converting two strings to a number, multiplying the result, and returning the result as a string is a fairly trivial function.
Unless the memcached source code is hugely surprising, I might be able to get a first working version in 15 minutes or so on a good day if I don't mess up, maybe double on an average day. Add 15 more minutes to build memcached and read the question, and maybe a further 15 minutes of making it nicer/leeway.
A 3 hour limit is long enough so that no one reasonable will stress about running out of time, affecting the performance, and short enough that bozos can't easily trial/error it, ask on Stack Overflow, or whatnot.
Other than that, the stage matters a lot as well – is this the first interview question shotgunned to everyone who applies, the final one and you're hired if you pass, or something in between?
- ofrzeta 3y agoDon't forget to add a test (written in Perl). It's not required by the actual task description but it might give you an edge over the other applicants.
- JohnKemeny 3y agoYou also need to add tests as well as decide whether to break the existing API, or to create a new function in the API, in which case you need to design it. Do you add room for future support for division? Exponentiation? Knuth's up-arrow notation? Logarithms? What about integer factorization and discrete log? What about factorial? Sooo many choices. I can easily waste 3 hours in just 1 hour.
- arp242 3y agoNone of that is in the description. You are spectacularly over-engeering things. A test might be useful, but I took a quick peek at the source and it really is as straight-forward as I suspected it would be: the input is already tokenized and you can just add a new entry in a large, if .. else if tree, write a multiplication function, and that's pretty much it. You can just test it by started memcached and sending it commands. I'd whip up a quick patch, but the tar.gz is 14 years old and doesn't compile with latest clang without twiddling -W flags that don't seem to apply, and the git copy doesn't compile due to autoconf woes (sigh...) Messing around with autoconf is not my idea of fun and too much pain for a HN comment, so whatever. But you really don't need 3 hours if you vaguely know what you're doing and are a competent C programmer (which I am only barely).
- JohnKemeny 3y agoI was obviously just kidding with the other operations, but the API issue is relevant.
- arp242 3y agoRight so – that wasn't clear to me. You'd be surprised how much some people will over-engineer some interview questions like that (even when you very explicitly tell them to use the simplest thing possible). The memcached protocol really is as simple as "CMD [params]", all of which is parsed for you, and this API is just "MULT num1 num2". There are no tricky bits or hidden complexities here as far as I can see.
- JohnKemeny 3y agoYes, the protocol is obvious, but you need to look at `do_add_delta`.
- yobbo 3y agoA little hint: multiplication and addition are associative which is why they make sense as atomic operations. (Although allowing both multiplication and addition in the protocol breaks this ...) If you read the introduction and then spend extra hours doing "other things" (that are bad), it could count against you.
- resolutebat 3y agoThat's pretty much what the original poster does in Part 2, although inevitably the actual source code is a little more complicated than that. https://quuxplusone.github.io/blog/2022/01/07/memcached-interview-solution/ https://quuxplusone.github.io/blog/2022/01/07/memcached-inte...