4 ms·
> In my previous company we used ask candidates to code a very simple task: reverse a string in Java, on the whiteboard or on paper. It is a trivial task but ma
by ashark 10y ago
> In my previous company we used ask candidates to code a very simple task: reverse a string in Java, on the whiteboard or on paper. It is a trivial task but many seasoned people failed it to the extent that we thought "you don't really know Java, do you?".
I've written quite a bit of Java in the last year (though not in the last couple months), and a fair bit before that. I'd have a low probability of giving you a correct answer to that question without references—I have several possible approaches but I'm not 100% certain which would work without checking. Can I treat Java strings as arrays? Or is there some kind of toArray thing I need to do first? I think so but I'd have to check to be certain. Is there a string-reversing util method somewhere? Quite possibly. How do I read it one byte (glyph? Ugh, I can't remember how Java strings work and I have Go on the brain, I think) at a time if the treat-as-an-array thing doesn't work? God, I don't remember. What's string length again? size, length? Maybe even len? Assuming I figure that out, was that a property or a method, now? Hell if I know, it's not like I've written more than a dozen lines of Java outside an IDE, ever, unlike some other languages. I suppose I can use split if I have to (it's just called "split" in java, right? I can't remember for certain, of course!)
I'm sure some people memorize this stuff. Plenty of us, I'm sure, if it's not something we use ALL. THE. TIME. rely very heavily on tools, context (i.e. cheating for syntax by looking at surrounding lines) and documentation. I haven't been doing tons of specifically string manipulation in java lately, so I'd likely be screwed on this question.
I dunno, I guess by that standard I know zero of the 7-8 languages I've been paid to write over the last fifteen years or so.
- orless 10y agoI'm not saying it's a good task, I'm saying that people failed it despite we were thinking the task is trivial. But still, I'd expect Java developer to know that Java strings are immutable and that you can't treat them as character arrays. It's totally OK to not know how toArray method is called (toCharArray). Assuming there's a kind of "reverse" method is a bad idea - even if there would have been one, it would clearly be not what we ask. Same for "I'd use StringUtils.reverse(...)" from whatever package. I won't mind if you write a.length() for array or a.length for String, this does not matter much. Knowing a difference between a byte and a char is, however, quite important in my book. The "usual" solution is to str.toCharArray() and then iterate this array to the middle, swapping i-th and (length - i - 1)-th characters, then new String(array) with the result. For this solution you only need to know fundamentals of the language and vaguely recall that there was some method to convert string to a character array. Bonus points (figuratively speaking, there were no actual points) were for: writing test cases, checking the argument for not being null or showing how to swap two characters without using an additional variable. Do you think IDE would help much? I don't think so. IDE would have helped you with the unimportant part (how was that toArray() method called exactly). But you'd need to know how to use a cycle to reverse an array. You need to know what the difference between a byte and a char is (in order not to choose the getBytes() method instead toCharArray()).
- ashark 10y ago> Do you think IDE would help much? I don't think so. It'd help quite a bit with the parts that make writing this in Java different from talking through a pseudocode implementation, yeah. Or just let them write pseudocode instead, since you've written you don't care much about the details (aside from knowing you need to take care when manipulating strings that you're operating on the correct unit in order not to turn it into gibberish, which is a universal detail, not specific to Java). Bonus to that approach: you haven't just made your candidate incredibly self conscious over and distracted by things you apparently didn't care about to begin with, and there's also a lower chance they'll try to correctly use the language in ways you don't like and expected them to guess you don't like (e.g. using a single utility method that solves the problem immediately).
- orless 10y agoWe've tried it with pseudocode. People wrote Java.
- watwut 10y agoThe IDE would make huge difference for me and this question. It would allow me to instantly know string api which I normally don't need. Also, it would add confidence that what I am doing is actually java. I don't mind whiteboard in general, however expecting me to solve language specific question like this is weird - especially in language like java that is never written without IDE.
- orless 10y agoWell, yes, in an interview for a Java dev position I'd expect the candidate to be able to write code (whiteboard/paper) which at least remotely resembles Java syntax, is logically correct and does not break universal things like byte vs. char.