Binary Trees, Recursion and Tail Call Optimization in Javascript
Use recursion to determine the height of a binary tree. You must figure out how to implement the binary tree in JavaScript.
We are covering each aspect of the job interviewing process. You have already seen some theoretical questions that demonstrate how well you can use JavaScript. You have seen some coding challenges that not only let you showcase your problem-solving abilities, but also demonstrate your theoretical knowledge and your algorithmic skills.
You are yet to experience some longer homework assignment type of tasks that challenge your abilities to write maintainable software. Some of these challenges are timed, some require you to use some frameworks or libraries, while others need you to structure your code.
The challenge I have chosen for this session is an online coding challenge on a site called HackerRank.
I have already recommended that you go through the challenges of a similar site called Codility. HackerRank ups the ante a bit more by giving you problems of continuously increasing difficulty.
Once you sign up, HackerRank recommends a thirty-day challenge for you. You get one exercise a day, which often takes just a couple of minutes. The thirty-day challenge is very healthy, because it builds a habit of coding just a bit every single day.
Consider the option of using challenges like the ones HackerRank provides to improve your problem-solving skills.
As an illustration, I will now solve a coding challenge that you can find in the Data Structures section of HackerRank. The problem is called Height of a Binary Tree.
For advanced positions, you will be expected to know some data structures and algorithms, as well as some programming techniques like pure functional programming and recursion. We will build on some of this knowledge.
You can either read the task by signing up on HackerRank and visiting the link, or by reading my summary here:
Suppose a binary tree is given with root R
. Each node may be connected to zero, one, or two child nodes. The edges of the tree are directed from the parent nodes towards child nodes. Determine the height of the tree, defined as the maximal number of edges from R
to any node in the tree.
The JavaScript data structure of a node is as follows:
Level up your interview prep. Join Educative to access 80+ hands-on prep courses.