0% found this document useful (0 votes)
0 views

Coding Interviews

Uploaded by

Syed Yusuf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Coding Interviews

Uploaded by

Syed Yusuf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ASHAY NAYAK

EP. 11

IN TEASHAYcoding
RV IENAYAK
N
W interview
ASHAY NAYAK
TOP T IO
UE S
Q

ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak Source: My Medium Article
ASHAY NAYAK
Hi Shreya! I am
Ashay. I am going
Hey Ashay! Thanks
for giving me this

ASHAY NAYAK
to take your oppportunity.
technical round.

ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak Source: My Medium Article
ASHAY NAYAK
I am going to ask

ASHAY NAYAK
few Coding
Questions. okay.

ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak Source: My Medium Article
ASHAY NAYAK
Given an m x n 2D binary
grid grid which

ASHAY NAYAK
represents a map of '1's
(land) and '0's (water),

ASHAY NAYAK
return the number of
hmm...
islands.

ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
An island ('1') is surrounded by water ('0') and
is formed by connecting adjacent lands

ASHAY NAYAK
horizontally or vertically. You may assume all
four edges of the grid are all surrounded by

ASHAY NAYAK
water. See next page for examples

ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak Source: My Medium Article
ASHAY NAYAK
Examples

ASHAY NAYAK
ASHAY NAYAK
here, All 1's are connected either
horizontally or vertically. So,

ASHAY NAYAK
number of islands = 1

ASHAY NAYAK
ASHAY NAYAK
here, you will find 3 groups of 1's
which are separated by water ('0').

ASHAY NAYAK
So, number of islands = 3.

By Ashay Nayak Source: My Medium Article


ASHAY NAYAK
Approach 1

ASHAY NAYAK
ASHAY NAYAK
Union Find using rank and path
compression: Not recommended as it has
lengthy implementation. To learn union-find,
ASHAY NAYAK
search “union find hackerearth” on google and
it has provided a very good explanation. If you

ASHAY NAYAK
know this topic then check my article for
code. Link in comment. Let's move to next

ASHAY NAYAK
approach.

→ →
TC
ASHAY NAYAK
O(n*m) and SC O(n*m)

By Ashay Nayak Source: My Medium Article


ASHAY NAYAK
Approach 2

ASHAY NAYAK
ASHAY NAYAK
DFS (Depth First Search): Best Solution — it
ASHAY NAYAK
is standard DFS which we are applying for 2d
array. See next page for understanding it's

ASHAY NAYAK
implementation.

TC → ASHAY→NAYAK
O(n*m) and SC O(n*m)

ASHAY NAYAK
By Ashay Nayak Source: My Medium Article
COde

// '2' -> it shows that cell(i,j) is visited

if we find '1' then it is an island (that


is why ans++), and we have to include
all 1's which are connected either
horizontally or vertically. It is
because all 1's which are not
separated by water make one island.
We are setting all connected 1's as
visited using dfs so that we don't
count them again.

By Ashay Nayak Source: My Medium Article


ASHAY NAYAK
Approach 3

ASHAY NAYAK
BFS (Breadth First Search): Best Solution —
it is standard BFS which we are applying for 2d
array. ASHAY NAYAK
ASHAY NAYAK
If you are looking to understand this BFS
algorithm with code for interviews,

ASHAY NAYAK
then check out my article link given in the

ASHAY NAYAK
comment below.

→ →
TC
ASHAY NAYAK
O(n*m) and SC O(n*m)

By Ashay Nayak Source: My Medium Article


Also, chec
kout my o
ther
articles on
"Your Que
stions/Inte
Exp./Tips rview
and Tricks
(link in co "
mment)

By Ashay Nayak Source: My Medium Article


ASHAY NAYAK
To Be Continued...

ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
ASHAY NAYAK
By Ashay Nayak Source: My Medium Article

You might also like