Microsoft Coding Interview Question - Valid Perfect Square - Leetcode 367

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ส.ค. 2024
  • FAANG Coding Interviews / Data Structures and Algorithms / Leetcode

ความคิดเห็น • 189

  • @GregHogg
    @GregHogg  3 หลายเดือนก่อน +41

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

    • @faisalbhagri
      @faisalbhagri 3 หลายเดือนก่อน

      🎩
      😁
      👕👍Great!
      👖

    • @VedantTinkhede-ei4zb
      @VedantTinkhede-ei4zb 3 หลายเดือนก่อน

      return (N & (N-1)) == 0

    • @red__________
      @red__________ 2 หลายเดือนก่อน

      That is not for perfect square.
      It is a check for if the number is power of 2 or not​@@VedantTinkhede-ei4zb

    • @snesmocha
      @snesmocha 2 หลายเดือนก่อน

      Can’t you literally just calculate the square root and then see if there’s trailing decimals? That would be O(1)

  • @davidgillies620
    @davidgillies620 3 หลายเดือนก่อน +281

    Yeah, that's not going to scale. In practice you test whether the number is a quadratic residue mod a few small integers. The first stage is to look at the number mod 256 i.e. examine the least significant byte. Then, for a 64-bit machine, test mod 5, 7, 9, 13, 17 and 97. This catches 99.62% of perfect squares very quickly. If the number passes the test you still need to verify it's a square which is expensive, but you only have to do it 0.38% of the time

    • @tommys234
      @tommys234 3 หลายเดือนก่อน +109

      every time this guy pops up on my feed i go to the comments and there's always an explanation for why his algorithm scales like shit

    • @jackhedaya571
      @jackhedaya571 3 หลายเดือนก่อน +27

      Serious question - where do you learn stuff like this? Had an algorithmic programming class in college but never had to solve problems like this on the job. At a bare minimum, this is fascinating.

    • @davidgillies620
      @davidgillies620 3 หลายเดือนก่อน +25

      @@jackhedaya571 Do you mean me or the channel owner? If you're asking me, then the answer is many, many years using computers to solve number theory problems (I'm not a mathematician, but a mathematically-oriented software engineer).

    • @jackhedaya571
      @jackhedaya571 3 หลายเดือนก่อน +15

      @@davidgillies620 Yup! Was asking you. Saw the question and immediately thought there needs to be a super fast way of doing it (I was guessing it would be some bit magic).
      That's really cool! Can I ask what domain of software you work in? My day-to-day is mostly APIs, DBs, deployments, etc. but a part of me yearns to work on problems that require more clever solutions.

    • @sophiophile
      @sophiophile 2 หลายเดือนก่อน

      Best way to scale this solution is to see that # of digits in the sqrt(n) is the ceiling of 1/2 the digits in n. Then you can set your binary search range to 10000....99999 (with the number of digits determined by the relation described above). This is true for 100% of cases, no exceptions.

  • @justinturbyfill7289
    @justinturbyfill7289 3 หลายเดือนก่อน +247

    square=sqrt(num)
    floor = square//1
    return square==floor
    I did this in python and beat 97.3%

    • @bharatsahu1599
      @bharatsahu1599 3 หลายเดือนก่อน +81

      That's the catch you can't use sqrt function..

    • @justinturbyfill7289
      @justinturbyfill7289 3 หลายเดือนก่อน +29

      @@bharatsahu1599 oh whoops, thx for tellin me

    • @hlubradio2318
      @hlubradio2318 3 หลายเดือนก่อน +2

      Huh I'll try this too

    • @hlubradio2318
      @hlubradio2318 3 หลายเดือนก่อน +2

      Sorry wrong answer dude better luck next time

    • @kaiketsu07
      @kaiketsu07 3 หลายเดือนก่อน +8

      sqrt is expensive

  • @LilSnotbag
    @LilSnotbag 3 หลายเดือนก่อน +46

    Perfect circles been real quiet since this one dropped

  • @sophiophile
    @sophiophile 2 หลายเดือนก่อน +7

    One optimization to the solution provided can be made by observing the fact that every number's square root has the property: # of digits in floor(sqrt(n)) = ceil (1/2 * # of digits in n).
    Therefore, instead of starting at 1/2 of n, you can safely have a range between 100.... to 999.... where the numbers have 1/2 as many digits as n (rounding up).
    For very small n, this won't help (but the process is really quick anyways). But for very large numbers, let's say 1000000000, you can start with a range of 10000-99999 instead of 1-500000000.

  • @GregHogg
    @GregHogg  3 หลายเดือนก่อน +2

    Btw, I offer personalized 1 on 1 tutoring services for data structures and algorithms / coding interview prep - please email greg.hogg1@outlook.com to book a meeting with me!

  • @shubhambhattacharjee1111
    @shubhambhattacharjee1111 3 หลายเดือนก่อน +36

    You can also start the range as 1 to N/2, the sqrt can't be bigger thaan half of the number for any number >= 4. And we can always do a sqrt(n) == floor(sqrt(n)), but I guess that could have a rounding error.

    • @brycejohansen7114
      @brycejohansen7114 3 หลายเดือนก่อน

      You would have to check if N is 1 or 2 first

    • @shubhambhattacharjee1111
      @shubhambhattacharjee1111 3 หลายเดือนก่อน

      @@brycejohansen7114 true you'll have to check for just 1, 2 isn't a perfect square. I just assumed people will understand that.

    • @brycejohansen7114
      @brycejohansen7114 3 หลายเดือนก่อน

      @@shubhambhattacharjee1111 Oops, my bad.

    • @mentanagavenkatasrinivas9245
      @mentanagavenkatasrinivas9245 3 หลายเดือนก่อน

      It's log(n), so doesn't matter much

    • @woutverjans2928
      @woutverjans2928 2 หลายเดือนก่อน

      Why only go for 2? If you use N/4 than it still works for every root over 4. If it's less just check with OP's algorithm.

  • @rira12621
    @rira12621 3 หลายเดือนก่อน +7

    Just do a square root and try to stick the result in a var type int and catch the error. If err != nil { fmt.Println("not perfect square ")}

  • @woutverjans2928
    @woutverjans2928 2 หลายเดือนก่อน +2

    Yes but you can make it scale better at the cost of a little bit of overhead.
    1. Squares of even numbers are always even and squares of odd numbers are always odd. Don't check even numbers when getting the square root of 121 but only check the odd ones.
    2. If you have some idea of what range most of the numbers you want to check are then don't begin at 1² (actually, never begin at 1, it's always 1). If needed make a list where the ends are the range of most of your numbers and fill the rest with other numbers that allow you to get closer in fewer steps. When your result is between 2 of the numbers in the list then you start checking more closely. Alternatively you can take steps of 10 and square those, of your number is more than 100 but less than 400 you know it's root is between 10 and 20.
    Edit: 3. As someone else pointed out, you only need to check up to N/2 as the square can't be higher than that. But again, if you know your numbers are going to be large than you could try N/4 as any number larger than 4 would fall in that range.

  • @GoodVolition
    @GoodVolition 3 หลายเดือนก่อน +7

    I think I have one of the fastest leetcode solutions for this one iirc. I cheated and used a taylor series expansion that's done using type coercion in C++ as an initial guess.

    • @joker345172
      @joker345172 หลายเดือนก่อน +1

      Stop lying. I checked the 100 best solutions for this problem and yours is nowhere. You didn't code anything, stop lying

    • @the_dented_NPC
      @the_dented_NPC หลายเดือนก่อน

      @@joker345172 LOL

  • @gustavo9758
    @gustavo9758 3 หลายเดือนก่อน +9

    Goddamnit why is it always binary search or recursion? xD

  • @R_Y_Z_E_N
    @R_Y_Z_E_N 2 วันที่ผ่านมา

    Better approach use range from 1 to n/2 (make r= n/2 in this code at start) and do binary search

  • @isarf69
    @isarf69 16 วันที่ผ่านมา

    If I close my eyes ..it feels like shroud teaching coding

  • @lifeofabel
    @lifeofabel 2 หลายเดือนก่อน +3

    Cant we solve it constant times by using bit representation of the number

  • @xzayler7311
    @xzayler7311 2 หลายเดือนก่อน

    Maybe it's dumb but you could lower the steps required in half if you check if the number is even or uneven. If it's even you know it can only be a square of even numbers. Similarly with an odd number.
    So just check between 1 to another odd large number or from 2 to an even number. And set the boundaries as L+2 or R-2

  • @espressowizard
    @espressowizard 3 หลายเดือนก่อน +1

    Thanks Greg, I learned new stuffs from you everyday.

  • @jbparkes190
    @jbparkes190 2 หลายเดือนก่อน

    It'd probably be much faster to find the number of digits in the representation of the value, and just drop half of them and start you search there. So if N = 405332, don't start with 202666, but with 405. A binary search takes 3 steps to remove a digit from a decimal representation, which could be a lot of steps for a very large number.

  • @user-jz7vf5iq7h
    @user-jz7vf5iq7h 13 วันที่ผ่านมา

    you can go from 1 to N/2 and check only those that have the same parity than N.
    that speeds it quite a bit for large numbers.
    there are other additional methods to speed it even more but are a bit more complex and it's late for me. good night.

  • @RezoJaco
    @RezoJaco 2 หลายเดือนก่อน

    You, dear sir, are a perfect square ⏹️

  • @TeofilBejan-lg2rt
    @TeofilBejan-lg2rt 16 ชั่วโมงที่ผ่านมา

    In c++ its too easy, just create an int n, read n from console, calculate sqrt(n) - dont forget to include cmath, create a double variable m = sqrt(n) and of static cast m - n == 0 return true else return false

  • @sophiophile
    @sophiophile 2 หลายเดือนก่อน

    I am thinking that you can scale all recorded speeds for segments down to a range (0,1] where 1 * speed limit gives you the absolute speed. Also, featurize the historical timestamps into day of week/month, year, season, holiday, etc for those 2 min segments for each day. From this, there are plenty of regression models that are fast enough to be able to put out a (0,1] speed given the features for the actual predicted period/road segment. You can train these on splits with multiple 2 month holdouts to do as many sessions of cross-validation for hyperparameter tuning as you like. Your loss function can be MAE/RMSE/etc for the 2 months of predictions.
    Each day, you batch create your predictions for the next day, and they are stored in a fast cache to be used with the existing shortest path function to calculate the ETA.

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Uhm, what

    • @sophiophile
      @sophiophile 2 หลายเดือนก่อน

      @@GregHogg Woops. This comment was supposed to go on a MLSD video. Was wondering why it didn't show up.
      I'll post an actually relevant comment in a new thread haha.

  • @postblitz
    @postblitz 20 วันที่ผ่านมา

    If you take a look at the perfect squares you'll notice their progression is as follows:
    4-1=3
    9-4=5
    16-9=7
    25-16=9
    ...
    Those are the odd numbers.
    So if you want to check if any number is a perfect square you can check if it's part of this progression or not.

  • @skejeton
    @skejeton 3 หลายเดือนก่อน +8

    just use square root

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +12

      Yes haha unfortunately the question doesn't allow this

    • @skejeton
      @skejeton 3 หลายเดือนก่อน +2

      @@GregHogg damn

    • @LilMissMurder3409
      @LilMissMurder3409 2 หลายเดือนก่อน

      @@GregHogg It doesn't guarantee perfectly square roots anyway due to finite FP precision.

  • @HR-pz7ts
    @HR-pz7ts 3 หลายเดือนก่อน +9

    Best algorithm is probably Newton ralphson method.

    • @joergsonnenberger6836
      @joergsonnenberger6836 3 หลายเดือนก่อน +4

      ...which is ironically exactly what square root is doing.

    • @StupidusMaximusTheFirst
      @StupidusMaximusTheFirst หลายเดือนก่อน

      @@joergsonnenberger6836 yeah but if sqrt() is not allowed for some reason, then this?

  • @charlesreed8420
    @charlesreed8420 3 หลายเดือนก่อน

    cur = 2
    while True:
    cur = cur * cur
    if cur > target:
    break
    then linearly count down

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Isn't this powers of 2 only?

  • @angelosefstratiou5352
    @angelosefstratiou5352 2 หลายเดือนก่อน

    Newton's method for solving the equation x^2 - y = 0. Could work as well and i believe it converges faster.

  • @user-cg1ul8bh8w
    @user-cg1ul8bh8w หลายเดือนก่อน

    I always used like x**0.5==int(x**0.5)....
    Why such complex thing? Is it more optimal then calculating sqareroot?

  • @CharIie83
    @CharIie83 2 หลายเดือนก่อน

    does it have to be that fast if the list is that small? at some point you can do the calculations instead or? just if x^2 less or more

  • @user-kj6lj7dp9j
    @user-kj6lj7dp9j 2 หลายเดือนก่อน

    I usually use
    if( (double)sqrt(n)==(int)sqrt(n) ) cout

  • @hlubradio2318
    @hlubradio2318 3 หลายเดือนก่อน

    Omg very nice. I'll try this next

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Awesome thanks so much!

  • @neverfail9432
    @neverfail9432 หลายเดือนก่อน

    Am I underestimating the problem or...
    return round(N**(1/2))**2==N

  • @BarcaOwl
    @BarcaOwl 2 หลายเดือนก่อน

    Is it more efficient to loop through the bits and see if there is only 1 set bit in an even position?

    • @Grimlock1979
      @Grimlock1979 2 หลายเดือนก่อน

      That does not identify square numbers.

  • @MuSic-ok7dh
    @MuSic-ok7dh 2 หลายเดือนก่อน

    No quake fast square root, or that was a different problem?

  • @Marvel9-j4d
    @Marvel9-j4d 3 หลายเดือนก่อน

    I love your videos 📸🤩🤩❤❤

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Thank you so much 😊😊😊

  • @huydezus5633
    @huydezus5633 หลายเดือนก่อน

    "sqrt(n) * sqrt(n) == n" left the chat

  • @hesido
    @hesido 2 หลายเดือนก่อน

    "No, 1^2 is 2." T. Howard.

  • @christianrazvan
    @christianrazvan หลายเดือนก่อน

    And that is why u should use a math library

  • @dimitrisgkofas7787
    @dimitrisgkofas7787 3 หลายเดือนก่อน

    You can do sqrt of this number and if it is not integer you know that it is not a perfect square.

  • @Rexxar677
    @Rexxar677 3 หลายเดือนก่อน +1

    If the sqrt is not allowed simply use the representation of a sqrt which will always be ^(1/2), by doing so and validating if the number is a int, it’ll beat that since its complexity is O(1)

    • @njgskgkensidukukibnalt7372
      @njgskgkensidukukibnalt7372 3 หลายเดือนก่อน

      Sqrt operation is not O(1). If i ask you what the sqrt of an arbitrary number is you would need to do an increasing amount of work proportional to the magnitude of the number I chose.

    • @user-wt1ul7ki6p
      @user-wt1ul7ki6p 3 หลายเดือนก่อน

      @@njgskgkensidukukibnalt7372 But we assume the size of integer if fixed, e.g. 32bit, 64bit. So the magnitude of a number is constant. Without the assumption, even the method shown in the video will need to handle large numbers, or integers with arbitrary number of bits.

    • @njgskgkensidukukibnalt7372
      @njgskgkensidukukibnalt7372 3 หลายเดือนก่อน

      @@user-wt1ul7ki6p ? i dont understand.
      The code in the video (and for an integer sqrt program) does a binary search between 0 and n. This will take O(logn). The number of bits needed to represent the number is independent of the number of steps the binary search part will take. What matters is the magnitude of n which is not constant here.

  • @Ganjaaa
    @Ganjaaa หลายเดือนก่อน

    My head : Put it in the square hole

  • @user-gi9ve6uj6g
    @user-gi9ve6uj6g 2 วันที่ผ่านมา

    shortest an is = n&(n-1)

  • @jacobanslum7191
    @jacobanslum7191 หลายเดือนก่อน

    return int(n**0.5)==n**0.5

  • @ElliexUwU
    @ElliexUwU 2 หลายเดือนก่อน

    what happened to using something like
    if MOD n DIV n = 0

  • @chrism3790
    @chrism3790 2 หลายเดือนก่อน

    I highly suspect some bitwise trickery would solve this in 2 or 3 operations.

  • @X-Dev_lols
    @X-Dev_lols 2 หลายเดือนก่อน

    And explain why not just get square root and check if it's a int?

  • @brandonbailey8323
    @brandonbailey8323 2 หลายเดือนก่อน

    why not n / 2 / 2?

  • @victorkochkarev2576
    @victorkochkarev2576 2 หลายเดือนก่อน

    Why not to use a square root function?

    • @LilMissMurder3409
      @LilMissMurder3409 2 หลายเดือนก่อน

      It boils down to there being no pure integer square root CPU instructions under the hood - the simplest is the x87 fsqrt function which yields a floating point result with only around ~23 bits of precision. The bottom line here is that you cannot rely on floating point values because it require equality testing which is non-deterministic due to the lack of precision. Even if you use SSE (32bits of precision) or SS2 (64 bits), this fundamental constraint applies - you cannot be guaranteed that you have enough precision to guarantee a "perfect" square root.
      You could of course implement an integer sqrt function (for example Newton's or Heron's method) to appeal to the "understandability" of what you're trying to achieve, but since it is iterative it has a non-linear cost, which happens to be greater than this recursive approach.

  • @ryanzullo7169
    @ryanzullo7169 3 หลายเดือนก่อน +1

    cant u just int(sqrt(val)) and if it gives an error (number can't be converted to int, meaning is a decimal number) it isnt a perfect square

    • @bobmike2373
      @bobmike2373 3 หลายเดือนก่อน

      In theory it wouldn't work for all N. there could be some N that is close enough to a perfect square that the decimals get truncated after enough places after the sqrt. and int() will truncate any float its given so you'd have to do something else like modulo division to check for decimals.

    • @ryanzullo7169
      @ryanzullo7169 3 หลายเดือนก่อน

      @@bobmike2373 then u could, instead of int(), just modulo divise the sqrt to itself floored. To do so, u would store the result of the sqrt in a variable so that u don't have to calculate it 2 times. If the result is 0, it is a perfect Square, else not

    • @bobmike2373
      @bobmike2373 3 หลายเดือนก่อน

      @@ryanzullo7169floor() is going to cause losses. you can just do:
      return (sqrt(N) % 1) == 0.
      This would work for the most of the reasonable amounts of values for N. sqrt() in code isn't actually a true sqrt() and is just an approximation using fancy math. Plus if N is very large, the amount of bits that sqrt() has access to will be limited. This bit limitation will cause errors for some N that are large enough and are close enough that the precision of a float number wont be able to tell the difference and will mislabel some numbers as squares.

    • @ryanzullo7169
      @ryanzullo7169 3 หลายเดือนก่อน

      @@bobmike2373 wont %1 always be == 0? A number, even with decimal values, divided by one is always equals to itself and has rest = 0

    • @bobmike2373
      @bobmike2373 3 หลายเดือนก่อน

      @@ryanzullo7169 % is remainder division. x%1 will return the remainder of diving x by 1. so while 5 % 2 = 1, 5 % 3 = 2, 5 % 1 = 0, 5.1 % 1 = 0.1

  • @InfinityCS77
    @InfinityCS77 3 หลายเดือนก่อน +8

    A = N ** ½
    if A % 1 == 0:
    print(N, 'is a perfect square')
    else:
    print(N, 'is not a perfect square')

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +2

      Yes haha

    • @DivyanshJindal
      @DivyanshJindal 2 หลายเดือนก่อน +1

      Not really. Those approximation errors will fuck this up

  • @maitri1656
    @maitri1656 3 หลายเดือนก่อน

    Is there any video of vertical traversal BST from greg?

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +1

      No actually I need to cover this question. Adding to my list, thank you

  • @Rokkc
    @Rokkc 3 หลายเดือนก่อน +4

    just do this:
    return math.sqrt(num)%1 == 0:

    • @jacpa2011
      @jacpa2011 3 หลายเดือนก่อน

      you may not use sqrt in this problem

  • @jahfarmuhammed3387
    @jahfarmuhammed3387 หลายเดือนก่อน

    Log(2)base N ??

  • @abhineetbhattacharjee1829
    @abhineetbhattacharjee1829 27 วันที่ผ่านมา

    How About:
    n= 17
    print(int(n**(1/2)) == n**(1/2))
    ?

  • @somone799
    @somone799 3 หลายเดือนก่อน

    Why not just square root all inputs and check to see if that number has a floating point or a decimal or not?

    • @njgskgkensidukukibnalt7372
      @njgskgkensidukukibnalt7372 3 หลายเดือนก่อน +2

      How do you think square root function is programmed? For integers it would be programmed in a very similar way (same time complexity), and for floating point numbers you would have to use something like newton’s method which would be even worse

    • @Broadsmile1987
      @Broadsmile1987 3 หลายเดือนก่อน

      @@njgskgkensidukukibnalt7372 First of all, if you're using Python, the sqrt can be programmed inside the implementation of the interpreter, likely in C. In such tasks C is likely at least one order of magnitude faster (I'd guess 2 orders of magnitude because of extensive use of loops). Now consider the possibility (spoiler: actually the case) that the CPU can implement the function in the hardware. This is why maybe youtube shorts isn't the best place to learn programming.

  • @bekiraltindal9053
    @bekiraltindal9053 3 หลายเดือนก่อน

    I like this

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Thank you!

  • @hehexd9781
    @hehexd9781 3 หลายเดือนก่อน

    Cant you just floor it the square root of it and see if thats equal to the square root?

    • @njgskgkensidukukibnalt7372
      @njgskgkensidukukibnalt7372 3 หลายเดือนก่อน

      You can do that, but
      1. It defeats the purpose of the question
      2. It is slower

  • @1flovera
    @1flovera 2 หลายเดือนก่อน

    Why can't you just the root square??

  • @ZackPyle
    @ZackPyle หลายเดือนก่อน

    Square root has left the chat

    • @GregHogg
      @GregHogg  หลายเดือนก่อน

      😂

  • @paulburger9904
    @paulburger9904 3 หลายเดือนก่อน

    Check if square root is an integer?

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +2

      Yes you can the problem just doesn't allow this

    • @carultch
      @carultch 3 หลายเดือนก่อน +1

      The idea is to do it purely in integer math, since integers are more efficient in a computer's memory.

  • @user-si3uf4ke6e
    @user-si3uf4ke6e 2 หลายเดือนก่อน

    Num ** (1/2) ?

  • @lolloBriggi
    @lolloBriggi 2 หลายเดือนก่อน

    Square root?

  • @Mohammedijas619
    @Mohammedijas619 3 หลายเดือนก่อน

    Int sq_root = Sqrt(N)
    Int sq_root_1 = Sqrt(N-1)
    If(sq_root > sq_root_1)
    Return true
    else
    Return false

  • @patrickfarrell2517
    @patrickfarrell2517 3 หลายเดือนก่อน

    what about using newton's method?

    • @carultch
      @carultch 3 หลายเดือนก่อน

      I think the idea is to do this purely in integer math. Integers are more efficient in memory, and less susceptible to rounding errors.

  • @imranexd279
    @imranexd279 หลายเดือนก่อน

    ✓17 = int(✓17) does it not work?

    • @imranexd279
      @imranexd279 หลายเดือนก่อน

      If true then it's a perfect square

  • @hmkl6813
    @hmkl6813 3 หลายเดือนก่อน

    Why cant you check if floor(sqrt(N))= sqrt(N)?

    • @kotooriiii
      @kotooriiii 3 หลายเดือนก่อน +2

      taking the sqrt of a number is expensive. that’s why games do approximations instead of calculating the sqrt of numbers. or in cases where you don’t need to explicitly sqrt, you can instead multiply x*x as shown here which is a faster and very easy for computers to do.

    • @philstanton8912
      @philstanton8912 3 หลายเดือนก่อน

      Thats technically O(n^1/2) which is slower than O(log(n)). So its just not the fastest

  • @DOODLEJR
    @DOODLEJR 2 หลายเดือนก่อน

    Cant you just find the square root and make sure it has no decimals.

  • @kyledouglass7683
    @kyledouglass7683 3 หลายเดือนก่อน

    uh...
    public boolean isPerfectSquare(int num) {
    if (Math.sqrt(num) % 1 == 0) return true;
    return false;
    }
    7ms runtime.

    • @kyledouglass7683
      @kyledouglass7683 3 หลายเดือนก่อน

      just realized Math class probably isn't allowed so here:
      public boolean isPerfectSquare(int num) {
      int sum = num-1, x = 0;
      while (sum < num){
      sum = x*x;
      if (sum == num) return true;
      x++;
      }
      return false;
      }

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Yes this would work the problem just doesn't allow this idea unfortunately

    • @eizeed7973
      @eizeed7973 3 หลายเดือนก่อน

      ​@@kyledouglass7683 so basicly its linear approach. In video he talked about it and did binary cuz linear is O(n) and binaey is O(log n)

  • @zbvirus2420
    @zbvirus2420 2 หลายเดือนก่อน

    Someone count voice cracks quick

  • @Rozenkrantzz
    @Rozenkrantzz 3 หลายเดือนก่อน

    def is_perfect_square(N):
    t = 1
    while N>0:
    N-=t
    t+=2
    return N==0

  • @vedantbhenia9085
    @vedantbhenia9085 3 หลายเดือนก่อน

    I just solved this question 😂

  • @Dongdot123
    @Dongdot123 หลายเดือนก่อน

    I still don't get what's a perfect square 😂

  • @TheCyanKiller
    @TheCyanKiller 3 หลายเดือนก่อน

    Just square root it lmao or if thats not possible on python take 16 to the power of 0.5 (same thing as square rooting)

    • @njgskgkensidukukibnalt7372
      @njgskgkensidukukibnalt7372 3 หลายเดือนก่อน

      That is slower, although in practice it may be better to do that for readability

  • @MisterSnail1234
    @MisterSnail1234 3 หลายเดือนก่อน

    Just take the sqrt btw

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Absolutely in the real world. But this problem statement happens to not allow this solution

  • @hodayfa000h
    @hodayfa000h 3 หลายเดือนก่อน

    I don't like when i am early...

  • @andrewtitus6839
    @andrewtitus6839 3 หลายเดือนก่อน

    How is this a Microsoft coding question?!

  • @MapleclawGD
    @MapleclawGD หลายเดือนก่อน

    if (sqrt(n)%1 == 0) {
    return “yes”;
    } else {
    return “no”;
    }

  • @websnarf
    @websnarf 2 หลายเดือนก่อน

    Thafuq? unsigned long t = (unsigned long) floor(0.5 + sqrt(x)); return x == t*t;

  • @_joey_4744
    @_joey_4744 2 หลายเดือนก่อน

    Dude… just do
    return sqrt(x) % 1 == 0;

  • @killatapez
    @killatapez 2 หลายเดือนก่อน

    you look like jeb

  • @Broadsmile1987
    @Broadsmile1987 3 หลายเดือนก่อน

    I "like" how the author doesn't address the elephant in the room (sqrt) to bait comments to game the youtube algorithm...

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Not baiting. The problem statement doesn't allow this solution. So I didn't bother mentioning it

    • @Broadsmile1987
      @Broadsmile1987 3 หลายเดือนก่อน

      @@GregHogg But the viewer doesn't know that if you don't mention it :D

  • @drcl7429
    @drcl7429 3 หลายเดือนก่อน +2

    sum of odd numbers anyone?

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      What?

    • @drcl7429
      @drcl7429 3 หลายเดือนก่อน +1

      @@GregHogg perfect squares are made up of consecutive odd numbers. subtract until you hit 0 or less, if exactly zero then number is a square.
      There are likely optimisations and mathematical shortcuts

    • @eizeed7973
      @eizeed7973 3 หลายเดือนก่อน

      ​@@drcl7429how u gonna know the number to check if u got a square and not just even number? If u gonna check it your code is already slower than binary search

    • @prixya
      @prixya 3 หลายเดือนก่อน

      Woww, is this true for all perfect squares? ​@@drcl7429

    • @Ayush-_-007
      @Ayush-_-007 3 หลายเดือนก่อน

      ​@@drcl7429 wow that beautiful

  • @AllanSavolainen
    @AllanSavolainen 3 หลายเดือนก่อน +2

    Much easier to just check that the number in binary has only one bit set

    • @reeeeeplease1178
      @reeeeeplease1178 3 หลายเดือนก่อน +3

      That only works for powers of 2

  • @PengPengTalk
    @PengPengTalk 2 หลายเดือนก่อน

    二分法

  • @chris52000
    @chris52000 3 หลายเดือนก่อน

    pow(sqrt(N),2) == N

  • @ClassyJacket
    @ClassyJacket 3 หลายเดือนก่อน

    this is an interview question but they cant make a good OS to save their lives

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Lol windows has its issues and always will but I still like it

    • @ClassyJacket
      @ClassyJacket 2 หลายเดือนก่อน

      @GregHogg they really struggle with the "just make xp but better" concept

  • @AJofSteele
    @AJofSteele 2 หลายเดือนก่อน

    return n >= 0 and math.isqrt(n) ** 2 == n

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Is that actually a function?

    • @AJofSteele
      @AJofSteele 2 หลายเดือนก่อน

      @@GregHogg I just thought I would contribute my solution to this comment section even though it shares the same time and space complexity with yours, being O(log n) and O(1) respectively.

  • @quokka_yt
    @quokka_yt 2 หลายเดือนก่อน

    Here's how i did it:
    def isPerfSq(a):
    return a**0.5 - math.floor(a**0.5) == 0
    For any number a
    EDIT: I just realized it's probably better to check if a**0.5 % 1 == 0 but whatever it still works

    • @GregHogg
      @GregHogg  2 หลายเดือนก่อน

      Excellent

  • @ShauryaTyagi06
    @ShauryaTyagi06 3 หลายเดือนก่อน +2

    def checksqure(n):
    if str(n**0.5).endswith('.0'):
    return True
    else:
    return False

    • @carultch
      @carultch 3 หลายเดือนก่อน

      The problem is that square roots and fractional exponents in general, aren't a very efficient algorithm. They are subject to floating point errors. You might have a near-miss case, where a square root APPEARS to be a perfect integer, but is off by 10^(-16), and it is a blind spot to your code if you use floating point math.
      The idea is to do this purely with integer operations.