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.