TRANSCRIPTEnglish

Kth Smallest Element in a BST - Google, Amazon, Facebook Interview Question

14m 24s1,713 words265 segmentsEnglish

FULL TRANSCRIPT

0:04

hey everyone welcome back to my channel

0:05

a new video in this video we're doing

0:07

another question um of K smallest

0:10

element in a binary search stream medium

0:12

there's medium question asked in lot of

0:15

companies um let's see where it was

0:19

asked K smallest

0:23

BST Google Facebook

0:26

what Facebook Google and Amazon so it

0:30

was asked in like three companies we're

0:32

doing the complete algorithms boot camp

0:33

so you can check out the links in the

0:34

description below and um three questions

0:38

usually they are paired up with other

0:39

data structures where we did some

0:41

questions with like hashmaps um and in

0:44

this one we're going to use

0:47

um heaps right so this one will be using

0:52

Heap and we have done heaps before so a

0:54

tree and a heap Tes will be used

0:56

together I think you can do it without

0:58

Heap as well but don't do it it in the

1:00

interview many people comment Kunal you

1:02

can do it directly like this why are you

1:04

solving it like you know with so many D

1:06

success because otherwise if you don't

1:08

do that you will get rejected in your

1:09

interview this is why I've seen many

1:12

compar proms get rejected in the big

1:14

tech company interviews um and on

1:16

LinkedIn there was a post by a a Fang

1:19

recruiter who said that they never hire

1:21

like Compu proms because um they don't

1:25

know how to communicate well or work

1:27

well in teams or whatever um not my

1:30

words so what I'm trying to say is when

1:32

you're given a question in an interview

1:34

and you you know the best answer to that

1:37

question don't tell the best answer

1:40

right away because they want to learn

1:42

about your thought process do the

1:44

opposite tell the worst answer the Brute

1:46

Force approach first then optimize it

1:49

and then try to further optimize it make

1:52

it more pretty and clean use less stuff

1:55

less lines of code and stuff and you

1:57

will be doing very well in your

1:59

interview

2:00

but let's do this question it says K

2:03

smallest element in a binary search tree

2:06

I've not done the Heap question videos

2:08

yet but if you had watched the video

2:11

after I have created it you would know

2:14

that whenever it is given to you like

2:16

find K smallest or k something you use

2:20

heaps the question is solved but since I

2:24

have not covered that already um this is

2:27

just a little hint that I'm giving you

2:29

that

2:30

um whenever K smallest something is

2:33

given you use heaps but because you're

2:35

trying to maintain it right but the

2:37

thing is you have to find the smallest

2:39

element so what do we do over here uh

2:42

this is a binary search tree

2:45

so I know that how can I get the sorted

2:48

order in ordered reversal we know in

2:51

binary search Tre if you do in ordered

2:53

reversal you will go sorted order so

2:58

three 1 1

3:01

4

3:03

2 okay if I do in ordered reversal

3:06

meaning I start from here then I go left

3:09

left nothing then current so one then

3:13

right then uh current so 1 2 3 4 see in

3:18

ordered

3:20

reversals I hope you all know what in

3:22

ordered reversal is so I don't have to

3:24

explain that so let's say once you have

3:25

this in ordered

3:28

reversal uh

3:30

let's say I talk about this one this

3:32

bigger

3:37

one okay let me just make this

3:42

tree

3:45

five 3

3:47

6

3:52

4 2 1 and K is equal to

3:55

3 third smallest

3:58

element

4:00

in ordered reversal will be

4:02

what from

4:07

here go here go here go here go here

4:11

there's nothing then go back

4:13

one

4:16

current write nothing current then

4:21

write then current then WR 1 2 3 4 5 6

4:26

what is the third smallest element third

4:28

smallest element is three so that should

4:29

be my

4:31

answer so what I want you to do is uh

4:35

since we are traversing it in uh sorted

4:38

order already I know I'm traversing the

4:41

tree in sorted order

4:43

already I know that I will visit the

4:46

tree in the sorted order so whatever the

4:49

third element is I can take that or the

4:52

kth element is so you can put this in a

4:55

heap of size three for example when you

4:58

are at one in the Heap you will

5:01

put one main Heap for example and then

5:05

you take two uh you're going to be like

5:07

okay I put two over here then you'll

5:10

like I'm at three I put three over

5:14

here

5:16

right four you can't put four over here

5:18

because it's uh like it doesn't

5:22

work make

5:27

sense right so so even though you

5:31

can cuz I don't want to restrict it to a

5:33

size so let's say we put in four then

5:36

five then six not a problem we put this

5:39

in a

5:43

heap but what we need to do is okay

5:46

there are multiple ways to do it yeah

5:48

you can add it all in if you want um so

5:51

you can

5:53

do just like this 1 2 3 4 five six so

5:58

this will be your Heap and then just

6:01

remove K elements from the Heap once it

6:04

is done so

6:07

remove K elements and you know the K

6:10

elements that the K element that you

6:12

remove it will be your answer one will

6:14

be removed two will be removed third

6:16

element will be removed this will be the

6:18

answer just run a loop from u 1 to K 0

6:23

to

6:25

K okay multiple ways to do it using Heap

6:29

what you can also do is you can only

6:31

maintain the

6:33

the K smallest

6:38

elements

6:40

right so only if something is like

6:42

smaller than this then only add it in

6:44

the

6:46

Heap make sense so makebe maybe you can

6:49

make a Max Heap in that case the size of

6:51

the Heap will be reduced okay so many

6:53

ways to do it you can also do it without

6:55

the Heap but I would not recommend doing

6:56

that as a straight up uh question um in

7:00

the interview

7:03

so what I would say is that let's say I

7:06

create a new

7:26

file okay create a new

7:30

file and uh what I'm going to do

7:37

is here we need a priority Q

7:43

so priority Q for

7:48

integer minan Heap is equal to

7:51

new priority

7:55

Q right then I can

7:58

say

7:59

helper or this will be my in ordered

8:04

reversal that I'll be doing and I will

8:06

be passing the root the minan Heap and

8:13

K right so it will change the mean he

8:16

because it'll pass it as a reference

8:20

um

8:23

private

8:25

void

8:27

helper TR node

8:31

node Min

8:36

Heap in

8:40

k um now

8:43

what if node is equal to equal to

8:46

null just return nothing you need to do

8:51

we go on the left first because that's

8:53

what in ordered reversal is node. left

8:57

Min Heap and K

9:01

node.

9:03

WR mean he and K before that the current

9:08

node that's what in order reversal is

9:10

left current right for the current what

9:12

I will do is I will just add in my M

9:22

he no dot

9:26

value okay now just take the remove key

9:32

elements so how do we do

9:45

that something like

9:47

that right and I can take an

9:49

answer initially is equal to zero I can

9:52

say answer is equal to Min he.

9:56

offer no not offer the offer is adding

10:00

uh min. pole which will remove it and

10:04

then in the end

10:05

just return the

10:19

answer accepted but you can see it's

10:21

like not very good in terms of

10:24

uh memory and stuff so what I will do is

10:29

do this much in the interview and then

10:31

uh remove the Heap so the interview like

10:34

Heap is taking extra space and stuff can

10:37

you remove it what I can do is I can

10:39

take

10:47

a I can take uh I can

10:51

say okay it's smallest

10:58

duplicate

11:00

okay it's smallest

11:03

two don't need

11:08

Heap I have a k over

11:11

here

11:14

so maybe I can take a value over here

11:18

private int K and here I can say this do

11:22

K is equal to whatever K we

11:25

have right provided to us now I will

11:28

call my

11:30

helper okay but in my helper I don't

11:34

need K now and I don't need root now and

11:38

uh I will have an answer as well right

11:41

so I can say private int

11:44

answer and here in the end I will just

11:47

return the answer so you can see it's

11:48

much more prettier now but in my helper

11:51

what will I do

11:54

now so instead of maintaining it in a

11:56

heap so here you can say instead of

11:58

maintaining in a Heap what I can do

12:03

is I can

12:06

just keep the value track of K so for

12:09

example K is equal to 3 when I reach

12:11

over here right when I reach over here I

12:14

will do K minus 1 so K will be now equal

12:16

to 2 and I will check is k equal to Z no

12:19

it's not then I will go over here I will

12:21

do again K minus 1 so 2 - 1 1 K's value

12:25

is one over here let me show you

12:33

okay so what I'll do is from when I come

12:35

over here I will put it in blue K's

12:37

value will be 3 - 1 2 2 is 2 equal to Z

12:41

no it's not then I'll go back check this

12:44

1 is 1 equal to 0 no it's not go back

12:48

minus one 0 is this equal to Z yes

12:51

because I have subtracted it K times so

12:54

I know that K elements I have seen in

12:56

the past so this is my k

13:00

element so I will just return from

13:03

here

13:05

right

13:06

so simple recursion I don't need to do

13:09

this I will just say k minus minus and

13:12

if K becomes

13:16

0o result is going to be equal to node

13:19

do

13:21

Val just return

13:25

need now this is much

13:27

simpler

13:36

right much simpler code less

13:41

memory oops this is why when you copy

13:44

paste should

13:46

always be

13:56

careful oh it's answer it's called

14:05

answer 100%

14:08

okay so much faster and

14:12

better go

14:20

cool

UNLOCK MORE

Sign up free to access premium features

INTERACTIVE VIEWER

Watch the video with synced subtitles, adjustable overlay, and full playback control.

SIGN UP FREE TO UNLOCK

AI SUMMARY

Get an instant AI-generated summary of the video content, key points, and takeaways.

SIGN UP FREE TO UNLOCK

TRANSLATE

Translate the transcript to 100+ languages with one click. Download in any format.

SIGN UP FREE TO UNLOCK

MIND MAP

Visualize the transcript as an interactive mind map. Understand structure at a glance.

SIGN UP FREE TO UNLOCK

CHAT WITH TRANSCRIPT

Ask questions about the video content. Get answers powered by AI directly from the transcript.

SIGN UP FREE TO UNLOCK

GET MORE FROM YOUR TRANSCRIPTS

Sign up for free and unlock interactive viewer, AI summaries, translations, mind maps, and more. No credit card required.