Medium Google Coding Interview With Ben Awad

Sdílet
Vložit
  • čas přidán 9. 06. 2021
  • In this video, I conduct a medium-difficulty Google coding interview with Ben Awad, a software engineer and tech CZcamsr. As a Google Software Engineer, I interviewed dozens of candidates. This is an intermediate coding interview that you could get at Google or any other big tech company.
    Want to see me get interviewed by Ben Awad? Check out the intermediate React interview video that we did on his channel: • Intermediate React.js ...
    AlgoExpert: www.algoexpert.io/clem
    SystemsExpert: www.systemsexpert.io/clem
    MLExpert: www.algoexpert.io/ml
    My LinkedIn: / clementmihailescu
    My Instagram: / clement_mihailescu
    My Twitter: / clemmihai
    Prepping for coding interviews or systems design interviews? Practice with hundreds of video explanations of popular interview questions and a full-fledged coding workspace on AlgoExpert - www.algoexpert.io - and use the promo code "clem" for a discount on the platform!
  • Věda a technologie

Komentáře • 1K

  • @clem
    @clem  Před 3 lety +563

    So, should we do a hard Google coding interview next? 👀
    Be sure to check out the other video that we filmed on Ben’s channel, where he gave me an intermediate React interview! czcams.com/video/6s0OVdoo4Q4/video.html

  • @bawad
    @bawad Před 3 lety +2828

    I should stick to inverting binary trees

    • @crvlwanek
      @crvlwanek Před 3 lety +269

      Time to go back to your root(s)

    • @mkhnuser
      @mkhnuser Před 3 lety +14

      Thank you for you positive, it was very pleased to watch this video.

    • @carlossegura403
      @carlossegura403 Před 3 lety +1

      Lmao

    • @dinasina3558
      @dinasina3558 Před 3 lety +1

      You did ok

    • @dinasina3558
      @dinasina3558 Před 3 lety +16

      Yesterday I was taking Facebook screening interview and they want you to solve this FAST

  • @harispapadopoulos4295
    @harispapadopoulos4295 Před 3 lety +1586

    There’s something about seeing people being nervous for a mock interview like this that’s so satisfying

    • @clem
      @clem  Před 3 lety +299

      😂Loving to witness other people's misery, huh?

    • @harispapadopoulos4295
      @harispapadopoulos4295 Před 3 lety +96

      @@clem Yeah, I just hope my future interviewers are not seeing this comment 🤫

    • @karthickshankar1527
      @karthickshankar1527 Před 3 lety +114

      coding while the whole world is watching your competency and you are in position to prove it. Nah I would rather prefer real interviews.

    • @aakarshitrekhi8071
      @aakarshitrekhi8071 Před 3 lety +8

      Sadist😂😂

    • @webdevinterviewtrainingusa1494
      @webdevinterviewtrainingusa1494 Před 3 lety +4

      czcams.com/video/xLGz4qU_0bA/video.html

  • @FrostTechSoftware
    @FrostTechSoftware Před 3 lety +1213

    Always love how Ben Awad deduces his problems. 50% Troll 50% Serious

    • @geoffnolan1053
      @geoffnolan1053 Před 3 lety +66

      "You know what? I'm being a pleb I think."

    • @VictorZamanian
      @VictorZamanian Před 2 lety +40

      He was over-engineering this problem completely.

    • @VladIDrago
      @VladIDrago Před rokem

      Donald Duck Coding, is amazing.

  • @madomation7984
    @madomation7984 Před 3 lety +692

    Me after 5 minutes: I don't think I am made for this

    • @salimahmedali9249
      @salimahmedali9249 Před 3 lety +89

      Trust me. I used to feel the same. Just keep at it, doing harder and harder probs and then you're solving such problems in no time :P

    • @janehoe531
      @janehoe531 Před 3 lety +16

      *le Clement after seeing this
      "...and how to make sure that never happens to you,,, ALGOEXPERT"

    • @diegovulcan1
      @diegovulcan1 Před 3 lety +7

      @@anonymousasdoasidjasd9911 Best comment. lol

    • @gujeffrey5433
      @gujeffrey5433 Před 3 lety +14

      Don't worry the guy getting interviewed isn't either.

    • @microapple97
      @microapple97 Před 2 lety +2

      @@gujeffrey5433 For real. It's hard to stay motivated when interview coding questions are so contrived.

  • @christsciple
    @christsciple Před 2 lety +589

    Watched this the day after you released Clement. During an interview with a fintech startup a week later, they gave me this exact problem. I was able to remember and successfully work through roughly 80% of it. Got offered the job and ultimately rejected their offer because one interviewer who would have been my daily collaborator, was rude and demeaning. All in all, this is great information and a great video you've produced here!

    • @clem
      @clem  Před 2 lety +78

      Amazing story! Thanks for sharing!

    • @joelaucca9081
      @joelaucca9081 Před 2 lety +6

      how much was the offer you declined? out of curiosity

    • @humann5682
      @humann5682 Před 2 lety +19

      Nice! Fintech companies IME can be horrible places to work because many are fundamentally banks/insurance companies at heart with tech departments. You'll be forced to appease some pretty unpleasant people in Fintech and try as they might Fintech just can't get the whole "tech" vibe good companies have.

    • @Rob-vg6lw
      @Rob-vg6lw Před 2 lety +4

      Sometimes you just got to man up and work with A-holes.

    • @christsciple
      @christsciple Před 2 lety +90

      @@Rob-vg6lw I come from a construction and ranching background; worked with plenty of A-holes. Working with A-holes doesn't make you a man. Ethics, love, and principles do. Thankfully I have those, and am now in a much richer and better environment and am in control of my future.
      Word of advice - whoever told you it's manly to take a miserable job over better options, was incredibly wrong. Sometimes you may have no other option, but when you can afford to take a different job with a more supportive group, do it.

  • @morenokv
    @morenokv Před 3 lety +68

    Please keep doing these! This is great and love hearing the thought process

  • @cgerman
    @cgerman Před 3 lety +884

    I think this shows that you need to practice specifically for these coding interviews. It doesn't matter if you are a programmer or you can code or if you are smart and you have experience. Ben is all these things. You need to practice problems like these to get a job at a company that does these interviews. It's like preparing for an exam. Even if you do ok like Ben did someone will do better because he spent hours and hours practicing. So many people want to get in these companies that it is very competitive. It's just how it is

    • @ducthinh2412
      @ducthinh2412 Před 3 lety +112

      ^^^THIS. I am nowhere as knowledgable as Ben in technologies that are actually useful on the job, but because I have done many similar problems, it only took me 10 min to code up the solution. I didn't even have to think, my hands just spit out code from muscle memory lol

    • @lolnoob5015
      @lolnoob5015 Před 3 lety +43

      Same, came up with a solution within the first 3-5mins because I've been doing these non stop, everyday for the last 4-5 weeks

    • @amynguy
      @amynguy Před 3 lety +12

      @@ducthinh2412 yup as soon as he showed the question, I knew this was dfs lol

    • @cartoons__for__kids_Hindi
      @cartoons__for__kids_Hindi Před 3 lety +1

      @@ducthinh2412 where you do problems like these ? Gfg, leetcode etc ?

    • @johnpainter367
      @johnpainter367 Před 3 lety +6

      I'm 17 and I almost always have a solution to leetclde medium or easy level problems in less than a minute. Hard usually needs some testing so that the solution is fast enough.

  • @TheKy01
    @TheKy01 Před 2 lety +22

    really love this format! so entertaining! very interesting to see "live" how ppl approach a problem

  • @whatsnextforkev
    @whatsnextforkev Před 2 lety +14

    for the "is_border" and "is_border_matrix" functions, since returning True and False over an If statement is redundant, I would have simply done a one-liner: "return ". That would tell me he's been through a number of code reviews or is part of a larger team.
    I've done my share of interviewing candidates as a Senior Eng at a huge retail company and even for me, a Google interview is anxiety-inducing.

  • @alsoSeeb
    @alsoSeeb Před 2 lety +187

    Almost two months ago, I started this video but didn't finish it because I wanted to solve this problem for myself. I didn't find a way to do it on my own, and both the problem and the video sat in the back of my mind for weeks. Today, after quite a lot of reading and practice (on other problems!), I returned to the problem with a fresh point of view and solved it in an hour (plus a few minutes), in one sitting, without any outside reference. It's been tough measuring my progress over the last few months, but thanks for giving me a point of reference!

    • @kimgysen10
      @kimgysen10 Před 2 lety +21

      You'd think that all software developers are building search engines. Lmfao. These coding questions can be solved, but the majority of problems in the software industry revolve around devops and system maintenance. Sometimes building new extensions / microservices / libraries, but these kind of problems only present themselves in my own pet (hobby) projects. Basically you can solve all of these abstract problems and still be a noob by lack of experience. Imo, time is much better spent by studying apis.

    • @frankzander6234
      @frankzander6234 Před 2 lety +1

      @@kimgysen10 dont forget actualy coding something thats where you notice a lot of problems and learn how important it is to write clean code

    • @kimgysen10
      @kimgysen10 Před 2 lety +5

      @@frankzander6234 For hobby projects yes. Majority of jobs as developer in the corporate world to earn a living requires quite a different skillset. That is in my experience.

    • @txic.4818
      @txic.4818 Před 2 lety +1

      @@frankzander6234 right but i'm not sure youre getting his point

    • @anjanathreya8306
      @anjanathreya8306 Před rokem

      @@kimgysen10 I don't think he's trying to progress professionally when he wrote this comment, the progress he's talking about is the progress he's made in how he thinks and his ability to problem solve. The relevancy of the project is none.

  • @ninasinthehaus
    @ninasinthehaus Před 2 lety +1

    thank you for making these videos! I solved this problem by modifying the grid to have perimeter of 1s (so both len(grid) and len(grid[0]) increase by 2) and running BFS on the grid. After finding an island, all other islands are changed to 0s. Pausing the video after you explain the question and talking out loud "back" to you has been so so helpful.

  • @SkaiCloud562
    @SkaiCloud562 Před 2 lety

    I'm addicted to these mock interview. Thank you for these quality content.

  • @ogbuzzy1974
    @ogbuzzy1974 Před 3 lety +12

    I absolutely love these videos! Do more of these interviews !

  • @TechWithTim
    @TechWithTim Před 3 lety +558

    Damn, this is a great coding interview question! ;)

    • @clem
      @clem  Před 3 lety +60

      I wonder who filmed the video explanation for it on AlgoExpert 👀

    • @mustafaaljanabi4818
      @mustafaaljanabi4818 Před 2 lety +6

      @@clem 👀

  • @madomation7984
    @madomation7984 Před 3 lety +21

    I like how both of them interview each other and post the video on same time

  • @rajatsharma7191
    @rajatsharma7191 Před 2 lety +4

    You guys are just incredible and an inspiration, that helps me keep on learning. Thanks for sharing!!

  • @RushOrbit
    @RushOrbit Před rokem +12

    Mark all the ones you shouldn’t touch with a different value. You can use DFS to do this.
    Go over the entire grid at the end, changing the marked cells back to one, the 1s to zero, and the 0s skip.

  • @x32gx
    @x32gx Před 2 lety +24

    This is extremely inspiring some how… Well done.

  • @MichikoHoshi
    @MichikoHoshi Před 2 lety +1

    I somehow stumble upon this video and I really enjoyed it. Watching you two talk through the process was very insightful and it made it very easy to comprehend more so than if this was just a tutorial video.

  • @grichl88
    @grichl88 Před 2 lety +2

    Clément seems like a really good interviewer which is hugely helpful.

  • @changkim8883
    @changkim8883 Před 2 lety +52

    Here's my approach
    1. Loop all edges - Only edges
    2. If it's 0, then move to next
    3. If it's 1, then update to 0 and find adjacent 1 and update to 0 until it can't find adjacent 1
    At the end of this loop, you end up with a new matrix where 1s connected to edge is all 0s
    then finally, you subtract the new matrix from the original.
    Since all edges and connected cells are turn to 0, you're not changing anything, but the island cells will be 0

  • @szeryk324
    @szeryk324 Před 2 lety +57

    Well done Ben! Coding when the entire world is watching - it's a nightmare. You're a really brave guy!

  • @BekBrace
    @BekBrace Před 3 lety +2

    Great coding interview ! Brilliant stuff, Clément !

  • @user-dn9dp8lu5j
    @user-dn9dp8lu5j Před 2 lety +1

    Store the index of rows and index of columns with ONES in seperated arrays and find the abs distance between indexes. Just drop the 2d-indexes that don't have min abs distance equal to 1. Redraw according to the remaining indexes that represent the Aces after the drop.

  • @chennn958
    @chennn958 Před 3 lety +23

    Make a hard coding interview one! These two were so good!

  • @crvlwanek
    @crvlwanek Před 3 lety +27

    This is my favorite crossover

  • @felipe_ai
    @felipe_ai Před 3 lety +5

    I could watch this 2 forever lol, amazing, entertaining and learned a lot :)

  • @drewtrades265
    @drewtrades265 Před 2 lety

    Wow Clements product has come a long way. So awesome!! Keep up the amazing work buddy.

  • @WaqasRants
    @WaqasRants Před 2 lety +58

    Seeing this level of skill, almost feel as if I am not worth my salt to be in this industry. As someone who does app development for a living, this was overwhelming and quite literally humbling to watch.

    • @razrgu3838
      @razrgu3838 Před 2 lety +22

      seriously? his thinking was really messy。i had watched some college kid wrote a much more complicated board game in one sit including all the gui code in c without internet access,and almost no bugs. This guy was like without thinking and just try

    • @agfd5659
      @agfd5659 Před 2 lety +6

      That was not impressive at all I don't think. It felt to me like early monday morning level of coding, or really nervous level of coding. Maybe if it was me in his place I would do stupid stuff too, but either way, he didn't impress me :)

    • @kelvinxg6754
      @kelvinxg6754 Před 2 lety

      ​@@razrgu3838 mildly ADHD maybe (i assume)

    • @rolfspuler1056
      @rolfspuler1056 Před 2 lety +19

      I'm an experienced software developer but I would have definitively not passed that test. These Google interviews are all about algorithms which is something I learned twenty years ago in my studies but in my professional life I hardly ever came across algorithms again. I think this kind of interviews are pretty artificial and to pass them you have to train for it. I'm pretty sure it won't have much to do with your real work you will do.

    • @varunshrivastava2706
      @varunshrivastava2706 Před 2 lety

      @@rolfspuler1056 in which company do you work?

  • @sifiso5055
    @sifiso5055 Před 3 lety +3

    We waited a lengthy period for this, but it was worth it🙌

  • @wilhelmngoma9009
    @wilhelmngoma9009 Před 3 lety +12

    This wasn't easy. Good job Ben. Good job coach Clément

  • @saran703
    @saran703 Před 3 lety +29

    Looking forward to solving hard coding challenge with Ben... that would be fun for sure lol

  • @nathanvincent8429
    @nathanvincent8429 Před 2 lety +42

    Ben laughing when Clement stops him for a second is great cause he knows it's a jank approach.

  • @MorganBlem
    @MorganBlem Před 2 lety +14

    Just wanted to say a nice very fast solution I thought up to this problem:
    1) Make a second matrix (we'll call it m2) of the same size filled with zeroes
    2) Iterate over the edges of the original matrix.
    3) Each time you encounter a 1, do a depth first search. At each visited position, add a 1 to the corresponding position in m2 and then set the value to 0 in the original matrix.
    4) Once you've iterated all edges, m2 will contain all islands that connected to the edges, though none that didn't. Return m2.

    • @Lorkwondo1234
      @Lorkwondo1234 Před 2 lety

      why do you have to set the value to zero in the original matrix

    • @MorganBlem
      @MorganBlem Před 2 lety +1

      @@Lorkwondo1234 just helpful to avoid having to keep track of which positions you've visited. Setting to 0 means future function calls that check a cell which has already been visited just return.

    • @megadeth_9611
      @megadeth_9611 Před rokem +1

      Oof i don't know what is dfs yet, will learn about it and come back 🤠
      I was thinking the same approach but didn't knew how to determine if it's on the edge or not. Maybe after knowing dfs i'll know

    • @noobygames7363
      @noobygames7363 Před 11 měsíci

      Hey this must be very late and I have implemented this in my own java code , what would the time complexity be for this ? I was thinking O(n log(n)) ? But im not sure

  • @iamnuckingfutz
    @iamnuckingfutz Před rokem +2

    This just shows that having too much knowledge can slow you down sometimes. I have a few years of coding experience and the first thing I thought of what the recommended solution, while I didn't know all of the things Ben was talking about.

  • @brands2131
    @brands2131 Před 2 lety +2

    There is another optimization, you can skip checking every 2nd square. Like a checker board. Because if each square checks the 4 neighbors, then the next neighbor doesn't need to check any neighbors, because the neighbors are already going to check that for it. So if there were all 0's on the board, you could cut the time in half, less so if more 1's, however that's already optimized by skipping islands.

  • @goodnightvids
    @goodnightvids Před 2 lety +25

    I'm basically new to code. I am currently learning the very basics of C++. This video is way harder to understand for me but i watched either way. I will rewatch it again in a few years and hopefully my skills are better and i can be able to understand it. Great content!!

    • @pvtryan
      @pvtryan Před 2 lety +3

      Don't give up

    • @pvtryan
      @pvtryan Před 2 lety +12

      This is your 2 week reminder to keep practicing

    • @icyrex7893
      @icyrex7893 Před rokem +3

      Adding on to Ryan’s comment. How is your understanding of the video now? I’m about 75% of the way through Codecademy’s Full Stack development course and I can understand the majority of what’s going on, but still am lost in some places.

    • @youisstupid2586
      @youisstupid2586 Před rokem

      @@icyrex7893 do you understand it all now?

    • @jerodewert8334
      @jerodewert8334 Před 2 měsíci

      As a computer scientist with ten years in industry, it was neat but nothing crazy: how is your understanding now?

  • @TheBenjaminsky
    @TheBenjaminsky Před 2 lety +26

    The "if return True else return False" lines are killing me :)

    • @NirEndal
      @NirEndal Před 2 lety

      After getting harassed by teachers all year when someone did this, it also triggers me ^^

    • @ButerWarrior44
      @ButerWarrior44 Před 2 lety

      @@NirEndal how are you supposed to do it?

    • @NirEndal
      @NirEndal Před 2 lety +1

      @@ButerWarrior44 "return something" with an eventual cast to boolean if it is not a boolean already

    • @ButerWarrior44
      @ButerWarrior44 Před 2 lety

      @@NirEndal pseudocode please

    • @NirEndal
      @NirEndal Před 2 lety +3

      @@ButerWarrior44
      Instead of:
      IF condition
      THEN RETURN TRUE
      ELSE
      RETURN FALSE
      Use:
      RETURN condition
      I can't do anything more explicit ^^'

  • @ronpaul9172
    @ronpaul9172 Před 2 lety +1

    This was AWESOME. Thank you!

  • @codearabawy
    @codearabawy Před 2 lety

    I liked the question. Well done, Ben! and Clement, you were very helpful :)

  • @1122slickliverpool
    @1122slickliverpool Před 3 lety +7

    I honestly can tell Clement enjoy these coding problems. These problems are like math problems to me. There’s multiple ways of doing these, but it’s always fun to find the most optimize solution. 😉

  • @matdatN1gg4
    @matdatN1gg4 Před 2 lety

    Recursively check each index with value 1 the top, left, right, bottom for value of 1 until at border. Return true if 0 at border, false if 1 at border.

  • @birinhos
    @birinhos Před 2 lety +2

    start from a blank matrix then start a recursive "walking" starting from the boders on the original matrix in all directions (recurssivly) on 1's and if it is valid put 1's on the new matrix (y and y coordinates must be maintained on the recursive process )

  • @ron-davin
    @ron-davin Před 3 lety +47

    0:55 That Ben's laugh as Clement promoted AlgoExpert 🤣🤣

  • @robbyoconnor
    @robbyoconnor Před 2 lety +3

    When you do functions with a single boolean expression, just do return boolean expression rather than if/else.

  • @user-hs9dc6rm3v
    @user-hs9dc6rm3v Před 2 lety

    get the position of all 1s . for each 1 check the elements at the index(+1 ,-1 , +length, -length(length of row)) till we reach the max length possible and store if it is connected to island. Do this for each 1's and we can exit if we hit 1 which has connected to island flag and remove the 1's which are not in boarder which are position of multiples of row length.

  • @mahrezjanati3426
    @mahrezjanati3426 Před rokem

    the way i would solve this is (i still didn't watch the vid) ...
    1:mark all "1"s on the edges
    2:go through the matrix from edge to center in a spiral or only horizontally (in c++ it would be for (int i =0; i< n^2) and mark every "1" that is connected to an already marked 1 and also mark every "1" that was connected to it but not marked
    3:for loop to delete unmarked "1"s

  • @fizzyjoecsgo7154
    @fizzyjoecsgo7154 Před 2 lety +13

    Nice and easy if you map it to a bitboard and can use bitwise operators to find your borders and store your islands as ints. Also makes it very fast on large matrices :)

  • @mannyioi
    @mannyioi Před 2 lety +5

    This has a bug if the dimensions of the matrix have 2 digits. In your border_island dictionaries when you store Row 1, Col 12 and Row 11, Col 2 both of these are going to be represented as {'112', True } which are both same ... you probably should have used tuples to store them ... But I love this.

  • @user-ss4tc4xm9u
    @user-ss4tc4xm9u Před 2 lety +1

    Love your interview series

  • @ghislaincarrier
    @ghislaincarrier Před 2 lety +1

    Islands = copy of input
    loop on each values in the border of variable islands
    if value == 1 set it to 0, then recurse on each 4 directions
    in the recurse, if the value is 1 set it to 0 and recurse 4 directions
    at the end return intput - islands
    no need to store and query a separate visited structure

  • @mojo_jojo913
    @mojo_jojo913 Před 2 lety +12

    Actually u can do it easier with same the same time limit
    Run a dfs from each cell on the boarder that is 1 and you will mark all connected edges to that cell
    So basically u will mark all connected components that are connected to the boarder
    After that u know that each cell that is 1 and unmarked is part of an island (because it doesn't have a path to any boarder)

  • @cibureh2684
    @cibureh2684 Před 3 lety +20

    “I really hope I don’t refactor and just ruin my code even more” - that always happens 😂 34:16

  • @ahmadrezaazimi5739
    @ahmadrezaazimi5739 Před 7 měsíci

    Astonishing interview question!
    Really enjoyed it!

  • @MrRobotUy
    @MrRobotUy Před 3 lety +2

    You guys are Awesome! 👏🏻👏🏻👏🏻👏🏻

  • @aUCLZlstrBh5upnFr7OmhNHag
    @aUCLZlstrBh5upnFr7OmhNHag Před 3 lety +73

    as an ex-Googler, you must interview him in Angular >: )

  • @sarthakjain3476
    @sarthakjain3476 Před 3 lety +6

    My approach :- do bfs/ dfs from 1s present in first row, last row, first column and Last column and then change all the ones encountered to -1
    After BFS/DFS
    Convert all 1s to 0 and -1 to 1

  • @lrkiller9227
    @lrkiller9227 Před 3 lety +1

    I was waiting for this😎🤙

  • @JonnieR
    @JonnieR Před rokem

    I value the thought process.
    The question about how to make sense of the 1 in the second row literally is a eureka moment that his code isnt gonna work.
    Because he still hasnt done the inner most square.
    The follow up question was litterally a saving grace lol

  • @TheEW
    @TheEW Před 2 lety +17

    I think there's a problem with the way he's naming the keys such that i = 1, j = 12 for example gives the same key as i = 11, j = 2 when they are two completely different coordinates. I think adding a space in between the i and j would solve this immediately though.

  • @claudiooliveira698
    @claudiooliveira698 Před 3 lety +16

    I love how Ben starts randomly laughing without any apparent reason

  • @NoodleFlame
    @NoodleFlame Před 2 lety

    I tried this one on your website, it was actually suprisingly easy. Basically just a stack based flood fill.

  • @siddarthchalla522
    @siddarthchalla522 Před 2 lety +1

    Below is my solution in js. I don't see anyone suggesting a DP solution so wanted to see if I'm thinking about the problem correct/am missing something.
    I solved this using DP.
    1. One pass from top left to bottom right to mark nodes as "connected" based on whether top/left neighbors are borders/connected
    2. One pass from bottom right to top left to mark nodes as "connected" based on whether bottom/right neighbors are borders/connected
    3. Nodes marked as "not connected" after both passes are changed to 0
    This is O(N*M) time and O(N*M) space but can be reduced to something like O(M) by only storing relevant rows for DP.
    Code is below:
    function removeIslands(map){
    const isConnected = []
    for(let i=0; i= map.length-1) return map[row][col] === 1
    if(col === 0 || col >= map[0].length-1) return map[row][col] === 1
    // now determine if neighbors are connected
    if(isConnected[row-1][col]) return true
    if(isConnected[row+1][col]) return true
    if(isConnected[row][col-1]) return true
    if(isConnected[row][col+1]) return true
    return false
    }

  • @nachiketshelar8114
    @nachiketshelar8114 Před 3 lety +3

    I love these data structure problems! Because there's no single unique way to solve them (I do not refer to PvsNP)

  • @tommike2548
    @tommike2548 Před 3 lety +49

    Did the test cases include matrices with more than 10 rows/columns? I expected the way Ben named the key to cause bugs. For example key "123" is ambiguous as (12, 3) and (1, 23) both map to this key - and that would mess up the visited map logic.

    • @hotharvey2
      @hotharvey2 Před 3 lety +4

      exactly what I thought, he should have used tuples as keys

    • @dimabolgov99
      @dimabolgov99 Před 3 lety +2

      Yep, there must be a separator, like {}:{}

    • @mamazu1995
      @mamazu1995 Před 3 lety

      Or just use the correct datatype which would be a set. Then you don't have to assign it a value that is never used.

    • @clem
      @clem  Před 3 lety +41

      Great catch! As written, Ben's algorithm would indeed fail for some large matrices. We'd have to add a delimiter (like "-" or "|") between the indices in the key to correctly handle this.
      I've added a test case on AlgoExpert that would have failed Ben's algorithm!

    • @martinharris4416
      @martinharris4416 Před 3 lety +6

      @@clem Did you just call Ben a failure ? 😂

  • @dane2565
    @dane2565 Před 2 lety

    for the string formatting thing (using '{}{}'.format(i, j) as a dict key):
    you can use tuple (i, j) as the dict key as tuples are hashable. No need for a key generator function or that fancy stuff
    also for your rec() function. you can create a global BORDER_ISLANDS dict and pass this in as a default argument:
    BORDER_ISLANDS = dict(). # global object
    def rec(i, j, border_islands=BORDER_ISLANDS): # default arg is this global object and so all recursive calls use the same object
    you wont need to pass this object around and all functions will use the same single instance of BORDER_ISLANDS.

  • @kaushikdey6333
    @kaushikdey6333 Před rokem

    These interviews are great for revising topics

  • @farhan787
    @farhan787 Před 3 lety +8

    Clement I haven't seen the full video but the first problem was literally easy medium-ish. For boundary elements you can just do DFS/BFS and mark the connected elements as connected to the boundary of matrix and then finally iterate the matrix and remove the 1s not connected with the boundary 1 elements. This would be O(rows * columns) time and space complexity. We might optimise the space further.

  • @andrewpersaud4144
    @andrewpersaud4144 Před 3 lety +10

    Saying I'm not gonna be asked to find max element in an array at my interview

  • @ryanpark3523
    @ryanpark3523 Před 3 lety +2

    This is so cool Clement~! EZ MEDIUM ADVANCE?! XD Thx for a gr8 content!

  • @sdcair
    @sdcair Před rokem

    I'd use a bfs by putting all coordinates on the edge that have value 1 into a queue. Then work off the queue, mark each discovered cell (eg. by setting the value to 2), and add neighboring cells with value 1 to the queue. Then just iterate over the matrix and set every 1 to 0, and every 2 to 1. Time complexity O(n * m), extra space complexity O(1).

  • @mexicanmax227
    @mexicanmax227 Před 3 lety +6

    Hey Clement! What are your thoughts on quantum computing (Open source or through a provider like AWS)?

  • @dflo34
    @dflo34 Před 3 lety +20

    Ben laughing at his ratings has me dying lol

  • @Haibrayn42
    @Haibrayn42 Před 2 lety

    I think the simple solution if not concerned by the storage would be to have a matrix of zeros then loop over the borders. Wheb finding a 1, add its coords to a list and set the 1 in the new matrix. Then find the neighbors recursively (just traverse for the coords not in the list or dict). Then return the new matrix

    • @sebastiandobrowolski2343
      @sebastiandobrowolski2343 Před 11 měsíci

      yea thats what I thought too. Going along borders and then recurring along all the 1s will give you the image without the islands

  • @codedbychavez8190
    @codedbychavez8190 Před 3 lety +1

    Love this Clem!

  • @thesnups
    @thesnups Před 3 lety +64

    Great interview! Really enjoyed this question. I have one question about the code -- shouldn't there be a comma in the key? Like "0,0" instead of "00"? The way the code stands, the key could refer to multiple different coordinates. For example, "2020" could refer to both positions (20,20) and (202,0).

    • @prakanshmishra9004
      @prakanshmishra9004 Před 3 lety +12

      Yep, I was thinking about the same thing as soon as he wrote that. The sizes of the matrix in the test cases must have been small enough that there were no such cases. We will need it as soon as we enter two digits. 110 -> 1,10 or 11,0

    • @user-on1rf1oj9z
      @user-on1rf1oj9z Před 2 lety +11

      I`s better to use a tuple as a key here

    • @MorenoGentili
      @MorenoGentili Před 2 lety +1

      @@user-on1rf1oj9z or a Point

    • @ehsankabirirahani9733
      @ehsankabirirahani9733 Před 2 lety +2

      @@user-on1rf1oj9z tuple is an option or just add a dot in between {}{}. "{}.{}".format(i,j). Is there any advantage for string as a key or tuple in terms of hash collision?

  • @biggbossfootage
    @biggbossfootage Před 2 lety +71

    I think there is 1 small bug in the code I see which went unnoticed. The key for border_islands should not just be 'ij' this would not work with double digits. The key should probably have a separator in between i and j. Also, this should be caught by the test cases. Please add more tests Clement.

    • @mohsenfaghihy7334
      @mohsenfaghihy7334 Před 2 lety +6

      I was surprised when the host didn't noticed that

    • @axaxaxaxaxaxax33
      @axaxaxaxaxaxax33 Před 2 lety +15

      Another solution would be to add the key as a tuple
      key[(1,2)] = True

  • @lancemarchetti8673
    @lancemarchetti8673 Před 2 lety

    This was so cool to watch!

  • @manhalrahman5785
    @manhalrahman5785 Před 2 lety

    I just solved this on AlgoExpert by myself and came back to this video to get a feel of how this question would've felt in the coding interview

  • @anonymoustv8604
    @anonymoustv8604 Před 2 lety +9

    Clement be like
    "I don't mean to interrupt you between your thought process, but, ANYWAYS Imma just interrupt you" Lmao, Clement is the best

  • @avonray9147
    @avonray9147 Před 3 lety +12

    There was a potential issue with the key. The key for the border_islands dictionary was just concatenation of the row & col indices. So, the index (1,21) and index (12,1) for instance will have the same key "121", which may mess up the visited checking for some different test cases.

    • @clem
      @clem  Před 3 lety +14

      Great catch! As written, Ben's algorithm would indeed fail for some large matrices. We'd have to add a delimiter (like "-" or "|") between the indices in the key to correctly handle this.
      I've added a test case on AlgoExpert that would have failed Ben's algorithm!

    • @Hobolover57
      @Hobolover57 Před 3 lety +4

      @@clem could also use a tuple of the indices as a key. my world was changed when I learned this was possible.

  • @manishsharma2211
    @manishsharma2211 Před 5 měsíci

    Clement is such a good interviewer 👌

  • @antonshelest4455
    @antonshelest4455 Před rokem

    Thank you guys for honest mock interview!

    • @antonshelest4455
      @antonshelest4455 Před rokem

      But not all interviewers are so helpful and eager to hire even when candidate goes in wrong direction at first :)

  • @mexicanmax227
    @mexicanmax227 Před 3 lety +3

    Question for Ben and Clément! Can y’all each make a video for best practices in React in front end and best practices in Python for algorithms?

    • @Zeegoner
      @Zeegoner Před 3 lety

      Give up

    • @mexicanmax227
      @mexicanmax227 Před 3 lety

      I’m currently learning JavaScript and html/css real quick. Picking up some best practices in general (code for scalability and multi-application use, and keep it organized. Also learning that extensions are also best practice (really fun 😂) and help speed up the coding/learning processes/efficiency process. Also I think green text (don’t know what it’s called yet) is good for organizing your code and if you ever need to come back to it later.

  • @archmad
    @archmad Před 3 lety +36

    This is the reason i hate interviews like this. I could solve this for sure just might take a week

    • @brookei7707
      @brookei7707 Před 2 lety +13

      It will come to me while I’m washing my hair in the shower

    • @damf7106
      @damf7106 Před 2 lety +2

      and this is why you wont work in google

    • @archmad
      @archmad Před 2 lety +12

      @@damf7106 true, but i already have 6 figure salary, so i am good.

    • @damianrecinski7329
      @damianrecinski7329 Před 2 lety

      @@archmad you work as a programmer?

  • @akshaylalwani6752
    @akshaylalwani6752 Před 2 lety +1

    Just my approach to it would be.
    For (1 to less than length-1){
    For(1 to less than length of inside array -1){
    Check side ways neighbour and up and down neighbour by -1 +1 on index if it's 1,
    If all checks prove its island, replace it with 0
    }
    }

  • @kasiruyamagata7716
    @kasiruyamagata7716 Před rokem

    Basically
    We have to go through all the boundary elements and look for the 1 connected with it and change them to 2. Later we will check if element is 2 change it to 1 and element is 1 change it to 0.

  • @stephanejanel3389
    @stephanejanel3389 Před rokem +5

    Interesting! However there is a bug in border_island_key function if one of the dimensions of the matrix exceeds 10. The key is ambiguous for (1, 11) & (11, 1) for instance (which both give '111').
    Just inserting a space between both indexes will solve the issue.

    • @nik_nsk
      @nik_nsk Před 9 měsíci

      I was just going to write a comment about that too 😀

  • @cpop6344
    @cpop6344 Před 2 lety +4

    Hahaha Clement was being nice to Ben when he said he would be a lean hire and it went to Ben's head so Clement had to stop being nice and just be honest with him 😂

  • @Gokuroro
    @Gokuroro Před 2 lety

    Maybe a recursive function for outside-in matrix which generates the next inside border using an AND between the current border and the next one would do the trick, at least for "clearing" the islands. To get a "removed islands" matrix would need some more planning though.

  • @TheManyfaced
    @TheManyfaced Před 3 lety +1

    I can see an Algo Expert graph-related ad coming :D

  • @abhishekm3752
    @abhishekm3752 Před 3 lety +3

    I dont know why am i here, but i like it.
    I'm 30 and randomly started liking coding/software development and got reminded of C++ programs i made in school days😂
    Can i make money in this field in a year or two if i start today? I m very much tempted

    • @Randoperson45
      @Randoperson45 Před 3 lety +1

      Obviously itd be better if you had a 4 year CS degree but if you self teach web dev or something and have some college education, its possible to find a job in this field and make pretty dam good money

  • @alefsdev
    @alefsdev Před 3 lety +3

    Do it with someone who is a beginner to data structures

  • @AntidotAleks
    @AntidotAleks Před rokem

    Before watching the video tried to make it. I spent, maybe, half an hour on this. I made a copying of the matrix ('result' matrix). with values -1, 0, 1. -1 -> unknown, 0 -> no land, 1 -> land. Edges are copied and the middle is filled with -1. in "while(true)" it goes through every -1 value and checks if there is value 1 on the original matrix at its location and near it on the result matrix then it puts 1 on the result. When the cycle can't put 1's anymore, it breaks from the cycle and replaces all -1's with 0's on the result. There were faster variants, but this one was simple

  • @TamasSzebeni
    @TamasSzebeni Před 2 lety

    I would go with sets, storing surrounding coordinates too. whole matrix as a set of islands where each has their main land and their surrounding field if it is a 1, by holding the coordinates. Start with set of islands on the border, for each of them iterate over the other sets and merge if overlaps. Iterate while the number of set changes, otherwise terminate. It will result the set of border island coordinates and remain those coordinates which are not connected to border. O(n*m) as the matrix needs to be processed.

  • @loganrussell48
    @loganrussell48 Před 2 lety +5

    Immediately after seeing him make the key by concatenating string representations of integers, I knew there would be an issue but sadly the test cases didn't cover it somehow.
    But basically, if *i* = 1, *j* = 11, the key generated for that is identical to the key generated when *i* = 11, *j* = 1. So basically he has no way to differentiate them, right?
    steps to generate test case his code will fail:
    1. create a 13x13 matrix of zeroes
    2. set matrix[1][11]=1
    3. set matrix[11][1]=1
    4. set matrix[12][1]=1 to connect the previous to the border
    since the step 3 tile is connected to the border, the key "111" is in the *border_islands* map, and this will prevent the tile at *matrix[1][11]* from properly being set to 0 as it should.
    if my logic or reasoning is wrong, let me know.

    • @pabloshi4863
      @pabloshi4863 Před rokem

      I noticed this right away. Actually I expected Ben would perform much better than this. If I were the interviewer, my opinion on the aspect of the algorithm would be no/weak hire. But he communicates so well, hence overall I'd give a weak hire / hire.

  • @josephwilliamcosta
    @josephwilliamcosta Před 3 lety +53

    This guy is an incredible programmer and his mind is a logic engine.

    • @dreerable
      @dreerable Před 2 lety

      @Anna Meredith he meant these lol

  • @cafin4908
    @cafin4908 Před rokem

    Simple and straightforward - DFS from each position on the border if its marked as 1 and mark everything in the dfs as 2. Then remove all 1 from the matrix and convert all 2 back into 1. O(n) time O(1) space.

  • @rybakostis
    @rybakostis Před rokem +1

    Here would be my approach:
    Create a set.
    Walk the perimeter and call explore() function for each cell of the perimeter. The function would add the cell index to the set and call itself on all adjacent cells. If passed cell is 0 or is already in the set, the explore() function will just return.
    Once it is done, walk the matrix and convert all 1 that are not in set to 0.