Difficult Apple Coding Interview Question - JavaScript

preview_player
Показать описание
Solution to a very popular coding interview question asked recently by Apple - Maximum Depth of a Binary Tree.

#softwareengineering #javascript #shorts
Рекомендации по теме
Комментарии
Автор

int maxDepth(node* root)
{
if ( !root ) return 0;
return max(maxDepth(root->left), maxDepth(root->right) ) + 1;
}

rutvikrana
Автор

This is the basic question in every company I'll say

rammrras
Автор

apple asks this. probably for a super warm up question lol very nice lesson!

ejv
Автор

This is an average Computer Engineering university exercise...

paololucchesi
Автор

10 years in, still haven't needed to code a binary tree...

ShortFilmVD
Автор

ask what they'd like as a no root return values first. usually it's a None or a 0 . let's assume 0 for a simple case
in Python:
def maxDepth(root):
if not root: return 0
return 1 + max(maxDepth(root.left), maxDepth(root.right))

TheShri
Автор

Honestly thanks man just had this as an interview question today. Haven’t thought about depth it breathe first search since college and wouldn’t have remembered if it wasn’t for this video

bChipps
Автор

That really weird to use side effect on recursion.
Instead you can just do this.

const maxDepth = node => node == null ? 0 : 1 + Math.max(maxDepth(node.left), maxDepth(node.right))

ence
Автор

As soon as you said "global variable" I knew exactly what kind of channel this was

Eknoma
Автор

// Option 1
function maxDepth(tree) {
const depthLeft = tree.left ? maxDepth(tree.left) : 0
const depthRight = tree.right ? maxDepth(tree.left) : 0

if(depthLeft > depthRight) {
return depthLeft + 1
}

return depthRight + 1
}

// Option 2
function maxDepth(tree) {
if(!tree) return 0

const depthLeft = maxDepth(tree.left)
const depthRight = maxDepth(tree.left)

if(depthLeft > depthRight) {
return depthLeft + 1
}

return depthRight + 1
}

martoxdlol
Автор

🚀 Exclusive DSA Course Vid - Click on Channel Page! 🚀

AlgoJS
Автор

Doing gods work. Never could wrap my head around dfs

engine_man
Автор

The variable maxDepth was a terrible idea. It's the name of the function.

DavidNewmon
Автор

I wish this was coding question for apple lol

skillfulfighter
Автор

BFS algorithm easily solves this in minimum time!

MatthewIrizarry-
Автор

No need for nested functions, just place a default parameter for the first launch.

YamiSuzume
Автор

I’m a beginner plzz correct my question if wrong since you have assigned max depth zero why have you used the ‘ let’ statement ? Is it bcoz zero also represents false in bool ?

rakeshcs
Автор

public maxDepth(TreeNode root){
if(root == null){
return 0;
}
int leftH = 1 + maxDepth(root.left);
int rightH = 1 + maxDepth(root.right);
maxDepth(root.left);
maxDepth(root.right);
return Math.max(leftH, rightH);
}

ericli
Автор

This is a beginner question, not a difficult one....

reydavid
Автор

Always try to avoid recursion.
These are better ways to solve it, don't use recursion

mikhailurmich