Interview Question: Random Binary Tree

  Рет қаралды 15,911

Byte by Byte

Byte by Byte

Күн бұрын

Пікірлер: 24
@anuragdani9739
@anuragdani9739 8 жыл бұрын
The main difference that I found in your videos and other is that you do explain the thought process and approach to solve the problem and explain why they might not be the optimum solution for the problem that you are trying to solve. It would seem like a very trivial point, but this does give a direction for thinking in general problem and this is what separates from you other similar videos online. Keep this up, and I am very sure you will be highly successful in your current venture.!
@ByteByByte
@ByteByByte 8 жыл бұрын
anurag dani thanks! That's exactly what I'm going for so I'm glad it's coming across and that you are finding it helpfil
@sharjeeltahir5583
@sharjeeltahir5583 4 жыл бұрын
There are other people who do the same
@sammyiboi
@sammyiboi 7 жыл бұрын
Hi Sam! Before you went into your explanation, I was trying to brainstorm a solution. And, I came up with "somehow" giving each node a certain weight value. For example, the root of the tree would get a low weight value so more than likely, a randomly generated value would not match it. And then it would traverse down to one of its children. And.. ahh, even trying to type this quasi-solution is giving me a headache! I think the trickiest part is remembering that we can "take control" of our data structure (e.g. "Can I assume that the Node class has a 'children' field?") It's a great thinking-outside-the-box approach that not only makes the solution more elegant but also shows the interviewer that you think critically about the assumptions of a question. Anyway, thanks for the vid! Hoping to watch 1 or 2 everyday as interview prep.
@ByteByByte
@ByteByByte 7 жыл бұрын
You're welcome! That's awesome to hear that you got some valuable insight from the video :)
@PabitraPadhy
@PabitraPadhy 3 жыл бұрын
@bytebybyte : Assume, I'm not modifying the tree structure, and calculates the GetChildCount() during runtime. So, for a dense tree, it would result me : { O(h) + O(h/2) + O(h/4) + ....} = O(h logh) and for a skew tree, it would result me : O(h^2) If I use this algo (with dynamic child count) Now, let's assume, your tree is going to add/delete nodes frequently, so we have to recalculate the child count, say worst case O(n). So, I believe, you're taking tree to have correct child node, when you're running the GetRandomNode() algo, that would make it O(h) Correct me, if I'm wrong.
@MegaModelina
@MegaModelina 6 жыл бұрын
Thanks Sam! Altho lengthier, would it also work to have a separate rightChildren() that also adds the length of left subtree + parent node, in order to keep the indexing the same throughout? Could then pass which fxn to use as parameter. Please lmk
@berylbose7006
@berylbose7006 2 жыл бұрын
Can we give an index to each of the nodes, in IN-ORDER fashion, like a bst. and when we traverse, we will be doing a bin search to get the node with the index we are looking for. If this a valid approach?? Does my approach have the same time complexity as yours?
@Famctabe
@Famctabe 8 жыл бұрын
Hi Sam, if you were given a perfect binary tree could you eliminate the extra storage for the children count? As long as you know the size (or height), you'd know where the random index lies because it's symmetrical.
@ByteByByte
@ByteByByte 8 жыл бұрын
that's a good question. you would at the very least need to know the total number of nodes in the tree (although you could track that as a single int). I think it would work if you knew that you had a perfectly balanced tree, but as soon as you add or remove any nodes, then your tree is no longer perfectly balanced so it's a somewhat narrow usecase
@gentleflameoflove
@gentleflameoflove 7 жыл бұрын
This video is more to do with random selection from a binary tree, doesn't it Sam? Does it discuss the 'random binary tree' data structure as such?
@ByteByByte
@ByteByByte 7 жыл бұрын
What do you mean by 'random binary tree datastructure'?
@HimanshuSharma-tm2ms
@HimanshuSharma-tm2ms 4 жыл бұрын
You are still using O(n) in the best solution cause you are storing no of children in each node.. but than logn trick was good:)
@piyush12121
@piyush12121 8 жыл бұрын
Hey, Awesome video. Can I say that you're basically doing an Inorder traversal of the tree to find the random value ?
@ByteByByte
@ByteByByte 8 жыл бұрын
Thanks! I gotta disagree. Really the whole idea is not to do the full inorder traversal. Rather we're treating the tree as ordered and doing a binary search for the index. Inorder traversal would mean traversing all the nodes until we get to an index, which we are explicitly trying not to do
@piyush12121
@piyush12121 8 жыл бұрын
Cool !
@RaymondLo84
@RaymondLo84 4 жыл бұрын
Is that a typo? randomNode is never defined? You may getRandomNode?
@sanjutoyou
@sanjutoyou 8 жыл бұрын
Hey what is the case when this tree will return root node i.e. 4
@ByteByByte
@ByteByByte 8 жыл бұрын
if the number generated was 3, then you would find that the root had 3 children on the left and so you would return immediately
@ivanpetrov3200
@ivanpetrov3200 4 жыл бұрын
Why don't you just at each step choose randomly left or right?
@kevinjad4506
@kevinjad4506 4 жыл бұрын
Hey, Ivan. We would be calling the random number between flag_right and flag_left more than one time which is costly.
@sivaprakashnithyanandam1473
@sivaprakashnithyanandam1473 5 жыл бұрын
Very lengthier............
@snoopyjc
@snoopyjc 5 жыл бұрын
Nice but... There is no randomNode function!
@RaymondLo84
@RaymondLo84 4 жыл бұрын
it should be getRandomNode
Interview Question: Random Linked List
26:26
Byte by Byte
Рет қаралды 17 М.
Binary Search Tree Tutorial - Traversal, Creation and More
32:30
Tech With Tim
Рет қаралды 33 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Interview Question: Balanced Binary Tree
17:43
Byte by Byte
Рет қаралды 56 М.
Interview Question: Autocomplete
31:37
Byte by Byte
Рет қаралды 37 М.
Lowest Common Ancestor Between 2 Binary Tree Nodes (A Recursive Approach)
20:30
Advanced Data Structures: Randomized Search Trees (RSTs)
14:03
Niema Moshiri
Рет қаралды 14 М.
Interview Question: Three Sum
27:13
Byte by Byte
Рет қаралды 61 М.
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 198 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 14 М.
Interview Question: Two Missing Numbers
31:49
Byte by Byte
Рет қаралды 40 М.
Balanced Binary Tree - Leetcode 110 - Python
13:11
NeetCode
Рет қаралды 278 М.
Interview Question: String Deletion
26:19
Byte by Byte
Рет қаралды 14 М.