I don't think i could type that off the top of my head. Sure, the normal java stuff public class Foo { ... blah blah blah ...}. Integer parsing with Integer.parseInt(...); do the sum in a loop, couple minutes maybe. But i have to look up references.
File because one of the constructors overwrites whatever is there there, and the Reader family because i can never remember what i need in the stack. I don't think i've had to open a file this year.
I guess it's better to just type in everything you know, then explain what you're looking up and why. But really at my desk, I'd instantly know what i needed to look up and go look that up before typing. Rather than local javadocs, i'd google for FileReader. But in an interview i'd feel bad.
Ah yes, and after actually doing it, i'm an idiot, and there's a nice convenience method that is perfect. Files.readAllLines.
Also, figuring out the package prefixes without an IDE kind of sucks.
Google has ruined me.
But yeah. stock linux + jvm and no internet connection? maybe 15 minutes? 5 minutes to figure out where the javadocs are, another 5 to find the docs. 5 for typing. Eh, maybe another 5 for remembering the weird path conventions for javac.
I'm kind of embarrassed it would take me that long.
edit
Oh gosh. you wouldn't have ctrl mapped to caps lock. I'd look like a complete moron.
Let's be fair, Java I/O -blows-. The whole stream, reader, buffered, file, blah blah blah, multiple levels of abstraction on top of the basic "Look, it's a file, let me read it", is what is getting you there.
Ideally if you chose Java, the person would let you handwave the actual library calls. Or you'd pick something saner. In Javascript, off the top of my head -
EDIT: Node, specifically, and with the spoken caveat that devoid of context of how it's being used, you're just going to let it block; were it a real application you'd use the async variant of readFile.
In C, in a handful of minutes (and with no reference):
#include <stdio.h>
int read_and_sum_ints(const char *filename, int n)
{
FILE *fd = fopen(filename, "r");
int c = getc(fd);
int sum = 0;
while (n-- > 0 && c) {
int m = 0;
while (c && c != '\n' && c != '\r')
m = (m*10 + (c - '0'));
sum += m;
}
fclose(fd);
return sum;
}
int main(int argc, char **argv)
{
print("%d\n", read_and_sum_ints("foo", 10));
return 0;
}
Given that I'm currently just an intern, I would expect both junior and senior developers to be able to do this.
I wouldn't be able to do that off the top of my head because, in my C experience, I hardly ever read files. Everything we did was reading from or writing to memory buffers.
Furthermore, we did almost all system interactions through a platform-independent middleware interface. So, while I, for example, knew what a semaphore was and how to use it I could not remember the standard Linux interface for semaphores because we used the platform-independent API for ours.
Expecting people to remember APIs off the top of their heads is ridiculous. Some do, some don't. The people who don't aren't lesser engineers just because they don't.
I agree people shouldn't have to remember APIs, but you should at least have a vague idea of roughly how to go about reading a file and parsing numbers from it.
As long as we're posting code/golfing (and since I've been lots of file parsing lately and it's fresh):
use std::io::prelude::*;
use std::fs::File;
fn main() {
let mut s = String::new();
File::open("numbers.txt")
.unwrap()
.read_to_string(&mut s);
let sum = s.lines()
.map(|l| l.parse::<i32>().unwrap())
.fold(0, |acc, i| acc+i);
println!("{}", sum);
}
The value of EOF is platform-specific, but is commonly -1. I'm fairly sure it's never 0.
But I was referring more to the fact that c is set exactly once in line 4 (`int c = getc(fd)`), outside of the nested loops, and never touched again. So if it's not initially a NULL, newline or CR, it never will be, and the inner loop will never exit.
I don't think i could type that off the top of my head. Sure, the normal java stuff public class Foo { ... blah blah blah ...}. Integer parsing with Integer.parseInt(...); do the sum in a loop, couple minutes maybe. But i have to look up references.
File because one of the constructors overwrites whatever is there there, and the Reader family because i can never remember what i need in the stack. I don't think i've had to open a file this year.
I guess it's better to just type in everything you know, then explain what you're looking up and why. But really at my desk, I'd instantly know what i needed to look up and go look that up before typing. Rather than local javadocs, i'd google for FileReader. But in an interview i'd feel bad.
Ah yes, and after actually doing it, i'm an idiot, and there's a nice convenience method that is perfect. Files.readAllLines.
Also, figuring out the package prefixes without an IDE kind of sucks.
Google has ruined me.
But yeah. stock linux + jvm and no internet connection? maybe 15 minutes? 5 minutes to figure out where the javadocs are, another 5 to find the docs. 5 for typing. Eh, maybe another 5 for remembering the weird path conventions for javac.
I'm kind of embarrassed it would take me that long.
edit
Oh gosh. you wouldn't have ctrl mapped to caps lock. I'd look like a complete moron.