News for hackers. Stuff that matters.
Re:Invert a binary tree on LeetCode by de_Selby on Saturday June 13, 2015 @06:27AM (#9710552)
The problem wasn't to just reverse the nodes, it was to re-sort it:

> @rogerdai16 to min-max the tree, ascending to descending.

https://twitter.com/mxcl/status/608891015945170944

Re:Invert a binary tree on LeetCode by SamReidHughes on Saturday June 13, 2015 @06:52AM (#9710605)
I can't stand this. That's an equally intelligible description of the problem as the original.
Re:Invert a binary tree on LeetCode by de_Selby on Saturday June 13, 2015 @07:02AM (#9710638)
Well it's a 140 character description from someone who's probably feeling a bit emotional. I'm sure he got a clearer problem description in the actual interview.
Re:Invert a binary tree on LeetCode by falcolas on Saturday June 13, 2015 @08:59AM (#9710861)
I have to admit, the idea of swapping children on all of the nodes would not have come to me immediately as a solution for reversing the sorted tree - I have not been exposed to that kind of application before, and haven't spent a lot of time thinking about that kind of problem.

One more for the toolbox, I guess.

Re:Invert a binary tree on LeetCode by blahblah3 on Saturday June 13, 2015 @09:36AM (#9710954)
It sounds like the problem may have been to convert a min heap to a max heap?
Re:Invert a binary tree on LeetCode by mgraczyk on Saturday June 13, 2015 @06:21AM (#9710545)
In fairness to Google, the question is pretty darn simple...

    def invertTree(self, root):
        if root is not None:
          tmp = root.left
          root.left = self.invertTree(root.right)
          root.right = self.invertTree(tmp)
        
        return root
Re:Invert a binary tree on LeetCode by danieldk on Saturday June 13, 2015 @06:31AM (#9710563)
Indeed, it's not a difficult question.

I think people were making fun of Google, because it is a caricature of their whole interview process.

If a large number of your engineers is using Homebrew, having its developer is an asset for many different reasons (e.g. influencing the focus of the project, getting formulae in that interest you, or simply improving a tool that is important to your productivity). But no, they reject him because he cannot invert a binary tree.

It's like throwing one of the better offenders out of your soccer team, because he cannot substitute as a goalkeeper.

Re:Invert a binary tree on LeetCode by onion2k on Saturday June 13, 2015 @06:57AM (#9710619)
Having the developer of Homebrew as an employee is only an advantage if you want to influence it's development, and if the developer is happy for his application to be influenced by his employer rather than the Homebrew community as a whole.

In fact, I would see Google hiring the leaders of open source projects in order to influence the direction the project takes as a problem. Sponsoring an open source project is brilliant, and Google are great at that, but buying influence over it is completely different and (potentially) an issue that would lead to people forking the project.

Re:Invert a binary tree on LeetCode by r0naa on Saturday June 13, 2015 @07:19AM (#9710679)
If interviews are really made to maintain high-engineering standards and prevent bad hires, interviewing and rejecting the author of a software that 90% of your employees use everyday is quite perplexing indeed.

While I understand that interviews are sometimes necessary to evaluate a candidate, it seems completely out of touch for Google to pass on an applicant with such an impressive background.

Not only that, but heaven knows that it takes a lot of dedication and leadership to lead an open source project from ideation to wide-spread use. If I were a startup founder, I would kill to get someone like him on my team.

Re:Invert a binary tree on LeetCode by wfunction on Saturday June 13, 2015 @06:37AM (#9710572)
It's more like not letting the CEO of your soccer ball manufacturer play on your team because he can't kick a soccer ball.
Re:Invert a binary tree on LeetCode by ScottWhigham on Saturday June 13, 2015 @07:50AM (#9710735)
I don't see how this is relevant - in this case, a coder was rejected for a coding position. If we substitute your case, you could re-state it as "A player was rejected for defender" (or attacker/etc). In your example, you've changed that to "A manager was rejected for defender". Not the same.
Re:Invert a binary tree on LeetCode by crdoconnor on Saturday June 13, 2015 @07:19AM (#9710681)
>I think people were making fun of Google, because it is a caricature of their whole interview process.

To be honest, it kind of makes me wonder about the kind of code Google has, if this is a measure of a good coder. Are low level algorithms haphazardly re-implemented in python everywhere there is a need for one?

I've seen lots of code like that before. I was very unimpressed.

Re:Invert a binary tree on LeetCode by sltkr on Saturday June 13, 2015 @07:09AM (#9710659)
Btw, Python has nifty multiple assignment, so you can write the swap simply as:

    root.left, root.right = (self.invertTree(root.right),
                             self.invertTree(root.left))
(Parentheses added to break over two lines.)
Re:Invert a binary tree on LeetCode by Freaky on Saturday June 13, 2015 @07:28AM (#9710699)
Same in Ruby, but it doesn't need the parens to cross lines:

   root.left, root.right = invert_tree(root.right),
                           invert_tree(root.left)
Re:Invert a binary tree on LeetCode by TheLoneWolfling on Saturday June 13, 2015 @07:39PM (#9713178)
I don't like that. I see it as potentially causing more far too many potential problems for such a niece feature.

Especially as Ruby has automatic unpacking of function returns, yes?

So something along the lines of:

    a,b = function_returning_two_things(),
    function_returning_one_thing()
could cause bugs.
Re:Invert a binary tree on LeetCode by Freaky on Monday June 15, 2015 @08:13PM (#9723063)
That's certainly possible - this would get you 'a' containing an array from the first function and 'b' with the result of the last.

Can't off the top of my head recall actually seeing such a bug. I totally appreciate the desire to reduce the potential for errors from typos, but that ship has kind of already sailed with either language. I guess it just depends how far you're willing to let it go.

Yay choice. Testing will catch it anyway, right?

:/

Re:Invert a binary tree on LeetCode by aidos on Saturday June 13, 2015 @07:13AM (#9710666)
That was my immediate thought too. It's nice to think of those 2 operations as an atomic unit.
Re:Invert a binary tree on LeetCode by alexchamberlain on Saturday June 13, 2015 @06:48AM (#9710594)
Very interesting that the 3 answers here are 2 Python and 1 C++; I feared the world had descended into a JS downward spiral, but clearly not.
Re:Invert a binary tree on LeetCode by TN1ck on Saturday June 13, 2015 @06:55AM (#9710613)
I am your fear:

var invertTree = function(root) {

    if (!root) {
        return root;
    }
    
    var left  = root.left;
    var right = root.right;

    root.right = invertTree(left);
    root.left  = invertTree(right);
    
    return root;
    
};
Re:Invert a binary tree on LeetCode by andresmanz on Saturday June 13, 2015 @08:47AM (#9710837)
That was my fear, too. And I also will go with C++ first.
Re:Invert a binary tree on LeetCode by api on Saturday June 13, 2015 @07:07AM (#9710656)
It's not the difficulty people are whacking, but the practice of white board coding.
Re:Invert a binary tree on LeetCode by tjscanlon on Saturday June 13, 2015 @06:36AM (#9710570)
Iterative solution:

    def invertTree(self, root):
        queue = []
        if root is not None:
            queue.append(root)
        while queue:
            current = queue.pop()
            current.left, current.right = current.right, current.left
            if current.left is not None:
                queue.append(current.left)
            if current.right is not None:
                queue.append(current.right)
        return root
Re:Invert a binary tree on LeetCode by TheLoneWolfling on Saturday June 13, 2015 @11:14AM (#9711242)
Better solution:

    def invertTree(self, root):
        queue = []
        if root is not None:
            queue.append(root)
        while current is not None or len(queue) > 0:
            while current is None:
                current = queue.pop()
            current.left, current.right = current.right, current.left
            if current.left is not None:
                if current.right is not None:
                    queue.append(current.right)
                current = current.left
            else:
                current = current.right
        return root
No needless enqueuing / dequeuing of things unless you need to. As a bonus, if the nodes maintain a count of how many children the have overall you can always recurse on the smaller child first and use less memory.
Re:Invert a binary tree on LeetCode by cousin_it on Saturday June 13, 2015 @07:40AM (#9710719)
Any particular reason why you're using an array as a stack (LIFO) but calling it "queue" (FIFO)?
Re:Invert a binary tree on LeetCode by seivan on Saturday June 13, 2015 @06:43AM (#9710581)
Not sure why, but I'd feel iffy iterating on a list while mutating its content.

But I guess it's file while ignoring the order of the items, and just caring if it's containing items or empty.

Carry on :)

Re:Invert a binary tree on LeetCode by ICWiener on Saturday June 13, 2015 @07:06AM (#9710649)
A functional approach:

      (defun mirror (tree)
        (when tree
          (tree 
              (tree-data tree)
              (mirror (tree-right tree))
              (mirror (tree-left tree)))))
For completness, here are the definitions:

      (defpackage :trees (:use :cl)
                  (:shadow #:copy-tree))
      (in-package :trees)
      (defstruct (tree (:constructor tree (data &optional left right))
                       (:type list))
        data left right)

As well as a test case:

      (mirror (tree 4 
                    (tree 2
                          (tree 1)
                          (tree 3))
                    (tree 7
                          (tree 6)
                          (tree 9))))

      => (4 (7 (9 NIL NIL) (6 NIL NIL)) (2 (3 NIL NIL) (1 NIL NIL)))
Re:Invert a binary tree on LeetCode by meggar on Saturday June 13, 2015 @08:03AM (#9710771)
This looks like it works

    <html>
    <style>
    pre {
        -webkit-transform:scaleX(-1);
        -moz-transform:scaleX(-1);
        -ms-transform:scaleX(-1);
        -o-transform:scaleX(-1);
        transform:scaleX(-1);
    }
    </style>
    <pre>
         4
       /   \
      2     7
     / \   / \
    1   3 6   9
    </pre>
    </html>
Re:Invert a binary tree on LeetCode by diminish on Saturday June 13, 2015 @06:40AM (#9710578)
What's hard is to have necessary CS background to understand what "inverting a tree" as a term means. As a non-CS major, I don't know what they use it for as a "term" and it evokes several things I can do with a tree as an outsider. So interviewers can quickly eliminate me as a non-CS. How to invert a tree on a whiteboard? Use a mirror.
Re:Invert a binary tree on LeetCode by logicchains on Saturday June 13, 2015 @06:50AM (#9710599)
I like the solution a friend suggested: flip the interviewer upside-down.
Re:Invert a binary tree on LeetCode by vishnuks on Saturday June 13, 2015 @07:54AM (#9710745)
I would go for flipping the White Board.
Re:Invert a binary tree on LeetCode by SamReidHughes on Saturday June 13, 2015 @07:02AM (#9710639)
I don't think anybody was told "invert a binary tree" in the interview room.
Re:Invert a binary tree on LeetCode by adnanh on Saturday June 13, 2015 @07:29AM (#9710701)
Re:Invert a binary tree on LeetCode by SamReidHughes on Saturday June 13, 2015 @07:35AM (#9710710)
And... nowhere there is there any reason for you to think the problem was posed as, "Could you invert a binary tree for me, please."
Re:Invert a binary tree on LeetCode by cousin_it on Saturday June 13, 2015 @07:52AM (#9710741)
At first I thought that he was given a slightly harder problem: given a binary tree and a particular leaf node of that tree, return another tree that's graph-isomorphic to the original, but has that node as the root. (You can visualize that as picking up the tree by a leaf node and shaking it, then reinterpreting the result as another tree.)
Re:Invert a binary tree on LeetCode by raverbashing on Saturday June 13, 2015 @06:44AM (#9710583)
Too bad the website requires you to sign up to run your solution.

But it's an interesting concept, I've had to use a similar one in the past for a job interview.

Re:Invert a binary tree on LeetCode by caf on Saturday June 13, 2015 @07:05AM (#9710647)
There's a bugmenot login that works.
Re:Invert a binary tree on LeetCode by djhworld on Saturday June 13, 2015 @07:22AM (#9710691)
I've enjoyed many of the responses on this topic, some people like to use it as a self congratulatory platform to express their position that the solution is trivial and "any engineer should be able to do it like me" with a piece of example code, while others use it as a soapbox to moan about the scourge of the tech interview process.
Re:Invert a binary tree on LeetCode by falcolas on Saturday June 13, 2015 @08:55AM (#9710849)
I've noticed that dichotomy as well. It seems to be splitting along culture lines - so one of these two camps would be right at home at Google, the other would be fairly miserable.

Of course, this creates its own issues related to creating and maintaining a mono-culture, but that's a topic for another thread.

Re:Invert a binary tree on LeetCode by AYBABTME on Saturday June 13, 2015 @06:36AM (#9710569)
This topic has spilled too much ink.
Re:Invert a binary tree on LeetCode by falcolas on Saturday June 13, 2015 @08:56AM (#9710852)
Its prevalence suggests that it's a pretty big issue that many interviewees face, and nobody has the answer on which is the best approach for hiring competent developers at scale.
Re:Invert a binary tree on LeetCode by ExpiredLink on Saturday June 13, 2015 @06:46AM (#9710588)
It's Google, it must be important!
Re:Invert a binary tree on LeetCode by ripitrust on Saturday June 13, 2015 @06:39AM (#9710575)
C++ Solution here :

TreeNode* invertTree(TreeNode* root) { if (root) { TreeNode* temp = root->left; root->left = invertTree(root->right); root->right = invertTree(temp); } return root; }

Re:Invert a binary tree on LeetCode by wfunction on Saturday June 13, 2015 @06:30AM (#9710557)
Considering the answer is 1 line, I doubt I would have hired him either...

    def inverse(t): return Node(inverse(t.r), t.v, inverse(t.l)) if t else t
or if you must do it in place, 3 lines...

    def invert(t):
        if t: (t.l, t.r) = (invert(t.r), t.v, invert(t.l))
        return t
Re:Invert a binary tree on LeetCode by ExpiredLink on Saturday June 13, 2015 @06:45AM (#9710587)
>> You have not signed in, cannot submit your code.</a>
Re:Invert a binary tree on LeetCode by aaggarwal on Saturday June 13, 2015 @06:58AM (#9710621)
You can use http://ideone.com, it doesn't require you to sign in to execute your code and allows almost every language.
Re:Invert a binary tree on LeetCode by adnanh on Saturday June 13, 2015 @07:39AM (#9710718)
My JS

  var invertTree = function(root) {
      if (!root) return null;

      root.right = [invertTree(root.left), root.left = invertTree(root.right)][0];

      return root;
  };
Re:Invert a binary tree on LeetCode by jbrooksuk on Saturday June 13, 2015 @06:58AM (#9710622)
As a non-CS major, what is inverting a binary tree used for? Google just keeps bringing me back to Max's tweet, or blog posts about it.
Re:Invert a binary tree on LeetCode by jbrooksuk on Saturday June 13, 2015 @07:03AM (#9710641)
I assume it's for sorting (and ordering) algorithms?
Re:Invert a binary tree on LeetCode by fishnchips on Saturday June 13, 2015 @09:22AM (#9710924)
This is just flogging a dead horse.