Live data from Hacker News

Inverting Binary Trees Considered Harmful

jasq.org

221–225 of 225 posts

Re: Inverting Binary Trees Considered Harmful

#221
post #208

Earlier quoted context omitted.

I also drew a complete blank at an interview in a coding problem... and then went home an implemented it (taking quite a bit more time than I'm comfortable admitting ;-) ). Interviewing is hard (from both sides). Sometimes it just isn't your day. Sometimes it just isn't your job. I was quite depressed when I screwed up that interview because it was the first one I had done after taking 5 years away from programming p…

Unit test are not just for people who write bugs (parent comment), a good start would be to test for the 9th row the problem was asking for. Since you can't test all possible values a representative sample would be good enough. Just to be pedantic: It's not TDD if he already wrote the functional code ;)

I think that's the point, though. If I were to build this TDD, I would probably make use of the fact that that the value at any point is the sum of the values above it, in the previous row.

I could write a failing test that PT(1) = [1]. I could make it go green easily, and then write a failing test that PT(2) = [1,2,1] and make that pass, using my algorithm. Depending on how much you did in the last step, there may be no more failing tests that you could write. I can imagine maybe one more failing test, but after that you are done.

That's what I meant by "and then there is magic". The "test driven" doesn't really help the "design" at all. Your first test is the trivial case and the second test is "did it work?" I could arguably skip the first test altogether, and whether or not I wrote the second test before I wrote the production code is completely irrelevant because the test doesn't help you write the code.

Now, you might want to "refactor" this to be:

PT(n) = (n.choose(i) for i in [0..n-1])

(This is just pseudo code for a list comprehension that returns an array of n choose i for all the values of i from 0 to n-1). I could then write the code for choose(), but I'm in the same boat -- there is no benefit to writing choose() TDD because your first test will be trivial and you r second test will require the full answer.

While my "refactor" could use the tests I wrote with my "TDD" of PT(), I haven't demonstrated the value of TDD at all because the "refactor" is not related to the tests. I could just have easily written that code first and then written a test to see if it was correct.

Finally, if a bug is introduced to the code, the tests may pick it up, but are almost certain not to shine light on what went wrong because the tests are not related to the design of the code.

This is an excellent example of code that I would not write TDD (and I write 99% of everything TDD). I might not even write it test first depending on my mood.

Trying to be slightly diplomatic, the point of my post was that in all likelihood, a person who gives you this programming problem to demonstrate your TDD abilities has no clue about TDD. The alternative explanation is that they intentionally picked a problem that doesn't work well for TDD and wanted the person to explain why this was a sucky problem.

If you weren't very familiar with TDD, I could imagine this tripping you up pretty badly. You'd think, "what the hell test am I going to write?" As you point out, it doesn't really matter -- you can write any test that will sort of test that your code can grossly come up with the right answer. After that there's not actually anything beneficial you can do.

Re: Inverting Binary Trees Considered Harmful

#223
post #11

Yeah I remember the Google interview a few years back. I did it mostly for fun, and because their recruiters kept contacting me. So there we were, building a tree out of a forest of existing smaller trees or such. That was after they couldn't find my resume, and used a 5 year old one they found some place. They seemed annoyed and tired. By that time I felt nothing short of me proving P!=NP would have help changed the…

Yes, the thing I hated the most about my Google interview experience was that there was no discussion whatsoever of my previous projects. But there was the question "What would you like to do?" and I realized later that I should have hijacked that question to talk about my past projects I enjoyed the most.

Re: Inverting Binary Trees Considered Harmful

#224
post #60

I'll be the contrarian and claim that inverting a binary tree is a perfectly reasonable interview question. (Although not anymore now that it's famous). It combines basic background knowledge (what is a binary tree and how is it structured?), ability to reason logically (binary tree algorithms are often recursive, "inverting" a tree with no children is a no-op, otherwise you want to swap the children, and the childre…

In your daily programming activities, how many times do you have to implement a binary tree? I'm genuinely interested.

I work a pretty regular dev job (database analytics/reporting tool essentially). I of course never have to _invert_ binary trees, that's a pretty artificial example, but I had to write a method for pretty-printing tree structures for debugging something just this week. This is a good question.
Post reply on HN