So I'm going to call my main function Island Counter and
I'm taking in the matrix here.
Now what I first want to account for is the none.
So if my matrix is equal to none or
if it is equal to the anti matrix.
[BLANK_AUDIO]
In this case I just want to return zero right way.
[BLANK_AUDIO]
Now if this is not the case, I'm just going to initialize my
islands variable to 0, which is the number of islands.
And I'm going to want to do two for loops to loop through this.
So I want to know the number of rows and columns.
[BLANK_AUDIO]
So this is actually going to be vice versa here.
Okay, so I'm going to write my for loop.
[BLANK_AUDIO]
Now when I get here, I want to check if the element is equal to one.
[BLANK_AUDIO]
And if it is, I'm going to increment my islands count.
[BLANK_AUDIO]
So now what I need to do here,
is I need to do the breadth first search I was talking about before.
So I'm just going to have a helper method to do that.
[BLANK_AUDIO]
Call it find points of island.
[BLANK_AUDIO]
So I'm going to go ahead and define that down here.
OK.
[BLANK_AUDIO]
So the first thing I want to do is I want to initialize a queue.
[BLANK_AUDIO]
>> Why did you choose a queue?
>> Since we're using a breadth first search here,
I want to be able to use a queue so that I can get the first element in the-.
When I add an element to the queue,
I want to be able to get the first element that I added.
>> Yeah, that makes perfect sense.
>> And I actually need to import this method here and
this is from the collections framework.
[BLANK_AUDIO]
>> That's a good catch.
[BLANK_AUDIO]
>> And then the first thing I'm going to do is I'm going to
append my Indexes here so I know the locations.
So I'm going to pin these to my queue.
[BLANK_AUDIO]
And now I'm just doing a breadth first search here, so
while it's empty, we're going to keep searching for other things around.
[BLANK_AUDIO]
So while the length of the queue
[BLANK_AUDIO]
Does not equal 0.
[BLANK_AUDIO]
So what I want to do here is I want to look at the elements around it.
So you mentioned before that I needed to check if they were actually there.
So I'm going to write a helper method to do that, too.
[BLANK_AUDIO]
Okay, so now that I'm checking if this is empty,
I want to do something with this element here.
But it looks like I'm running out of space, so
I'm just going to draw a little arrow over here.
And just cut this little section off here that I'm going to be using
to finish this method here.
So what I want to do is I want to be able to
get this element here from-, let me make sure to go up a little bit higher.
So I want to pop this element off the cue.
And then I want to be able to access these two variables,
so I'm just going to set them to a new one.
[BLANK_AUDIO]
Okay. So now that I'm here,
I want to check if this element is equal to one.
[BLANK_AUDIO]
And if it is, I want to set it to two as I mentioned before.
[BLANK_AUDIO]
So I'm going to use a helper method right here to check if these
elements that are next to it, actually next to it.
And then if they are, I'm going to add it to the queue.
So, I'm going to call that-
[BLANK_AUDIO]
Set up more space here.
So this method is going to be called append
[BLANK_AUDIO]
If.
And I'm passing in the queue.
And I also need
[BLANK_AUDIO]
The row, column, and
both of these coordinates here.
Okay.
So what I want to check here is if the x value
is greater than or equal to 0, or-.
I mean not or.
And.
[BLANK_AUDIO]
It is less than the number of columns,
and if y is greater than or equal to 0,
and if y is less than the number of rows.
And then if all those conditions hold true,
what I'm going to want to do is append this to the queue.
[BLANK_AUDIO]
there, and so I'm going to want to append this right here.
And just to make sure I'm appending it the right way so
I access the first element, I want it right here.
So my helper method's going to work.
So after I do all of that there, method here.
I'm running out of space right here.
So all this information here is just basically I'm calling this method on all
the elements around it.
So I'm going to append
[BLANK_AUDIO]
If.
And so what I need to do is pass the queue.
Rows, columns, and what I'm going to do is x plus 1 and y.
So it's going to be the elements to the right of it.
[BLANK_AUDIO]
And then this is going to be the element below it.
[BLANK_AUDIO]
So left.
[BLANK_AUDIO]
And on top.
That looks like it.
Well, that went pretty smoothly.
>> Yes it did.
After going through all the necessary steps before coding the solution,
the actual coding is a lot easier than if he were to have just jumped in and
coded from the beginning.
>> The interviewee also made sure to let the interviewer know what
he was doing when he was coding.
But he didn't read out all of his code as he was coding.
We often see interviewees just reading out their code as they code which can
get annoying and does not really build understanding with the interviewer.
>> Yep.
This interview, we focused more on explaining the purpose of the code
while he was writing.
By talking out loud, you give your interviewer the chance to jump in and
help if your logic is wrong.
You also proved to your interviewer that you're actually
thinking through all of your code.
If you just write code you might be spitting out a memorized answer.