Live data from Hacker News

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

shekhargulati.com

81–90 of 128 posts

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

#81

Earlier quoted context omitted.

Interfaces are (open) sum types. Or close enough for government work.

Yes, but Java (unless I missed a recent change) doesn't let you assign new interfaces to existing types, so short of either wrapping Lists and Ints in containers implementing a common interface and demanding that the input be in the wrapped form, that doesn't actually help.

Faced with this problem, I would precisely wrap Lists and Ints in containers implementing a common interface and demand that the input be in that form (actually, the input is text right? so I'd parse it into that form - assuming I couldn't get away with just manipulating the textual representation).

Note that in the genuine sum-type case you're wrapping, too.

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

#82

Am I the only one who saw the string representation of the nested array and said "you could just walk it character by character and ignore the brackets and commas?"

Rubric item #10: "No one thinks about generic program so that solution will work across all types."

you fail.

(I don't intend to be mean to you, I intend to be mean to the question ;-) )

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

#83

> No one thinks about generic program so that solution will work across all types. Is this even possible to do generically in Java or C#? You can do it for objects so that it works for ALL types (i.e "object"), but you can't make it work generically for a type T for any T. That's why this is such an excellent example of why sum types really are useful. Writing the flatten interface without generics (Java List or c# I…

Yes you can code up sum types in the object system of Java or C#. But are you allowed to do that? The problem write-up makes it sound like you're supposed to be ingesting the interviewer's input. So, how did the interviewer code up sum types? With tons of nasty casting out of List , or with a slightly less terrible object hierarchy mimicking sum types? That's what makes the question terrible. It implies you're suppos…

Agree 100%.

Title of blog post could be "How do I keep failing to convey the basic constraints and requirements of a programming interview question?"

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

#84
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

"Have you ever needed to flatten a list in your career outside exams and interviews?"

I had to write this function to flatten a tree of expressions, so I could sort them by precedence, etc and fold them into a tree. I already had a bunch of higher-order primitives for constructing a parser and that was basically a lazy hack for me to get expressions parsed. I eventually implemented a proper expression parser, that was basically the same thing, but produced a flat list from the beginning.

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

#85
post #34

Earlier quoted context omitted.

Doesn't stop people from asking these questions of people at all levels. I still get asked these coding questions pretty much every interview I have had, and I have almost a decade of programming experience, and have even been lead programmer on some projects (without the title). I've been asked a barrage of these questions for senior positions, even. Especially sucks because I don't usually program terribly fast or…

Have you ever needed to flatten a list in your career outside exams and interviews?

I mean, I once wrote a python script called unearthed.py that took all the files in all subfolders and put them into the parent folder, then deleted the subfolders, but I'm pretty sure I just used a simple os.walk for that.

Came in handy when working with rom archives that liked to put everything in its own folder, and a couple of times when I just had a bunch of crap that I didn't want to have to manually dig up anymore.

Other than that? No, not really.

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

#86

I fail to see how some people see this as a contrieved test. It's an issue many programmers have probably found at some point as opposed to fizzbuzz or other artificial tests. Probably other questions related to your business would also be good, but using this as a first and quick filter sounds about right IMO. In Javascript, depending on your context, it could be as easy as: // Only if we're using numbers or other v…

The complaint isn't that it's contrived in general. The complaint is that it's a bad question if you're interviewing Java developers (because Java's type system is impoverished, but that's really beside the point).

If you're going to ask this question of a Java developer, you should carefully indicate what form the input takes. Heterogeneous collections are canonical in dynamically typed languages, and also in reasonable typed languages as recursive sum types. But in Java you're stuck between awkward subtyping / dynamic casting, or coding up your own definition of a heterogeneous collections. And in particular, the wording of the question suggests List, which is terrible style in Java land.

The real take-away is that Java's type system stinks for this sort of thing, and the standard libraries don't offer much help. But if you're interviewing Java developers, you shouldn't start off by asking them to writing terribly non-idiomatic Java code.

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

#87
post #7

Some reasons: * Nobody required this from them before. * Recursion is rarely used to write business logic, most common thing programmers write and learn. Same for static methods and being generic. * Type checking is done by compiler for them, so no surprise that they don't know how to do it. * Relatively few people go beyond minimal, so adoption of is java 8 is low Overall, it looks like a good question to pick up th…

To me, this smells like: * Required before: I only ever code at work. * Recursion: I didn't get any/much theory in school * Type checking: I don't know my language very well. * Going beyond minimal: agreed- try not to hire them.

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

#88
post #59

Earlier quoted context omitted.

The coming glut of Python devs is going to be a problem. Its an excellent language, I've used it for work for years, but its almost too powerful. A minimally skilled programmer can write mostly-functional Python programs while still not having the slightest notion of what they're doing, meaning the pressure for a developer to understand and improve their craft is often minimal. If I were responsible for hiring develo…

I have seen a fair bit of Python code in the style of Java (as well as some completely unreadable Python). Just because you can be useful in Python doesn't mean you are writing good idiomatic Python code. And if you aren't doing that, then what is the point in using Python?

To be fair, I probably still have a bit of C-stink in my python code. I don't know the idiomatic way to do everything, and I don't try to put everything into list comprehensions by default, but I still think it's an excellent, and fast, language, and have been using it for many years.

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

#89
This is pretty easy actually: since the question is horribly vague on what the input type actually IS, I'm going to just assume it's a string.

So flatttening the list is just a string split on all delimiters. Done.

My Java is rusty but something like input.split("\[\],"). Done.

It's an answer of exactly the same quality as the question...

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

#90

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…

I suppose a fair counter question would be how on earth the interviewer ended up with a couple of nested heterogeneous lists in Java, instead of a simple Tree data structure.

If you look at the actual, original question it was presumably a textual serialization of a simple data structure.
Post reply on HN