Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You may well be thinking, "Can it really be that hard to figure out how to open stdin?" Having just attempted it, I can confirm that it's hard. Shockingly hard. I could have easily failed that test.

I consider myself a fairly competent programmer, and I have a little knowledge of Java, including building GUIs and so on. Here's the Hello World program off the top of my head:

    public class HelloWorld {
      public static void main(String[] args) {
        System.out.println("Hello World.");
      }
    }
So to answer the interview question, I just need to figure out how to open stdin. At a guess, maybe something like System.in.readln will do it? Let's look in the class libraries.

Bingo! I've found System.in, and it says it's an InputStream. So now I just need to find ... ah ... there are three of them. Is System.in a java.io.InputStream, org.omg.CORBA_2_3.portable.InputStream, or org.omg.CORBA.portable.InputStream? The documentation doesn't say.

Well, I have a vague idea of what CORBA is, and I don't think I'm using CORBA here, so it must be java.io. Good first problem solved. Now, what methods can I use?

    abstract int System.in.read()
    int System.in.read(byte [] b)
    int System.in.read(byte [] b, int off, int len)
 
These methods just read bytes! I want a String, or at least a char. And Java uses multibyte characters. Can I get away with casting bytes to chars? I don't know. Maybe it was one of the CORBA methods after all? Or maybe ... I'm stuck!

I figured it out in the end, but only through pure luck: I need to wrap System.in in a java.io.InputStreamReader.



> "Can it really be that hard to figure out how to open stdin?"

? In most runtimes stdin is opened before you start. Certainly is true in the case of Java.

> So to answer the interview question, all I need to do is open stdin.

No, you also need to "open" stdout. More importantly, you have to figure out how to read and write to streams without losing data (which you'd think would be easy but evidence indicates otherwise).


Sorry, I'm getting the terminology wrong. I mean "read from".




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: