Live data from Hacker News

Why it’s hard for programmers to write a program to flatten a list?

shekhargulati.com

11–20 of 128 posts

Re: Why it’s hard for programmers to write a program to flatten a list?

#11

Couldn't you just do something like: def flatten(x): if isIterable(x): for y in x: yield from flatten(y) else: yield x Well, technically this is a generator, but it's easy enough to put its result in a list.

Sure, the point is that writing this requires knowing more than most candidates do.

Re: Why it’s hard for programmers to write a program to flatten a list?

#12
post #10

Couldn't you just do something like: def flatten(x): if isIterable(x): for y in x: yield from flatten(y) else: yield x Well, technically this is a generator, but it's easy enough to put its result in a list.

I found this on Stack Overflow a while back, and I've been using it in my Python code since then: [item for sublist in l for item in sublist]

That's not generic and would not qualify as valid answer for this question.

Re: Why it’s hard for programmers to write a program to flatten a list?

#13
> I am not sure what makes this problem tough for candidates.

Most people aren't programmers, and sad to say, most people employed writing code are not programmers.

I find trivial problems like this are good interview questions. What the person asks, says and does tells me a lot about what they would be like to work with. And it's not like you're using some bizarre datastructure or obscure CS case: this is an ordinary workaday problem. So it's not a "trick" or one-upsmanship, just "do you know your tools?"

Re: Why it’s hard for programmers to write a program to flatten a list?

#14
post #8

The word "Java" really should be in the title. Whatever else you say about it, Java is one of the entry-level languages. It's no wonder there are many entry-level programmers among its users. It's obviously a trade-off, as you get that many more candidates to choose from, compared to for example OCaml, Clojure or Erlang programmers. On the other hand, a percentage of people who can flatten a list is greater in users…

One of the problems is that for Java the task is underspecified: Flatten a list of what? Sure this one is integers, is that always the case or should the list take ?

Leaving out that information from the question is going to disorient novices who in a high stress situation at the short end of an asymmetrical relationship. I suppose if the interviewer lets them Google, then it is a fair test (and objectively, not Googling before writing code is probably the best criterion for failing miserably).

What the question really measures is how lucky the candidate was in their education. If they were taught in Racket rather than Java:

  (flatten '((1) ((2 3 ((4) (5 6)))))
and if they were taught the JVM rather than Java

  (flatten [[1] [[2 3 [[4] [5 6]]]]]
and with J

  flatten =: [: ; 
I'd bet that the interviewer would not know if it was wrong or right. Which is the same issue of open endedness that the candidate faces.

Re: Why it’s hard for programmers to write a program to flatten a list?

#15
post #8

The word "Java" really should be in the title. Whatever else you say about it, Java is one of the entry-level languages. It's no wonder there are many entry-level programmers among its users. It's obviously a trade-off, as you get that many more candidates to choose from, compared to for example OCaml, Clojure or Erlang programmers. On the other hand, a percentage of people who can flatten a list is greater in users…

But...but Python is starting to become a Programming 101 language, and Python is awesome :( :(

Re: Why it’s hard for programmers to write a program to flatten a list?

#16
post #10

Couldn't you just do something like: def flatten(x): if isIterable(x): for y in x: yield from flatten(y) else: yield x Well, technically this is a generator, but it's easy enough to put its result in a list.

I found this on Stack Overflow a while back, and I've been using it in my Python code since then: [item for sublist in l for item in sublist]

That wouldn't work in this case: it only works with lists where every element is also a list and it only flattens one level deep.

With these constraints, there are quite a few interesting techniques, for example in Scheme you can apply append:

    (apply append list-of-lists)
or in Python you can use reduce with add operator:

    reduce(op.add, list_of_lists) # op == operator module
And anyway, even if it's not the constrained version, the solution is indeed trivial, for example in Erlang:

    flatten(L) -> flatten(L, []).

    flatten([], Acc)            -> Acc; 
    flatten([[] | T], Acc)      -> flatten(T, Acc); 
    flatten([[_|_]=H | T], Acc) -> flatten(T, Acc ++ H);
    flatten([X | T], Acc)       -> flatten(T, Acc ++ [X]).
(please don't mind the inefficient use of ++, it's a toy example...)

It can get a bit more complex in a statically typed language, especially if you want to have static guarantees (no down-casting from Object), but it's still doable in a couple of minutes...

...is what this post - and discussion - is not about.

It's trivial once you get it, of course. But it's also true that it's almost impossible to figure it out on your own during a stressful interview if you didn't get it beforehand. It's so easy to forget that. We all once struggled with some concepts, and we all went through many discoveries of facts, techniques, and skills we didn't know existed, much less that we needed them.

What I want to say is that the beginners who fail to answer this question are not bad, they are just beginners. It's ok to reject them right now, but it's not ok to post an article suggesting that "programmers find it hard to solve trivial problems even with my help". It's not programmers, but beginner programmers; it's not trivial problem, just relatively widely known one; and it may well be the help was insufficient.

Re: Why it’s hard for programmers to write a program to flatten a list?

#17
post #4
post #2

I'm a retired data architect and C developer with 36 years of professional experience. In my opinion, this is not how you find an appropriate candidate. It would be better to ask substantive questions that are related to the thing you are making. What does this person bring to the team? How will the team receive this person? Has this person delivered something of substance in the recent past that would convince you t…

Those are questions for juniors. Most likely they had no previous job at all. The only thing they can bring is potential, and this question tests it rather well.

It does not really test potential because a junior trained in Java has poor tools for flattening a list because idiomatic Java would not represent hierarchical data structure as a nested list. Idiomatic Java would use tree and node objects. Idiomatic Java also prefers arrays over lists for sequential data.

An experienced programmer might look at the problem and choose a better tool: a different language or call a service or rewrite the offending code that produces a nested list or any one of a dozen things that are not bashing away with the hammer of Java.

Re: Why it’s hard for programmers to write a program to flatten a list?

#18
post #8

The word "Java" really should be in the title. Whatever else you say about it, Java is one of the entry-level languages. It's no wonder there are many entry-level programmers among its users. It's obviously a trade-off, as you get that many more candidates to choose from, compared to for example OCaml, Clojure or Erlang programmers. On the other hand, a percentage of people who can flatten a list is greater in users…

One of the problems is that for Java the task is underspecified: Flatten a list of what ? Sure this one is integers, is that always the case or should the list take ? Leaving out that information from the question is going to disorient novices who in a high stress situation at the short end of an asymmetrical relationship. I suppose if the interviewer lets them Google, then it is a fair test (and objectively, not Goo…

> is going to disorient novices who in a high stress situation at the short end of an asymmetrical relationship.

> What the question really measures is how lucky the candidate was in their education.

Yes! Exactly! This is the other point I wanted to make[1], thanks for doing it for me :)

> flatten =: [: ; Nice, I was playing with J quite a lot but somehow missed the spread word (for the curious: http://code.jsoftware.com/wiki/Vocabulary/scapco)

[1] In my comment here: https://news.ycombinator.com/item?id=13725095

Re: Why it’s hard for programmers to write a program to flatten a list?

#20

Earlier quoted context omitted.

One of the problems is that for Java the task is underspecified: Flatten a list of what ? Sure this one is integers, is that always the case or should the list take ? Leaving out that information from the question is going to disorient novices who in a high stress situation at the short end of an asymmetrical relationship. I suppose if the interviewer lets them Google, then it is a fair test (and objectively, not Goo…

> is going to disorient novices who in a high stress situation at the short end of an asymmetrical relationship. > What the question really measures is how lucky the candidate was in their education. Yes! Exactly! This is the other point I wanted to make[1], thanks for doing it for me :) > flatten =: [: ; Nice, I was playing with J quite a lot but somehow missed the spread word (for the curious: http://code.jsoftware…

I played with J and have it loaded on an old Android phone...but I copy pasted the code from Rosetta Code after Googling 'flatten a list in Java' which is where I became aware of the issue {racket I knew and clojure I suspected}.
Post reply on HN