Google Coding Interview With A Competitive Programmer

Поділитися
Вставка
  • Опубліковано 1 січ 2025

КОМЕНТАРІ • 2,7 тис.

  • @clem
    @clem  5 років тому +461

    Hope you all found this coding interview insightful! And check out the video we made on Errichto's channel where we chatted about coding interviews, Math in Software Engineering, and more: ua-cam.com/video/Y8VeyLG3NhY/v-deo.html

    • @danielk8452
      @danielk8452 5 років тому +2

      Thank you, mock interviews like these are really great to share :)

    • @akshatgupta1904
      @akshatgupta1904 5 років тому +1

      Sir, I wanted to ask that can we practice competitive programming with Python or our only choice to go for CP is through C++ /Java?

    • @aetherllama8398
      @aetherllama8398 5 років тому +11

      I was inspired by your midpoint idea to make an O(N^2) general solution. Two lines with the same length and midpoint form a rectangle.
      diagonals = {} #python dictionary
      count = 0
      for Point A in points:
      for Point B in points:
      key = [ length(A,B), midpoint(A,B) ]
      If diagonals{ key } == NULL
      diagonals{ key } = 0
      count += diagonals{ key }
      diagonals{ key } += 1

    • @jishnugopakumar
      @jishnugopakumar 5 років тому

      cant we just rotate all those points 45 degrees in any direction and make the diagonal lines horizontal or vertical.
      So we could use the same old code for searching diagonal rectangles also.

    • @aetherllama8398
      @aetherllama8398 5 років тому +2

      @@jishnugopakumar No. They are the diagonals of the rectangles, they could have any angle so rotating them 45 degrees would not make them horizontal or vertical.

  • @Errichto
    @Errichto 5 років тому +11032

    Spoiler: Google didn't hire me.
    Apparently, Clement is an ex-Google engineer and this wasn't a real interview :(

    • @clem
      @clem  5 років тому +2523

      Double-spoiler: if this _had_ been a real interview, I would have given you a Strong Hire.

    • @SajeelCodes
      @SajeelCodes 5 років тому +399

      You are hired, Errichto, start your job from 32 April, 1985 Lol

    • @raghav4711
      @raghav4711 5 років тому +153

      @errichto: holy shit dude, you fucking killed the interview!

    • @wulymammoth
      @wulymammoth 5 років тому +203

      Raghav hahaha! He’s #50 on Codeforces (grandmaster). It’s like watching a superstar at their respective sport. Even with sheer hard work, it’s nearly impossible to reach that level without talent as well. Fortunately that’s not the expectation that top tech has. Google does employ Petr, though, who has always been top 5 in Codeforces

    • @bunchofiitians5900
      @bunchofiitians5900 5 років тому +331

      Lol, If there would have been a Google interviewer who didn't hire Errichto, Google would have fired the interviewer.

  • @Bharanivijay00700
    @Bharanivijay00700 5 років тому +4317

    I just learnt to display "Hello world" in python. Now this video is recommended to me.

  • @TheQA247
    @TheQA247 5 років тому +1481

    Before Interview: Confident in Python skills
    During Interview: Draws a complete blank when given a code challenge
    After Interview: Crawls in hole and contemplates current career choices

    • @yesaeses
      @yesaeses 5 років тому +46

      Learn c++ everything will make sense

    • @coffeeenthusiast8774
      @coffeeenthusiast8774 5 років тому +2

      @@yesaeses should i learn C/java first before jumping to C++?

    • @yafi2475
      @yafi2475 5 років тому +26

      @@coffeeenthusiast8774 No need.

    • @evgiz0r
      @evgiz0r 5 років тому +30

      Just try completing small to medium projects almost alone without googling how to for loop. If you are programming with copy-paste and 17 tabs open explaining the basic methods, then try without those.

    • @Jp-ue8xz
      @Jp-ue8xz 4 роки тому +13

      @@coffeeenthusiast8774 NO JAVA. Jumping from Java to C/C++ makes people whine when faced to pointers, pointer arithmetic, and other abstract concepts. I've seen it multiple times in 2nd year students who came from different colleges. You should learn C/C++ first, then go higher-lvl langs. And I wouldn't jump into Java unless I'm literally starving and can't get any other job xD

  • @DaMainDude
    @DaMainDude 3 роки тому +643

    "I'm quite good at computational geometry" - well there is something I didn't know existed.

    • @Ecell_2023
      @Ecell_2023 3 роки тому +10

      Dude the first question was a basic combinatorics question in maths its extremely easy to implement

    • @quadroninja2708
      @quadroninja2708 3 роки тому +4

      A lot of games are filled with it

    • @Tetrahfy
      @Tetrahfy 3 роки тому +1

      Dude get your fucking shit togetther

    • @geckogeico2212
      @geckogeico2212 3 роки тому +2

      @UCvxbsNm-80mD0BggH-95rew go fuck yourself

    • @HyrumCooper
      @HyrumCooper 3 роки тому +42

      @@Ecell_2023 I know this is an older comment, but just wanted to mention that the first question wasn't actually a combinatorics problem because the points aren't necessarily arranged in a grid. If we could assume that the points formed an m x n grid, containing every (i, j) such i < n and j < m, (as in the example drawing), then we could solve it using combinatorics, but this would be a false assumption.

  • @Hallden_
    @Hallden_ 4 роки тому +631

    Great idea for a video! His knowledge, the idea for the format of video and you as the interviewer is well worth the (as of writing this) 1.5+ million views. Nice work!

    • @poisson6673
      @poisson6673 4 роки тому +2

      Kalle sir omg! U r the python goat

    • @techiedev7290
      @techiedev7290 3 роки тому

      OMG Kalle Hallden I love your videos!

    • @loudsquad2324
      @loudsquad2324 3 роки тому +1

      IMO: Get Kalle on here Clem :)

  • @simonbachmann2120
    @simonbachmann2120 5 років тому +4307

    writing code in google docs brings cancer to my eyes

    • @encrypted983
      @encrypted983 5 років тому +15

      Lol

    • @mattmmilli8287
      @mattmmilli8287 5 років тому +59

      Simon Bachmann IKR, there are a dozen websites for this now or live share in vscode even

    • @PSUC3
      @PSUC3 5 років тому +133

      Interviews are conducted in pseudo code, so this is the most realistic way to conduct it.

    • @cebulagner
      @cebulagner 5 років тому +35

      I had an SQL interview there, yeah its only in Google Docs and you have to imagine everything (scheme, db name, fields etc..)

    • @xxstealerxx
      @xxstealerxx 5 років тому +12

      @@PSUC3 no it's not. Even on a whiteboard it wouldn't be as cancer as this as you have more width and can eyeball tabbing

  • @hoagiesandwich
    @hoagiesandwich 5 років тому +235

    My self-esteem doesn’t need this

    • @AbCd-zo5tb
      @AbCd-zo5tb 3 роки тому

      Thank god i am not the only one.. phewww

  • @saiprajeeth
    @saiprajeeth 5 років тому +394

    For every aspiring software engineers out there, watch the last 5 minutes of this video. It is GOLD and it will provide answer to all your insecurities about problem-solving abilities.
    I thank Clement and errichto for making this. Looking fwd to watch more

    • @clem
      @clem  5 років тому +60

      Love this comment, and I’m really glad you found the video so helpful!

    • @nudeood
      @nudeood 5 років тому +15

      @@clem Thank you for this video and advice. I just dropped out of a computer science PhD to get a developer job and I feel so out of practice with coding. I immediately thought of ways to approach the first problem but I don't think I could engineer a working solution with good use of data structures in a high-pressure interview. You saying that you wouldn't expect a working solution really put my mind at ease.. even so I definitely need to do some practice

  • @DavidAttenbraai
    @DavidAttenbraai 4 роки тому +579

    5:06 The only part I can do...

  • @AmanDeepSingh-xe9of
    @AmanDeepSingh-xe9of 5 років тому +154

    Didn't follow much of the problem, but the composure of this guy is something to learn from. I'm super impressed :)

    • @ShaliniNegi24
      @ShaliniNegi24 2 роки тому

      +1

    • @PegasusTenma1
      @PegasusTenma1 Місяць тому +2

      He's an LGM on Codeforces, have to have nerves of steel for that

  • @peterlucas6732
    @peterlucas6732 5 років тому +1779

    "I'm quite good in computational geometry"... you sure you want to ask me such a stupid question?

    • @ItsAllEnzynes
      @ItsAllEnzynes 5 років тому +107

      Peter Lucas for someone who is good at computational geometry he sure picked a really complex solution...
      For no diagonals where n = amount of rows and m = amount of columns the solution is (n-1)!(m-1)!
      With diagonals it’s (n-1)!(m-1)!+ (n-2)!(m-2)!
      You could optimize both those equations pretty easily with some simple dynamic programming and get the runtime to O(n).
      Edit: my solution answered the wrong question incorrectly. Rip.

    • @kieranhorganmallow
      @kieranhorganmallow 5 років тому +65

      @@ItsAllEnzynes You're given a list of (x,y) coordinates. Not the dimensions of a grid.

    • @Bunny99s
      @Bunny99s 5 років тому +15

      @@kieranhorganmallow Right. Also no one said the coordinates are limited to whole numbers, just that they are axis aligned. So the points
      (10.123, 5.321), (10.456, 5.321), (10.123, 7.5), (10.456, 7.5)
      do form an axis aligned rectangle.
      James "solution" seem to only address the number of rectangles in a full grid of n x m points. This was not the question. If you go back to 03:20 they actually make it clear that in order to form a rectangle you need 4 points in the array. They even discussed the example of deleting the middle top point in which case there is only one instead of 3 rectangles. Rectangles could even overlap / intersect.
      Optimising factorial calculation is also pretty pointless. It would only work with relatively small numbers anyways. The result of "12!" is already larger than what a 32bit uint can hold. (21! doesn't fit into a 64bit integer)
      About how to speed up the actual solution, the only thing I could think of is if you have many points it might be worth to first sort the points on one axis (at least for the axis aligned case).
      The arbitrary rectangle case could possibly simplified by first constructing all "edges" and put them in a map based on the slope. Therefore you can easily find parallel edges as well as perpendicular edges. Though the worst case is probably still O(n^4).

    • @ItsAllEnzynes
      @ItsAllEnzynes 5 років тому +13

      Bunny83 Bunny83 my solution was also wrong. The actual solution of a grid formed with nxm points would be f(n,m) = (((n-1)/2)*n) * (((m-1)/2)*m))
      I had generating functions in my mind when I was solving and got careless :|
      Also yeah, I jumped the gun and skipped though the video, my solution is not for the problem specified

    • @radrad5119
      @radrad5119 5 років тому +3

      @@ItsAllEnzynes I was thinking something like that as well... except mine was something along the lines of sum(width-1)*sum(height-1) where sum is just the summation of all the numbers leading up to it. It kinda looks like his in the video was more versatile. Like it was actually counting the rectangles instead of calculating them. At the start though I was also thinking about factorials before realizing they weren't working for me.

  • @artkuts4792
    @artkuts4792 5 років тому +1571

    So basically you're being challenged with leetcode exercises, pass the interview and then end up in a team writing yet another JS/Android framework where you won't use any algorithms at all.

    • @DanAaronWhite
      @DanAaronWhite 5 років тому +273

      100% accurate

    • @rooberry-music
      @rooberry-music 5 років тому +248

      How i wish tech tests would reflect the development work you'd be expected to do on the job. You don't interview a doctor by asking them to dissect a frog. Sigh.

    • @TonyDemetriou
      @TonyDemetriou 5 років тому +220

      I've been team lead on web/js/PHP/MySQL projects, and while I agree that you don't NEED to use algorithms like this to get the job done, I disagree that you won't use it.
      I've never searched for rectangles, but the same general problem solving mindset is useful for other real coding situations where you need to loop back over the same lists of data.
      You use the skills that you practice. About once a month I'll use this sort of mindset to write something more efficient, or to simplify the logic, or to mentally figure out the efficiency of a database query before running it to know if it'll impact service. Even then, mostly we'd be fine without doing this. But maybe once every six months I'll write an algorithm that makes a big impact, such as delaying the need for a $2M hardware upgrade by three years.
      The thing is.... You gotta know how to write solutions like this before you're able to know where you can use them.
      I've never learnt this because I think it'll be useful. I learnt it because it was part of my course (that I never thought I'd use) and then continued learning because it's interesting.

    • @PerisMartin
      @PerisMartin 5 років тому +131

      The point of this exercises is not to find out if you know algorithms or not, it is to test your attitude when facing a challenge and your problem-solving thinking process.

    • @ItsAllEnzynes
      @ItsAllEnzynes 5 років тому +10

      Tony Demetriou Tony Demetriou Ehh... especially in the case of Web Dev
      Id rather have:
      4 engineers with a deep understanding of JS/PHP/MySQL and no sense of algorithms and 1 person who can write complex algorithms than who knows nothing about the tools used but is good at algorithms
      Instead of:
      1 person who deeply understands the js/PHP/MySQL and 4 people who know can write complex algorithms but know very little about the actual task
      Ofc the argument can be made that teaching the algorithms people js/whatever is easier than teaching the js people to write algorithms, but depending on the task is that time investment really worth it? Wouldn’t you be worried those algorithm people would move on to bigger and better things?

  • @antonistrychalski5125
    @antonistrychalski5125 5 років тому +905

    This ended up being Errichto giving tutorial on algorithms to Clement

    • @yunjiehong4649
      @yunjiehong4649 5 років тому +24

      Antoni Strychalski And Clement is fired cuz he didn’t hire Errichto aha

    • @smcalpha
      @smcalpha 5 років тому +15

      You can obviously see that Errichto has tons of experience in solving these kind of problems

    • @abrarulhaque4614
      @abrarulhaque4614 4 роки тому +25

      well duhh! Errichto is not just any competitive programmer you know? he is one of the best in the world out there so yeah obviously this was gonna happen

    • @saedyousef
      @saedyousef 4 роки тому +3

      He is familiar with algo since he was 16
      Just watched the interview video on Joma Tech channel
      This guy is an algo beast

    • @iamnoob7593
      @iamnoob7593 3 роки тому +1

      @@saedyousef gennady korotkevich - Let me introduce myself.

  • @FredericJardon
    @FredericJardon 5 років тому +64

    For the second part, the diagonals were the way to go. If two segments intersect in their middle and have the same length they are the diagonals of a rectangle. Just modify the code for the first solution by using the center of the segment and its length as the key in your map. Solution would be O(n²).

    • @visskiss
      @visskiss 2 роки тому

      Came here to say this.

    • @rajeswarynarasimman3728
      @rajeswarynarasimman3728 Рік тому

      + The diagonals should not have a degree of 0 between them.

    • @SteezyMD
      @SteezyMD Рік тому

      I agree that this is the most intuitive solution. Not sure if this approach could get better than N^3.
      I imagine looping over pts A and making lines with pts B, then for each C, we check if the distance to the midpoint of AB is equal to 1/2 distance of AB. Then we check for pts D in hashmap if Cx-midABx == midABx -Dx and Cy - midABy == midABy - Dy
      Im new to complexities but it seems like N^3. Maybe checking those D coordinates with hashmap and if statements makes it N^4

  • @JLRide
    @JLRide 3 роки тому +23

    This was entertaining to watch. I liked how Errichto remained so calm and composed during the mock interview. I've had an interview recently where I was asked an extremely simple question and I blankly stared at the screen for minutes before regaining my composure and completing the problem. I have a lot of respect for you and many of the software engineers who have done excellent during these interviews with those large companies. It takes a lot of knowledge, skill, and composure. Respect.

  • @Fgcbear15
    @Fgcbear15 5 років тому +1435

    This gives me anxiety even though I have a job now.

    • @tulsikhatri8694
      @tulsikhatri8694 5 років тому +8

      What type of ?

    • @Fgcbear15
      @Fgcbear15 5 років тому +33

      @@tulsikhatri8694 Process Eng. My degree is not Comp Sci. Interviews in gen still give me anxiety.

    • @charles-y2z6c
      @charles-y2z6c 5 років тому +115

      I am old, have contracted short term 3-9 month projects my entire carerr of 40 years, i work 11 months a year. I have done so many interviews. My advice? You are a smart person, just go on as many interviews as you can with the atittude you dont care. I guarantee you will get hired.

    • @ScylockeX
      @ScylockeX 5 років тому +9

      @@charles-y2z6c I'd be fascinated to hear more about a career like that. What have you been doing all your life?

    • @saddiqjeelani563
      @saddiqjeelani563 4 роки тому +4

      @@charles-y2z6c It means a lot to hear this right now. Thank you.

  • @michaeltolsma7717
    @michaeltolsma7717 5 років тому +1487

    When the Google interviewer is completely overwhelmed by the interviewee...

    • @Adam-cn5ib
      @Adam-cn5ib 5 років тому +25

      that's a bad thing cause it makes teamwork hard. It's better that everyone is on the same level

    • @skilz8098
      @skilz8098 5 років тому +11

      @@Adam-cn5ib Not true because you need someone to lead the herd...

    • @milanstevic8424
      @milanstevic8424 5 років тому +25

      @@Ricardo-C what I'm seeing as horrible is that there are people in power with an opinion like this. that means that hindering more capable staff is not simply a malign action from the position of envy, it is also a deeper mental organizational fallacy.
      from what I've experienced so far, yes, there are two fallacious modi when it comes to evaluating peers/employees:
      "I hate that you are so much better than me, I won't help you, in fact I see you as a threat to my status."
      "I believe that you should be a greater team worker than this, you should slow down a little for the others to catch up with you, you're just showing off and are too laid back, thus your behavior is unacceptable."
      I can accept the former, because it is always served incognito and can be deemed a personal fault. but the latter one is typically a formal statement, as evident by that comment, the guy is absolutely rational and firmly believes in this. this is unfathomable in my mind, yet quite a lot of big and essential organizations are fundamentally run by this mantra. horrible.
      no wonder the world is a mediocre place at such large scales. regression to mediocrity is a real, human-induced, effect. it definitely helps to explain the "bozo horizon" as well.
      for more on "bozo horizon": blogs.harvard.edu/waldo/2012/07/27/the-bozo-event-horizon/

    • @milanstevic8424
      @milanstevic8424 5 років тому +9

      @@Ricardo-C Though the amount of likes you got, restores my faith in humanity to an extent.

    •  5 років тому +1

      @@milanstevic8424 that's deep in so many ways.
      Think about how this relates to minorities and victmism

  • @arunarkamukhopadhyay6443
    @arunarkamukhopadhyay6443 5 років тому +81

    Clement: never interviewing a Competitive Programmer again!!

  • @shreyanshsingh2627
    @shreyanshsingh2627 4 роки тому +27

    I like the confidence @Errichto has here. He very clearly stated he is good at computational geometry and even asked for something harder. Not for nothing grinding Topcoder and Codeforces for years is a pretty big deal!

  • @mikejohnstonbob935
    @mikejohnstonbob935 5 років тому +306

    that moment when your interviewee finds a more elegant solution than your best solution in the first 15 min of the interview

    • @ZimZam131
      @ZimZam131 5 років тому +8

      Actually, I think they had an n^2 solution. Errichto's solution was n^3. But it was pretty clever, that's for sure.

    • @numbdigger8558
      @numbdigger8558 5 років тому +26

      @@ZimZam131 Dude, it's clearly O(n^2 log n). Or even O(n^2) with hash table.

    • @bigsassyster
      @bigsassyster 5 років тому +4

      This happens. Sometimes the interviewer will have to think for a second if what the interviewee wrote actually works because they wrote something that is more simple than what they wrought.

    • @iurigrang
      @iurigrang 4 роки тому +3

      I doubt it was better than the interviewers solution. If you simply iterate through diagonal points, checking if the other points are on the hash table, that gives us O(N²) time and O(N) space, which is probably the solution he had in mind, since it's pretty simple.

  • @hoffie7456
    @hoffie7456 5 років тому +206

    I don’t know a single lick of coding, how did I get here

    • @mikeg8343
      @mikeg8343 5 років тому +4

      Ex Hoffie I’m not even good at math

    • @mariqstigler2197
      @mariqstigler2197 5 років тому

      😂

    • @Michi9609
      @Michi9609 5 років тому +4

      I guess Google wants you to learn coding, Demand is high and the future system will need many people capable of coding

    • @hoffie7456
      @hoffie7456 5 років тому +1

      Adestrix96 big brain answer

    • @Michi9609
      @Michi9609 5 років тому

      @@hoffie7456 thx ^^"

  • @pratiksingh8650
    @pratiksingh8650 5 років тому +34

    What a guy! The way he approaches the questions is just amazing.

  • @thane9
    @thane9 5 років тому +275

    I haven't written code in 40 years, but as a mathematician I could definitely walk someone through several ways to execute this counting, both cases. This was very interesting to see the code/shorthand in a modern programming language.

    • @criptik5208
      @criptik5208 3 роки тому +9

      What have you been doing since 40 years then

    • @DoccyStars
      @DoccyStars 3 роки тому +78

      @@criptik5208 not coding

    • @robinder_
      @robinder_ 3 роки тому +105

      @@criptik5208 probably counting

    • @g33xzi11a
      @g33xzi11a 2 роки тому +4

      I guess C++ is modern by comparison to BASIC.

    • @stevenborik3026
      @stevenborik3026 2 роки тому +2

      @@robinder_ day made thank u

  • @batmanarkhamcity3684
    @batmanarkhamcity3684 3 роки тому +100

    no one
    literally no one
    errichto: ” Are you sure this problem is hard enough “ ? 😂😂

  • @m13m
    @m13m 5 років тому +23

    The way he was solving the problem it felt like a poet reading his poem. Thanks Clement for an awesome episode.

  • @shrad6611
    @shrad6611 5 років тому +54

    I really love the attitude of errichto even with so special talent he dont have pride at all love you errichto

  • @sudosai
    @sudosai 4 роки тому +164

    2:10 "i can handle upto 10 dimensions". - Errichto

    • @arkros1
      @arkros1 4 роки тому +2

      and yet right at 22:50 he can't even handle 2 dimensions correctly

    • @justicechukwuemeka8138
      @justicechukwuemeka8138 4 роки тому +41

      @@arkros1 You sound stupid (no offense).

    • @rtubleiii
      @rtubleiii 4 роки тому +11

      Arkros wow so brash of you to say that

    • @chhabisarkar9057
      @chhabisarkar9057 4 роки тому

      @@arkros1 bruuh -__-

    • @thegeekandthegedik
      @thegeekandthegedik 4 роки тому +5

      @@arkros1 what's wrong with the formula?

  • @RimantasLiubertas
    @RimantasLiubertas 5 років тому +28

    Solutions based on fundamental geometry: "hacky". Ok, then.
    Btw, if diagonals intersect in the middle and are of the equal length, you've got a rectangle there. This property is very often used by craftsmen.

    • @ajuc005
      @ajuc005 5 років тому +2

      I was screaming at the screen. But then I'm doing 2d vector math all the time cause I make games :)

    • @359Aides
      @359Aides 4 роки тому +2

      Had the same thought with finding center point of all lines and add them to a map.
      As soon as you find a an entry already contained in the set that should form a rectangle (and there should not be any duplicates +- floating point imprecision).
      Didn't think the implementation all the way through but it should come down to O(n^2)

    • @nacl3470
      @nacl3470 4 роки тому +2

      @@359Aides Yeah, there's an O(n^2) solution. You keep a dictionary mapping each pair to a counter. Then you have N choose 2 rectangles for each N diagonals with the same center and length. Here's a quick python solution:
      def num_rectangles(points):
      diag_dict = {}
      for i in range(len(points)):
      for j in range(i + 1, len(points)):
      p1 = points[i]
      p2 = points[j]
      length_sq = (p1.x - p2.x)**2 + (p1.y - p2.y)**2
      midpoint = Point((p1.x + p2.x)/2, (p1.y + p2.y)/2)
      if (length_sq, midpoint) in diag_dict:
      diag_dict[(length_sq, midpoint)] += 1
      else:
      diag_dict[(length_sq, midpoint)] = 1
      num = 0
      for v in diag_dict.values():
      num += v * (v - 1) // 2
      return num

    • @briceparent593
      @briceparent593 3 роки тому

      I may be wrong, but you could also just check for one square angle. If diagonals meet in their middle and there's a square angle, I believe you have a rectangle. I believe it just adds the same complexity though. (I'm speaking about the problem itself, in woodworking, it would definitely be more precise to compare the measures, unless you have a square that is at least as big as the shape you're checking)

  • @SchweineSchnitzlBallerina
    @SchweineSchnitzlBallerina 4 роки тому +39

    i think, when you connect two dots, there will be a line

  • @stephenhousman6975
    @stephenhousman6975 5 років тому +11

    33:30 What you were looking for is that the 2 diagonals intersect at their midpoint and are the same length. If you are doing this approach the slopes of the 2 diagonals are opposites.

    • @KPkiller1671
      @KPkiller1671 5 років тому

      The slopes are only opposites if the sides of the rectangle are parallel to the x and y axis.

  • @jishudohare9141
    @jishudohare9141 5 років тому +10

    Errichto is a super cool guy, he is totally chill and always ready to help you. He is awesome.

  • @alexnezhynsky9707
    @alexnezhynsky9707 5 років тому +67

    2:34 How many rectangles are formed on the plane... Why are we still here? Just to suffer

  • @shaekmrsyahoocom
    @shaekmrsyahoocom 4 роки тому +36

    "I'm quite good in computational geometry".. when clement reminded only 2 mins left ''Take it easy".... Erricho is a gangster!

  • @ianyname1736
    @ianyname1736 5 років тому +181

    What do you do for a Living?
    Before the interview: I am a software engineer!
    After the interview: I am selling weed niga!

  • @DanT-iu6oc
    @DanT-iu6oc 5 років тому +150

    "I can handle two dimensions"
    IS FOUR DIMENSIONAL BEING

    • @sparky5543
      @sparky5543 5 років тому

      You know, you just confused the heck out of a bunch of viewers :) Most people can barely handle a single dimension! For you coders, think logically beyond what I am saying, it makes sense :)

    • @jan7751-o4w
      @jan7751-o4w 5 років тому

      @@Maca64N May well be a lot more dimensions than that according to some theoretical physicists, but usually we like to think we live in 3 spatial dimensions and that's just about what our brains can handle. Time isn't a spatial dimension - it's just convenient to think of it as a fourth dimension in some cases. When it comes to math any finite amount of dimensions is usually no harder than 2 or 3. Infinite amount of dimensions can be a bit trickier sometimes.

    • @rtubleiii
      @rtubleiii 4 роки тому

      James K. I think there is this animated TedED video that explain dimensions simply. It also mentioned a book and references for further reading

  • @Zzznmop
    @Zzznmop 5 років тому +33

    The awesomeness complexity of this channel is growing factorially. Haven’t even started the video but want to thank you for what I know will be AMAZING content.
    Thanks again Clément and Errichto! :)

    • @clem
      @clem  5 років тому +7

      They should test you on your awesomeness complexity in interviews! Who cares about time and space complexity when you've got awesomeness complexity?

  • @VideoArchiveGuy
    @VideoArchiveGuy 5 років тому +420

    The sad thing is that these gotcha coding interviews weed out a lot of really good experienced programmers who just don't think this way.
    Sure it's great for vetting new college hires, but if say you're hiring a storage engineer they probably haven't done coding like this in decades.
    However, ask the person who does well on this test to write a bit of kernel code and they will likely stare at you with a deer in the headlights look.

    • @guntandy
      @guntandy 5 років тому +33

      isnt this test meant to test more about the algorithm than the syntax knowledge

    • @JayLooney
      @JayLooney 5 років тому +44

      @@guntandy The point is basically that it tests that a graduate of a CS program learned CS, but that knowledge isn't necessarily fresh in someone who has a lot of experience actually doing software development work.

    • @DanAaronWhite
      @DanAaronWhite 5 років тому +65

      I agree 100%. I run my own software dev company and make myself a six figure salary, I’d never be able to pass this interview.

    • @lorenzocabrini
      @lorenzocabrini 5 років тому +77

      Not sure I agree with you there. First, I don't see this as a gotcha question, it is a fully normal algorithm example. Second, I think you make the mistake of treating all software development the same. If you're going to work on AI code, for instance, you are going to have to think this way. If, on the other hand, you are going to write storage code, kernel code or standard business applications, you need a different set of skills.
      By analogy, a nurse a general practicioner and a surgeon all work in the medical field. That doesn't mean they all perform the same tasks or even that they have the same specialized skills.

    • @VideoArchiveGuy
      @VideoArchiveGuy 5 років тому +29

      @@lorenzocabrini The problem is companies for the most part don't work that way. Whether you want to work designing UIs or doing development work on OS internals, you get asked the questions about sorting two dimensional arrays and how to write a program that generates BINGO cards.

  • @sthubbar
    @sthubbar 4 роки тому +11

    Amazing. It is like watching professional athletes. I know they are doing something that I can't and I respect their skills.

  • @hououinkyouma5372
    @hououinkyouma5372 4 роки тому +8

    HOLY SHIT! They're on a whole different level.. I have so much more to learn. Just watching them discussing stuff I have no idea about is freaking exciting!

  • @RedEyedJedi
    @RedEyedJedi 5 років тому +48

    The only time I felt, even slightly smart watching this video, is when I said slope before Errichto did.

  • @benjaminschulz2377
    @benjaminschulz2377 5 років тому +13

    the formula for checking right angle is actually x*x2 + y*y2 = 0 :) You can see this also for his example values:D Also this is calles the dot product or inner product. This is very much differetn from the cross product which is only defined in 3 dimensions and is something different:D

    • @jwy4264
      @jwy4264 11 місяців тому

      Yeah this was a pretty weird mistake

  • @jaydunn6859
    @jaydunn6859 4 роки тому +9

    The UA-cam algorithm letting me know I haven’t got a clue what I’m looking at by showing me an advert for glasses midway through.

  • @manishsharma2211
    @manishsharma2211 4 роки тому +24

    Clement gets surprised every min when errichto writes code😍😍

  • @victornaut
    @victornaut 5 років тому +140

    Why am I nervous?! I'm not even the one being interviewed!

    • @MauriceBoulard
      @MauriceBoulard 5 років тому +6

      so true dude! :) we all feel the same here

    • @andrewilliams1974
      @andrewilliams1974 5 років тому +7

      Cancelling my Google interview opportunity right now.

  • @Spiritusp
    @Spiritusp 5 років тому +12

    His solution to part 1 basically solves part 2, u just need to have the right key (angle + 2 projections).

  • @Xaxxus
    @Xaxxus 5 років тому +229

    It’s stuff like this that depresses me because I’d probably fail the first question in an interview.

    • @IgorAntarov
      @IgorAntarov 5 років тому +82

      Try to solve one algorithm problem every day. In a couple of month you'll be amazed how far you can go.

    • @mikeg8343
      @mikeg8343 5 років тому +40

      Git gud

    • @grizius4123
      @grizius4123 5 років тому +8

      @@IgorAntarov nice advice ty

    • @jthymesthree602
      @jthymesthree602 5 років тому +6

      Mike G This isn’t Dark Souls dude....

    • @CHROMIUMHEROmusic
      @CHROMIUMHEROmusic 5 років тому +15

      check out project euler, its a website with loads of problems like these. I myself must admit I haven't done any in a while but when I did, they were fun and useful

  • @hasantaz7832
    @hasantaz7832 5 років тому +11

    I don't even know programming but I can understand that this guy knows his stuff

  • @TGRRG
    @TGRRG 3 роки тому +6

    In the 2nd problem, after finding the diagonal lines AB, you could attempt to rotate the axes to make the new cordinate systems for all diagonal lines AB, and then run the same logic that you used in the first problem. This way you iteratively run the simpler solution for varying coordinates based on the different diagonal pairs that you discover.

    • @filip14528
      @filip14528 Рік тому

      I think you almost nailed it, but you dont even need to run the simpler solution iteratively. You can use the same map structure but with more information in it. For example map where the angle is the angle and the pair is the pair that defines a line that goes through origo. You get the same O(N^2) time complexity.

  • @brandonfremin6186
    @brandonfremin6186 5 років тому +23

    I think the diagonal midpoint idea was the best approach. You can take every pair of points and calculate their midpoint and distance apart and make a pair where the first entry is the location of the midpoint and the second is the distance between the points. Then use a map to store how many pairs of points have the same midpoint and distance. Then iterate through all of the midpoints to count how many rectangles there are. If there are 6 pairs of points with the same midpoint and distance, you add 6 choose 2 (equals 15) to your answer, and if 1 pair of points have the same midpoint and distance, you ignore it. This should produce the answer in O(n^2) with a hashmap

    • @maartena5169
      @maartena5169 5 років тому +2

      I agree. This is a slightly better version than what I thought of, I just looped through again and again, which is just a longer process for finding a combination of n pairs on a circle choose 2.

  • @Ballltas
    @Ballltas 5 років тому +10

    Best solution that I can think of is: For every 2 points check every third point if draw lines makes 90 degree angle. If it does, then check if 4th point exists where it should be.

    • @bitzartdev
      @bitzartdev 5 років тому

      Wow that's elegant

    • @gabriel-lyra
      @gabriel-lyra 5 років тому +2

      If I am not mistaken he said something similar with option 1, but gave up on it, as looking for A and B points require N^2 complexity. Looking for a C point in an line requires N complexity. Looking for D requires a some very simple computation. Everything together and we still have N^3.
      Honestly I also can't think of anything better myself. Maybe you can do just like problem 1 and look for two pairs but with some kind of offset? I dunno.

    • @devups6391
      @devups6391 5 років тому

      Yeah I did thee same thing

  • @maherhayek9696
    @maherhayek9696 5 років тому +7

    For the second problem, use same code as first problem with the following changes:
    - remove if statement (get all pairs of points, not just vertical)
    - for each pair of points, instead of storing their y-coordinates, think of the pair as a line segment, find the 2 perpendicular lines that pass through each endpoint of this segment (let's call them P1 and P2).
    - Store the slope of P1 (sP1) and the intercept (iP1). Same for P2.
    So for each pair of points, instead of storing coordinates, store ((sP1, iP1), (sP2, iP2)).
    - And the rest of the code stays the same from problem 1. Everytime you find a new pair of points with same sP1,iP1,sP2,iP2, then increment the counter.
    - This is the same time complexity from Problem 1's code.

    • @EnrahimRPG
      @EnrahimRPG 5 років тому

      You get some serious numeric trouble with low or high slope pairs this way though..

  • @xakkep9000
    @xakkep9000 5 років тому +14

    TY, your "interview" helps me to get over the fear of interviews! You provide an excellent experience that I can try on myself. Now interview looks more like a predictable conversation, than a challenge full of surprises!

  • @patrickmarshall6594
    @patrickmarshall6594 2 місяці тому +1

    It is enjoyable to have watched this when this video first came out and been so perplexed that I was taken aback, to now where 5 years later a majority of what he is saying makes sense and the confidence to code it would be within grasp. 5 years ago I couldn't print to screen.

  • @lol_perry6835
    @lol_perry6835 5 років тому +44

    If I ever get asked this question in any interview, I would just start searching for other jobs.

  • @DarkestValar
    @DarkestValar 5 років тому +51

    36:22 You know shits about to get real when
    Clément sips some coffee.

  • @violetviolence3358
    @violetviolence3358 5 років тому +54

    Got a "work as a taxi driver" add before this video.
    That's the best burn I've received this year :D

  • @-hero-5882
    @-hero-5882 4 роки тому +13

    I have a dream, and I don't care how challenging the field is, this is my inspiration

  • @KurtVanBever
    @KurtVanBever 3 роки тому +32

    I think most people overlook the most important takeaway from this interview : communication and teamwork. As you couls see in the second part, Clement shifted from being an interviewer to becoming a collaborator.
    Bringing different ideas to the table, putting in a different perspective and poning real time critiques... All of these contribute heavily to a deeper and more complete understanding of the problem by everyone involved.
    Great video guys.

    • @Slimehardtshawty
      @Slimehardtshawty 2 роки тому

      So is it normal for interviewers to help the interviewee along?

  • @pweddy1
    @pweddy1 5 років тому +4

    Second problem:
    Facts: rectangles diagonals are equal length. Rectangle diagonals intersect in the center of each other.
    For all lines count the number of equal length lines that bisect each other in the center, plus or minus rounding tolerance. I think a divide by 2 should give the answer. I may have to look at it on pen and paper.

  • @jonathanconte1395
    @jonathanconte1395 5 років тому +34

    Come on Clement ! I was studying algorithms and I had to stop because I can't resist watching this video !

    • @clem
      @clem  5 років тому +25

      My mission is to distract algorithm studiers with mock coding interviews!

    • @abhinavraut3099
      @abhinavraut3099 5 років тому

      same here!

  • @ISmellMopWho
    @ISmellMopWho 5 років тому +274

    I’m planning on studying computer science and this shit scares me.

    • @RM-gm7lu
      @RM-gm7lu 5 років тому +58

      Don't be. The journey of a thousand miles starts with a step. Sometimes it's good to be confused but keep at it.

    • @jaytrivedi8422
      @jaytrivedi8422 5 років тому +88

      Forget about coding at first. just read the problem statement and think about how you would try to solve it in real life.
      Coding is just a language representation of how a computer will execute instructions your instructions.. remember you are in control..always...

    • @Cobrax101
      @Cobrax101 5 років тому +29

      I have 2 degrees, this shit still scares me. Interviews are like tests, they're scary and they suck.

    • @Ryuudo123
      @Ryuudo123 5 років тому +3

      under presthure

    • @trousersnake81
      @trousersnake81 5 років тому +9

      There are many ways to interview and many ways to solve this problem. Just break it down into small steps. After you take a couple courses, the problem itself won't be the issue anymore as you will see many ways to solve it. It will then become about doing it in the most efficient way, both in terms of the time it takes you to write, and how long it takes to run, as well as conducting yourself appropriately for an interview - as with any other job.

  • @Exeedo.
    @Exeedo. 5 років тому +8

    I think the second solution also works in O(N^2).
    A rectangle can be defined as two diagonals with the same length and same midpoint.

    • @AashayS
      @AashayS 3 роки тому

      True. They're are also perpendicular to each other.

    • @Exeedo.
      @Exeedo. 3 роки тому

      @@AashayS Actually no. Only if it was a square.

  • @fredschneider7475
    @fredschneider7475 4 роки тому +3

    For the second half, my idea was to create a map where the doubles are line segment length, slope and x-intercept resp. where the slope is zero or more but not infinite (a vertical line). The x-intercept is where the perpendicular (to the line segment) through the left point in the pair hits the x-axis.
    If the slope is zero, the x-intercept is just x. Otherwise, it's y+x/m where m is the slope.
    Then you could use the similar count trick from the first half. I think that would be unique.
    That would have O(N^2 * (log N)^3) complexity and O(N) space used. To get down to O(N^2), maybe you could use an unordered map (hash table) and have constant lookup time with a hash function, perhaps based on the sum of the three doubles. Maybe Errichto could weigh on that idea.

  • @HAWXLEADER
    @HAWXLEADER 3 роки тому +3

    22:51 You were close, pretty sure it's the dot product you're looking for.
    x1*x2+y1*y2=0 when perpendicular.
    Also the thing you are looking for in 30:50 is 4 points with lines that intersects in the middle and are of same length.
    (distance from corners and the fact that they intersect in the middle).

  • @kareha0
    @kareha0 5 років тому +9

    const counter = require('count-rectangles');
    echo counter(point-tuples);
    // done

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 5 років тому +38

    Nice to have winner of Codejam on channel.
    Liking before watching it Clement!
    🙂

    • @clem
      @clem  5 років тому +4

      The only way to do it 😛

    • @indiansoftwareengineer4899
      @indiansoftwareengineer4899 5 років тому +3

      @@clem
      BTW how are you managing your FTJ with UA-cam and managing Algoexpert too??
      You are very hardworking, I too try to work more n more but brain shuts down after FTJ.
      Can't leetcode after coming from office.
      What can you suggest, to stretch more hours in "breathing n coding"?
      Maybe this would be good topic for your next video.....

    • @clem
      @clem  5 років тому +2

      @@indiansoftwareengineer4899 This is a great video topic; already planning on doing it!

    • @indiansoftwareengineer4899
      @indiansoftwareengineer4899 5 років тому

      @@clem oh, nice to hear that.🙂

  • @joseortiz_io
    @joseortiz_io 5 років тому +10

    Yes! This guy is awesome! I first saw him in an interview with Joma. Im glad you got him in. What an awesome video 😁👌

  • @michaelphoscar7509
    @michaelphoscar7509 5 років тому +170

    I feel like I'm being spat at constantly

    • @spidermid3003
      @spidermid3003 5 років тому +6

      evil

    • @astwas
      @astwas 4 роки тому +1

      Or like that shame scene from Game of Thrones, which is far easier to handle than having to attempt this in an interview..

    • @AnkitJosh
      @AnkitJosh 4 роки тому

      Underrated

  • @jj691
    @jj691 5 років тому +10

    60 seconds into interview I pull my internet cable out

  • @sumeetbhujang2756
    @sumeetbhujang2756 5 років тому +17

    Still to figure out for me, was this interview motivating or demotivating ;)... He nailed it completely. Loved to see this entire interview.. very inspirational..

    • @clem
      @clem  5 років тому +5

      Hahaha, it should be a little bit of both 😛

  • @GiTsticker
    @GiTsticker 5 років тому +4

    For the second part:
    1) Find all vectors that share equal length with another.
    2) Find all equal length pairs that bisect each other exactly in the middle.
    Also works for the first part.

    • @viniwittke
      @viniwittke 5 років тому +1

      I'm not actually a programmer (so idk about performance using this method), but this seems like a smart and elegant solution to the problem. At least to my ignorant eyes. Can Clément take a look at this and dive deeper?

  • @lucianbicsi1636
    @lucianbicsi1636 5 років тому +27

    Fun fact: you can actually solve the problem pretty easily with idea #2.

    • @daliborduric992
      @daliborduric992 5 років тому +8

      just need to verify halves of the intersecting diagonals are equal length :)

    • @pHuzi0n
      @pHuzi0n 5 років тому +1

      @@daliborduric992 Yep, I thought of checking the length pretty immediately and it is O(N) and rather simple.

    • @julianelischer
      @julianelischer 5 років тому +3

      @@daliborduric992 yes generate all possible vector mid points and store them in a hash along with originating points (and length) (in lists), That would be ON^2 I think.. Then run through the hash, stopping only at those points with multiple entries. then you need to find clusters of lengths... For each cluster, the number of rectangles is I think (N-1).. Maybe you could move items from the first hash/list to a second one only when they get second entry.. so that last pass might be a very short list to traverse.

    • @daliborduric992
      @daliborduric992 5 років тому +1

      @@julianelischer edit: it's actually n(n-1)/2 + n which simplifies to n^2 although it will never reach n^2

    • @julianelischer
      @julianelischer 5 років тому +1

      Dalibor Duric that is way too much work for me. It has two loops inside each other so I go for n^2. 😊

  • @DogeCharger
    @DogeCharger 5 років тому +33

    things like these make me reconsider trying to major in computer science lmao

    • @VortechBand
      @VortechBand 5 років тому +4

      Don't worry. These guys are handling just one field in software development. A very difficult one at that, and only represents like 1 % of all software development. In about 99 % of software development jobs, you will *never* have to deal with algorithms, O-complexity, compiler optimization, etc. And most of that 99 % is far easier than what these guys are doing in the second part of the video. So don't be afraid :) Backend development is mostly about creating data queries (database, cloud services, or similar) and returning the data in a specific format, and frontend development is about displaying and styling that data. Very simple stuff, that pays well.

    • @cgme7076
      @cgme7076 5 років тому +2

      @@VortechBand :: Are you employed?
      I absolutely love programming, but I have been reconsidering my career choice for about 6 months now.
      I was in college and was doing nothing but acing all of my programming and math classes. However, I halted and took stock in what I was doing when I got burnt out from all of the classes I was taking at once; I wasn't sure if that field was for me.
      Sure, I could stay up all night and write a beautiful program. Yet I had the feeling, after pushing myself really hard, that it simply wasn't for me.

    • @MonsterhunterFTWWTF
      @MonsterhunterFTWWTF 4 роки тому

      Good, if you're scared of difficulty then don't major in something difficult.

  • @zedzempai123
    @zedzempai123 5 років тому +5

    Approaching this with vectors is absolutely an incredible idea!!! I was amazed when he used the condition of colinearity! Thank you for sharing this :D

  • @farhanhaider5652
    @farhanhaider5652 5 років тому +376

    I don’t know why I watched this video, I’m a beginner lol

    • @eh7931
      @eh7931 5 років тому +47

      I'm not even a beginner. I just stumbled upon this video. I don't...understand.

    • @aryanadiazcakasana4414
      @aryanadiazcakasana4414 5 років тому +14

      of course you should watch it.as begginer strong foundation on algorithmic & daya structure is a must

    • @danielmateofitness7618
      @danielmateofitness7618 5 років тому +29

      Instant anxiety

    • @Twe4ke
      @Twe4ke 5 років тому +14

      @@eh7931 I fucking hate math of literally any kind and for some ungodly reason, I sat here and watched the entirety of this video. What in the actual fuck is wrong with me?

    • @SG-ek2zw
      @SG-ek2zw 5 років тому +10

      @@Twe4ke you want to feel smart because you watched the whole thing.

  • @tekk7989
    @tekk7989 5 років тому +21

    Why didn't Google recruit Errichto after he won second place at the Google coding competition? Or maybe they did, and he wasn't interested?

    • @jaysondemarco
      @jaysondemarco 4 роки тому +2

      Tekk he was not interested he prefers competitive programming and gaming you can find this out by watching out his interview on jomatech channel. Interview with a competitive programmer

  • @SumitKumar-fn3gj
    @SumitKumar-fn3gj 5 років тому +10

    This Is Kind a Motivation For me. Today I messed up very badly in Codechef Lunchtime. But i will keep fighting again and again and learn new Things.

  • @ManishSharma_msinvent
    @ManishSharma_msinvent 3 роки тому +2

    @Errichto This is a legendary solution. It gets as beautiful as it can get.

  • @OMGclueless
    @OMGclueless 4 роки тому +1

    At 31:40 Clement asked Errichto to avoid solutions that use clever math properties. But you will never be able to solve this problem in O(N^2) time without using clever math properties. The reason is that the count of rectangles itself is more like O(N^3) and not O(N^2). If your solution amounts to iterating over rectangles, you're already too slow. You need something more clever.

  • @skylerockspecial
    @skylerockspecial 5 років тому +16

    Was he really struggling with “how do I know from diagonals wheter it’s a rect or a rombus”??
    Just check if diagonals have equal length...
    Iterate through points in your plane
    From every point
    Project pair to any other point
    Calculate middle coord
    Create perpendicular vector with same middel point
    Check if new vectors endpoints are both in hashmap

    • @skylerockspecial
      @skylerockspecial 5 років тому +1

      To take out doubles, instead of raycasting in all directions, only take pairs in one quadrant

    • @satyampandey2222
      @satyampandey2222 5 років тому +4

      This happens in programming, you sometimes forget the small things

    • @skylerockspecial
      @skylerockspecial 5 років тому +4

      satyam pandey this is, in a google interview you’ll first take time to define the problem, this guy went straight in and proved he knew an easy answer on the first problem quickly. But that’s not the way to go about it in an interview, it’s for more important to prove that you fully understand and are able to break down the problem itself.
      First this you’d do is start writing down what makes a rectangle a rectangle, what makes the coordinates work. After that you’d be able to explain to the interviewer what you will try to do in code and then code it out.
      The way he lost the interviewer in what he was doing was his own fault.
      I know this is a mock interview with a speed coder, but for anyone looking up “google onterview” these are some free tips 😉

    • @baseballboytetris
      @baseballboytetris 5 років тому

      Ah, I somehow forgot this too. Instead I considered calculating the two slopes of the sides and seeing if the negative inverses match.

    • @skylerockspecial
      @skylerockspecial 5 років тому +1

      baseballboy you can indeed do exactly the same as I dit but starting with two neighbour corners instead of opposites, thing is that the rectangle can go much further so there is no definite way to calculate this for a hash map and thus your big O would be considerably larger.
      Thank you for coming to my TED talk

  • @christopherchege7404
    @christopherchege7404 5 років тому +161

    I thought coding is all about "HELLO WORLD!"

    • @christopherchege7404
      @christopherchege7404 5 років тому +3

      @@tanishcqmehta9984 You might think I'm not ambitious but let me stick to moving company.

    • @nielsliljedahlchristensen4924
      @nielsliljedahlchristensen4924 5 років тому +10

      No you didn't, you just wanted some likes on a stupid comment

    • @TopAhmed1
      @TopAhmed1 5 років тому +1

      @@nielsliljedahlchristensen4924 Ok Boomer

    • @nielsliljedahlchristensen4924
      @nielsliljedahlchristensen4924 5 років тому +5

      @@TopAhmed1 Says boomer and has Messi as their picture. I'm guessing you're offended because you also never had an original thought yourself

    • @YourMom-rg5jk
      @YourMom-rg5jk 5 років тому +1

      @@TopAhmed1 ok millennial

  • @riyesh3284
    @riyesh3284 5 років тому +31

    why didn't he start off with 'Hello World !!'?

  • @fr4nk777
    @fr4nk777 4 роки тому +1

    I would personally go for an approach based on diagonals.
    The diagonals of a rectangle can be stored as their mid-point and their length. ( map )
    Then iterating through each pair of points for(A) for(C) and storing the pair if does not exist.
    Then each time we find another pair which has the same Center point and Length as one in the map, we found a rectangle.

  • @Coeurebene1
    @Coeurebene1 3 роки тому +3

    "I don't know if you know, but this is like a baby problem for me, are you sure you want me to go through the motions"? haha, love that guy

  • @avinashthakur80
    @avinashthakur80 5 років тому +25

    For a rectangle, the diagonals are equal and bisecting each other.

    • @IlariVallivaara
      @IlariVallivaara 5 років тому +3

      Yep, I had the same idea and drafted a solution based on that: scastie.scala-lang.org/clQ2xWZ2TzOT5F9pxsriuQ

    • @AntonioLicon
      @AntonioLicon 5 років тому

      @@IlariVallivaara Your code says a 4x4 grid has 44 rectangles. I'm only getting 42 (on paper and in code)

    • @IlariVallivaara
      @IlariVallivaara 5 років тому

      @@AntonioLicon : Ok, let met check what's wrong.

    • @IlariVallivaara
      @IlariVallivaara 5 років тому

      @@AntonioLicon : I did manual recomputation on paper, and found 44 rectangles. Here is an illustration of the 9+6+6+3+3+2+2+1+4+2+2+4 = 44 rectangles I found:
      pasteboard.co/IRWnxBc.jpg

    • @IlariVallivaara
      @IlariVallivaara 5 років тому

      @@AntonioLicon : What I think you are missing those two non-straight-or-45-degree rectangles.

  • @treqqqq2850
    @treqqqq2850 5 років тому +11

    I wish you do more of these youtube sessions with top coders or professional problem solvers :😂: and solve hard problems. It is quite educational and interactive and love to pay for that.

    • @BigTimeCharley1956
      @BigTimeCharley1956 5 років тому

      There are people who do screen dump videos of solving problems, I think searching for topcoder, adventofcode, codeforces, etc. on youtube would do it. I just watched on earlier today, it was hilarious because they had a few typo (that I noticed right away) and spent a lot of effort finding it; the *sigh* when they finally got it was precious.

  • @asandax6
    @asandax6 5 років тому +38

    When a lengend enters the room you know because they don't need to think about any problem "THEY ARE GOOD IN COMPUTATIONAL GEOMETRY'.

    • @iamnoob7593
      @iamnoob7593 3 роки тому

      gennady korotkevich - Let me introduce myself.

  • @reshulw884
    @reshulw884 4 роки тому +10

    Clement please do interviews with Competitive Programmers more.

  • @TheJFKWahoo
    @TheJFKWahoo 3 роки тому +1

    as a solution to the second part.
    I would:
    pick a point, make it my center of coordinates
    rotate all the points 45 degrees around the center
    run the algorithm that solved the first half.
    Rotating can be done by converting to polar coordinates and then adding a theta of pi/2 and then converting back to cartesian

  • @MrAntarpreets
    @MrAntarpreets 5 років тому +5

    I loved his first solution, for the first part I just wanted to create a set (O) for original points and then iterate over points to find a pair {x,y}, {x1,y1} with x > x1 and y > y1 such that O also contains the points {x, y1} and {y,x1}. Return the count of such pairs found as the answer.
    For the second, I am not familiar with vectors like he described but I realized that 1. the diagonals intersect at their midpoints and 2. the diagonals are equal in length. So iterate over all points and complete a map. At the end just iterate over the entries in the map and calculate answer += count_slopes*(count_slopes - 1) / 2 for each map entry.

    • @gyan1010
      @gyan1010 5 років тому

      I'm not sure why you store the slope, as I don't think you need it. My solution for that was to do the same thing at first, but only map the midpoint and length of each line segment, and just keep a count of each unique entry. Then since any 2 line segments with matching midpoint and length can form a rectangle, you need to take each different result and get the combination of n choose 2, where n is how many of that mapped pair were counted. So if 4 lines (8 coordinates) have matching midpoint and length, then you could actually make 6 rectangles because 4 choose 2 is 6. Anyway, you can add the combinations to get the result.

    • @MrAntarpreets
      @MrAntarpreets 5 років тому

      ​@@gyan1010 The slope is just to avoid duplicates (and save some memory from keeping 4 coordinates) since a line segment can be uniquely specified by mid point, length and slope. If you don't care for those, you'll at least have to restrict the selection of point pairs in a way to avoid same set twice.

    • @gowthamchandanraju
      @gowthamchandanraju 5 років тому +1

      Well, i shld say i didn't get shit out of it but for the first question if they made it lol bit complex we can actually get it in a easier ways.
      Let's say there r n points then no.of lines formed will be nC2 so considering only slopes of all the lines, if 2 slopes are equal then inserting 1 amongst 2 into an array and then multiplying those 1 to any other and if the product is -1 then the count is increased to +1
      That is all it is they made it much more complex to look.

    • @gyan1010
      @gyan1010 5 років тому +1

      @@MrAntarpreets you wont need to avoid duplicates as long as you only store and compare diagonals once. Two different matching diagonals means a different rectangle, and by taking the combination formula on the matching length/midpoint diagonals, and choose 2, you get (n! / ( (n-2)! * 2! )) = number of unique sets of 2. Do that for each match and add the results and should be no duplicates.

    • @MrAntarpreets
      @MrAntarpreets 5 років тому

      @@gyan1010 How do you store a diagonal uniquely ? I am using slope to do this.
      Also nC2 = n*(n-1)/2 , which is what I used.

  • @skyrius5568
    @skyrius5568 5 років тому +5

    I'm 99% sure in the second part when he said cross product, he meant to say dot product. The formula for a dot product is (x1*x2)+(y1*y2) and it equals 0 for right angles. A cross product would give you an angle perpendicular to the plane created by those two vectors (and we'd also be working in 3D instead of 2D at the point lol). But other than that, clever answer

  • @SanixDarker
    @SanixDarker 5 років тому +8

    The video just started, so i wanted to do the challenge too before seeing Errichto's solution, so this is the algorithm i propose in 3 steps :
    1- I calculate the distance between all the points between them in the plane (x, y).
    2- I recover points 3 by 3 in a loop that respect the Pythagorean theorem(finding the right angle).
    3- For each triot, I look for a fourth point that respects the same law and I get my rectangles avoiding duplicates.
    Let's see his solution now....

    • @SanixDarker
      @SanixDarker 5 років тому +1

      Really Nice approach using vectors !
      After thinking twice i could also use affixes (complex representation of each point X + iY ) of each point to get angles between them preventing me to loop throught 3 per 3 to get distances.

    • @ruslikaici
      @ruslikaici 5 років тому +1

      The distance idea is nice, especially because you could store the square of the distance and avoid rounding : map. I would use those distances to partition problem into smaller ones - (all 4 points from a rectangle should be have the same distance from them, so they should be all mentioned in the same list)

  • @computercentral5914
    @computercentral5914 4 роки тому +1

    For the squares one just do (n+n-1+n-2...)(m+m-1+m-2...), where n and m are the side lengths of the rectangle

  • @NiranjanKumarGHC
    @NiranjanKumarGHC 4 роки тому +2

    I'm just commenting my approach here, and am ready to criticism ok here we go:
    Given a grid of points we can easily count the number of any type of rectangles formed using combinations, for which we get a straightforward formula that can be solved in constant time given the number of vertices in the grid. Given that the grid at tight angles. Now to get a grid we can loop over every pair of points in N² time and find the slope between points and store pairs with the same slope in a map. Then we just count the number of pairs of points with same slope and run it through our formula and done.

  • @alexander14994ever
    @alexander14994ever 5 років тому +19

    Could you make a video on how to prepare for this and improving your skills. Also what resources do someone as good as him used to reach this level of skill! Great video!!

  • @hemanthreddyvennapusa18
    @hemanthreddyvennapusa18 3 роки тому +3

    Errichto’s “professional problem solver” remark is really underrated

  • @brillianceplayground
    @brillianceplayground 5 років тому +11

    This man has serious skills! Love the idea Clément, keep up the great work 👍

  • @Dimencia
    @Dimencia 4 роки тому +5

    I am sad that he was discouraged from the A-C option in the second half. That definitely seems like a super simple way to get this down to N^2. Even if we ignore the midpoint bit, you can take any two points, treat them as A and C. Imagine a line between them. Then imagine vectors out from each point, at +45 and -45 angles to the line between a-c. Where the lines from A intersect the lines with C, there should be points to a square. Check those in a hash set and bam, n^2

    • @thomas2446
      @thomas2446 2 роки тому

      this would only find squares not all rectangles like the question is asking.

    • @Dimencia
      @Dimencia 2 роки тому

      ​@@thomas2446 Not so; I wrote square incorrectly, but it's the points to a rectangle.
      No, the part I neglected is that it's not trivial to check if/where two arbitrary lines intersect - or at least, I don't know how off the top of my head
      The basic idea is that it takes only two points to define a rectangle, and so for any two given points you can determine what two other corners would be required to complete a rectangle

  • @Shri7hari
    @Shri7hari 3 роки тому

    We can find the slope and distance of all two points where X2> X1 and store them in a map. For each slope and for each distance check all the pairs in the loop where they make 90° with the bottom point