Hacker News new | ask | show | jobs
by Jtsummers 1008 days ago
> You have to do leetcode if you want any hope of being able to find a different job. You have to be ready for that kind of question.

I disagree with that statement. You don't have to do leetcode, though it can be helpful if they have leetcode-style problems to get timed practice (not everyone is good at the 30-minute challenge under pressure; for some reason my general test anxiety, which interviews can bring out, doesn't apply to code tasks).

Learning and maintaining the fundamentals is sufficient (again, modulo the benefit of timed practice). Even for your problem, there are only three places that an action can be applied to a node when traversing a binary tree: before traversing the children, between traversing the children, and after traversing the children. The traversal code is otherwise the same (this also applies generally to any tree structure, it's not just a binary tree thing though "in-order" is ambiguous when you have more than two child nodes).

  def traverse(node):
    if not node: return # alternatively, this test can be applied to left/right before the recursive calls but requires also testing before the initial call
    # pre-order
    traverse(node.left)
    # in-order
    traverse(node.right)
    # post-order
I've named them here, but those are the three positions you can put a print statement. Even if you don't have the time to work out which one is which, you only have three things to try. An important aspect of focusing on the fundamentals is recognizing why this traversal code is the same regardless of where the action will be placed: You're traversing a recursive data structure (`Node = None | (data: T, left: Node, right: Node)`), that in itself determines the fundamental recursive structure of the traversal code. This same approach can work with any recursive data type which mitigates the need to memorize any particular algorithm on specific recursive data types.
1 comments

You still need to practice, you won't be getting easy questions like that all the time. There are tricks you need to know to solve many others.