4 ms·
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
by brudgers 10y ago
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.
- fiedzia 10y ago> An experienced programmer might look at the problem and choose a better tool: a different language Come on, you are not going to call Haskell or Python to flatten a list in your Java. > or call a service I know we have "micro" services now, but really? Sending serialized data to ListFlatteningService to get deserialized FlattenedList? > or rewrite the offending code Something may produce it because it makes sense it its context, while it doesn't for you. No offence here.
- deleted 10y ago[deleted]
- brudgers 10y agoI make no claim to being a good programmer or having high potential (of any sort but the wasted), just the ability to occasionaly mimic those who are. Charged with flattening lists, I'd write my Java in Clojure. (flatten [1 [[2] [[3 4] [5 [6]]]]]) as much of it as I could because it would be less work and easier to read and maintain and debug. Since nested Java lists are isomorphic with trees, there's more than one way to deserialize them: inorder, preorder and postorder. Flattening deserializes in preorder. Expecting different consumers of the nested list to deserialize it in different ways might be a reason that producing a nested list makes sense in a particular context. Once we start talking about "consumers of the nested list" we are using the language of services. That, for better or worse, is a road that currently tends to lead to micro-services.
- pcmonk 10y agoAren't list-of-lists more like trees where values are only stored on the leaves? In this case, aren't {pre,in,post}order all the same?
- brudgers 10y agoThe example input in the article is: [1,[2,3], [4, [5,6]]] It happens to be representable as a binary tree. Indeterminacy regarding whether or not that is accidental or intentional is due to underspecification of the interview problem. The first time I read a description of Linkedin using logs as the fundamental data structure (for what turns out to be Kafka I later learned) I had an epiphany that there is no such thing as a generic simple data structure living in the wild because data is data because it has semantics, without semantics it is just noise. To me, it looks like the root node is |1|.
- pcmonk 10y agoTo me, "nested list structure" means list-of-lists, which implies a certain natural tree representation, where all elements in a list are siblings. But maybe I've just been poisoned by Lisp, which tries to use lists as the elusive generic simple data structure. (OT: https://en.wikipedia.org/wiki/List_of_lists_of_lists https://en.wikipedia.org/wiki/List_of_lists_of_lists)
- cestith 10y agoFor the trivial case in which the nesting always gets deeper at the end like in the OP, perhaps. Take a look at nodes and leaves from my Perl5 example elsewhere in the thread. my $nested = [ 1, [ 2, 3 ], [ 4, [ 5, 6, [ 7, [ 8, [ 9, 10 ] ], 11, 12, [ 13, [ 14, [ 15, 16 ], 17 ], 18 ], 19 ], 20 ] ] ]; What the code does when there are leaves and nodes intermixed at arbitrary depths in arbitrary order from left to right determines the final order of your leaves. Are you pre-flattening all the more deeply nested lists and then building your flat list, or are you walking along a certain nesting level and deferring anything that's not a leaf to later flattening? I gave two examples. One produces a flattened list like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 The other products this: 1 2 3 4 5 6 20 7 11 12 19 8 13 18 9 10 14 17 15 16 Are either of those outside the specification set forth?
- deleted 10y ago[deleted]
- LgWoodenBadger 10y agoIdiomatic Java hasn't preferred Arrays since the advent of the Collections API back in 1.2. Idiomatic Java as of 1.8 now has flatMap on Streams, which all Collection implementations provide.
- brudgers 10y agoI don't disagree. I was thinking about how Java is typically taught in computer science contexts where Fortran in any language is not uncommon because that context seemed the relevant one to nest inside the context of junior programmers using Java. One of the difficulties of learning Java is that because it has been around so long, old idioms live on on the internet. For example, the Java examples of flatteing a list (which I recently looked at) do not include flatmapping a stream. It does not surprise me that it is there since I believe Java is an incredible and sophisticated language.
- dwc 10y agoThis wasn't stated as a test for Java programmers. Rather that most programmers chose to implement in Java because they were familiar with it. And you have to ask, if they were familiar with Java then why didn't they choose one of the idiomatic approaches you suggested? I mean, the "list" part is not an internal requirement so you could easily build a tree/node structure on input...
- brudgers 10y agoSee my answer to the sibling to your comment.