But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
Write a function to find the 2nd largest element in a binary search tree.
Here's a sample binary tree node class:
class BinaryTreeNode
{
public $value;
public $left;
public $right;
public function __construct($value)
{
$this->value = $value;
}
public function insertLeft($leftValue)
{
$this->left = new BinaryTreeNode($leftValue);
return $this->left;
}
public function insertRight($rightValue)
{
$this->right = new BinaryTreeNode($rightValue);
return $this->right;
}
}
We start with a function for getting the largest value. The largest value is simply the "rightmost" one, so we can get it in one walk down the tree by traversing rightward until we don't have a right child anymore:
function findLargest($rootNode)
{
$current = $rootNode;
while ($current) {
if (!$current->right) {
return $current->value;
}
$current = $current->right;
}
}
With this in mind, we can also find the second largest in one walk down the tree. At each step, we have a few cases:
function findLargest($rootNode)
{
$current = $rootNode;
while ($current) {
if (!$current->right) {
return $current->value;
}
$current = $current->right;
}
}
function findSecondLargest($rootNode)
{
if (!$rootNode || (!$rootNode->left && !$rootNode->right)) {
throw new Exception('Tree must have at least 2 nodes');
}
$current = $rootNode;
while ($current) {
// case: current is largest and has a left subtree
// 2nd largest is the largest in that subtree
if ($current->left && !$current->right) {
return findLargest($current->left);
}
// case: current is parent of largest, and largest has no children,
// so current is 2nd largest
if ($current->right &&
!$current->right->left &&
!$current->right->right) {
return $current->value;
}
$current = $current->right;
}
}
We're doing one walk down our BST, which means time, where is the height of the tree (again, that's if the tree is balanced, otherwise). space.
Here we used a "simplify, solve, and adapt" strategy.
The question asks for a function to find the second largest element in a BST, so we started off by simplifying the problem: we thought about how to find the first largest element.
Once we had a strategy for that, we adapted that strategy to work for finding the second largest element.
It may seem counter-intuitive to start off by solving the wrong question. But starting off with a simpler version of the problem is often much faster, because it's easier to wrap our heads around right away.
One more note about this one:
Breaking things down into cases is another strategy that really helped us here.
Notice how simple finding the second largest node got when we divided it into two cases:
Whenever a problem is starting to feel complicated, try breaking it down into cases.
It can be really helpful to actually draw out sample inputs for those cases. This'll keep your thinking organized and also help get your interviewer on the same page.
Reset editor
Powered by qualified.io