4 ms·
I have a healthy love/hate relationship with Java - but that's the first thing I thought of too. Alternatively you could read into a byte[] buf and not have any
by thomaswk 13y ago
I have a healthy love/hate relationship with Java - but that's the first thing I thought of too. Alternatively you could read into a byte[] buf and not have any casting.
This is valid JDK 1 code. You only need to read the Javadoc for InputStream and PrintWriter. I think the author needs to check his biases.
Now Javascript... (just kidding).
- cbsmith 13y ago> I have a healthy love/hate relationship with Java - but that's the first thing I thought of too. Alternatively you could read into a byte[] buf and not have any casting. Okay, that kind of points to the problem. You don't see anything wrong with that code beyond the casting (and avoiding casting is the wrong reason to read in to a byte[].. which opens up some additional complexity too).
- thomaswk 13y agoSigh so half of an interview is to probe the candidate's knowledge and the other half is to understand their thought process. So my ideal impl in java would be: public class F { public static void main(String [] args) throws Exception { byte[] buf = new byte[256]; int len = -1; while ( (len = System.in.read(buf)) != -1) { System.out.write(buf, 0, len); } System.out.flush(); } } In python #!/usr/bin/env python -u import sys while True: buf = sys.stdin.read(256) if buf: sys.stdout.write(buf) else: break Which both feel roughly the same. Although I kind of wish python would let me assign within the expression - just in this case. The similarity boils down to the fact that they're sitting on top of posix read and write calls. There's not much variation you can get from that. The differences get down to fd stream flushing. Python we enable unbuffered io with -u, java we must flush. In C++ we can count on all FDs being flushed in atexit. You certainly can muck this problem up for binary files and different charsets if you're not careful. But the OP's solution still works in many cases. A fine first answer. Any time a candidate writes code for you during an interview, the first answer will most likely have issues (stress, trying to finish quickly to impress, etc). I care very little about that stuff if there answer is roughly functional. After they have their first stab out, then you go thru the 'why did you do it this way?' 'what corner cases might you be concerned about?' and so on. The answers to those questions will let you make a good estimate of their knowledge and competence.
- cbsmith 13y agoSo, my first follow up would be: why are you bothering to read in to a byte array, and why a 256 byte array? And to be clear... the correct answer is that in Java it basically buys you nothing but some extra overhead initializing an array and in Python it merely saves you trips through the repl loop. Either way the choice of 256 bytes is arbitrary.
- thomaswk 13y agoI'd phrase it in a nicer way in an interview, but on a forum: You're wrong. The explicit buffering afforded by read(byte[]) & write(byte[], off, len) vs. read() & write(int) saves demonstrable time as file size increases. 256 - yeah arbitrary :) tweak as necessary. on my junky MBP running java 6 - read(byte[]) & write(byte[], int, int): real 0m6.588s user 0m0.876s sys 0m1.406s vs. read() & write(int) real 0m16.836s user 0m10.624s sys 0m4.245s vs the mighty cat - < in > out: real 0m4.609s user 0m0.015s sys 0m0.458s For a 150 mb file. I'd say read(byte[]) / write(byte[], int, int) performed pretty well. Now some points. System.in is an InputStream, System.out is a PrintStream. Assuming anything more about their buffering nature is wrong. AFIAK there is nothing in the java spec that spells out any other explicit behaviour. So if you want to ensure your code works properly, code against the interfaces you're given, not the implementations you expect are sitting behind them. This question (and stdin/stdout/stderr questions in general) are heavily biased in favour of devs comfortable of working in a Unix-y environment. If you're fine with short changing people that use that other OS that's cool. But if you want a 'trivial' demonstration of programming skill - a FizzBuzz type of question is much less biased way of achieving that.
- cbsmith 13y agoOkay, I stand corrected that you can achieve some win due to doing buffering yourself. I guess it does depend on your implementation (though I'd think the JIT would eventually get around to eliminating most of that overhead you see), though you should still have buffering as per the platform's standard runtime, which I'd expect in most cases would be not insignificant. > So if you want to ensure your code works properly, code against the interfaces you're given, not the implementations you expect are sitting behind them. A test which the original code, and your initial review, failed on multiple fronts (failed to handle character encoding issues and failed to flush the output stream before exiting). > This question (and stdin/stdout/stderr questions in general) are heavily biased in favour of devs comfortable of working in a Unix-y environment. I honestly find it hard to believe that Java devs would have their skills sets be terribly different regardless of platform they run on, and I really would think people would learn the basics of working with streams regardless of the platform they work with, but I grok the gist of what you are saying. > But if you want a 'trivial' demonstration of programming skill - a FizzBuzz type of question is much less biased way of achieving that. FizzBuzz doesn't really test for knowledge of the language's libraries though. I always thought the purpose of these tests was more to validate that the programmer had successfully coded in the past, rather than any demonstration of skill.