Meta Coding Interview Question - Top K Frequent Elements - Leetcode 347

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

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

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

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

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

      Great. No longer/slower version of this video? :)

  • @vchandrashekarreddy6338
    @vchandrashekarreddy6338 4 หลายเดือนก่อน +11

    I think Using bucket sort we can solve this problem in O(N) time complexity. Where we need to take the frequency of occurrences as index and append the corresponding numbers to that index.

  • @akashsonar6332
    @akashsonar6332 5 หลายเดือนก่อน +29

    Min Heap i.e (Priority queue) also works on sorting, so using Min Heap the time complexity is still gonna be O(nlogn).

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

      He keeps heap of size 2 only. So it's O(n)

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

      o(nlogn + nlogk)

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

      It’s O(n log k) the heap will have a max of k elements (2 in the video)

  • @Neura1net
    @Neura1net 5 หลายเดือนก่อน +42

    Got it. Heaps are the solution to everything

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

      😂

    • @Krokodil986
      @Krokodil986 5 หลายเดือนก่อน +5

      Either that or a hashmap, if not that then both 😂

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

      🤣🤣

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

      They are pretty awesome 😎

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

    Using bucket sort is also a solution. Use bucket sort but take instances of numbers as the key and occurences as the value.

  • @jonnylu_
    @jonnylu_ 5 หลายเดือนก่อน +9

    Oh my god the way I got this question as part of my meta interview last year and I used min heap, yet the interviewer seemed confused on why I used it. Didn’t make it to next round :(

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

      he must be used to either the brute for e solution or the hashmap bubble sort method

    • @Leonhart_93
      @Leonhart_93 5 หลายเดือนก่อน +5

      So you didn't know to explain that it was because lower time complexity?
      You have to understand that some problems have lots of solutions. If you don't show that your solution is superior, then it might just look like you did something overly complex for no reason.

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

      Rough.

  • @TF2Shows
    @TF2Shows 5 หลายเดือนก่อน +27

    Your calculation is wrong.
    Min/Max heap insertion: average time complexity: O(log n), do it for worst case (all unique numbers): O(n log n). Which might be a little faster than sorting. In addition if k=n (worst case) then it takes O(k log n) to pop elements.
    You got: O(n (log n + log k)) instead of O(nlogn). What am I missing?

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

      Wait so here is how I think this works. Kth top elements can ever only be as large as n as you cannot have more top elements than exist in the list. Since k is at most the size n we can say in the worst case the algorithm is O(2logn) or O(logn)!

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

      You completely misunderstood the solution. I don't know where to begin.

    • @Andy-nt8tq
      @Andy-nt8tq 5 หลายเดือนก่อน +2

      I agree with @TF2Shows. You can build a heap from an array in O(n), but removing the top k elements would still be O(k*log(n))

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

      ​@@Andy-nt8tqif you build heap of size k then it will be klogk.

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

      @@Andy-nt8tq if the k = n then you remove zero elements from the heap

  • @Anthony-oz1jc
    @Anthony-oz1jc 5 หลายเดือนก่อน +5

    also, you can solve this in O(n) time with a bucket sort, it just costs extra memory, tradeoffs

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

      Yeah but you do realise big O is for the worst case. A lot of sources get this wrong but technically there are other ways to talk about best and average cases. So in terms of big O, the big O time complexity of bucket sort is still n squared. The average case is n

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

      Yeah, and you could also use quickselect.

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

      ​@@Krokodil986it's actually O(n + c), not O(n^2)

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

      @@arsenypogosov7206 that's only if you are sure each bucket represents a unique value, which it won't be here
      Otherwise it's O(n^2)

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

      @@Krokodil986 well, we are sure about that. We have integer values from 1 to c, and each bucket represent it's own integer, so there are c buckets in total each with it's unique element. O(n + c).
      In this problem values are the number of times an element appears in the array. So they are not greater then n. c < n. O(n + c) = O(n + n) = O(n).
      Hope I'm making sense.

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

    Since problem allows returning top k frequent elements in any order, better solution would be to use quickselect after hashmap step, then total complexity would stay linear on average
    Alternatively, you can build a second hashmap from frequency to list of numbers with given frequency and probe it with frequencies in range n..1, it will also give you O(n) solution for the second part.

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

    In C++ Map is already sorted, you can change the compare function to be greater than and just do the 2nd portion

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

      map is sorted by the keys not the values.

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

    1 idea sort array, nlogn
    2 idea hashmap + top k storing something like sorted array of k elements, its n*k
    3 idea is hashmap + heap, heap can store top k for log k so its n log k

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

      4 idea is hashmap which is length of the array which where the index acts as the freq count and you store each value with that freq in it. that means you can scan once to generate tye counts and then move through the map from the end to fink k most frequent

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

      @@mark1A100even better, don’t use a hashmap and use an array of lists to avoid collisions

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

    Hash map -> stack with max k elements with min freq element at top

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

    Why not just iterate the hashmap and store the top two frequencies as you traverse? O(n) for building the hashmap + O(n) for traversing the hashmap

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

      K can be anything not just 2, so traversing through may keys won't give you the top k elements

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

    Isn't it possible to solve it using simple traversing with reverse every number and check using the count function if it is equal to k if the number is equals to k we store the value and then we traverse and if the number is smaller than the number we have found equal to our cat number we do not do any traversing or any count function on that but if the number is greater than or number that has the cat time repetition then we check the count frequency of the number

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

    ❤ unbelievable 😮

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

    This solution purely depends that your frequencies are sorted in ascending order because if your first key had frequency of 2 and last had frequency of 2 then your solution will fail as heap will return wrong numbers

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

    But aren't the heap operations O(log n)?
    Isn't the complexity of the second solution also be O(n log(n))?

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

      The "n" refers to the size of the heap. So since the heap is never bigger than "k", we get O(n logk). Be careful with what you refer to as "n".

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

      k can be equal to n if each element appear only once, so the worst case is still the same

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

      @@mohamedfarid7742 Yes, but k can vary. So it makes sense to include it in the time complexity. You can't just ignore it and pretend its O(n logn). That does not make sense.
      That is like having 2 different arrays as input and saying time complexity is O(n^2) since their sizes are equal in the worst case, instead of saying O(nm).
      So yes, in the worst case for the problem, it's still O(n logn), but for the worst case for any particular k, its O(n logk), which will be much faster if k

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

      @@mohamedfarid7742 no? Isn't K independent of n here? K represents the "top x" numbers. So if we're finding the top 2 most frequent like here, k=2. If we were finding the top 3 most frequent. k=3. Pretty sure it's independent of n here unless I'm missing something

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

    Why wouldnt you pull the elements off the hm where the frequency is k

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

    Optimal solution would be to use bucket sort

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

      Correct!

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

    I think hash map is overkill. Like why would you need heap of you already have that map. You can go through it and extract top k elements anyway

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

      You could indeed sort the dictionary although that's n log n

  • @Anthony-oz1jc
    @Anthony-oz1jc 5 หลายเดือนก่อน

    You should try k most frequent words
    leetcode 692, a similar idea with an added condition

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

      Thanks, I will actually try that and might end up making a short about it! I love question suggestions. Cheers!

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

    bucket sort will be O(N)

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

      Indeed it will be!

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

    Now that one I get it right away

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

    Hm what if there are 3 frequent elements ?

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

      In the description says the answer is unique. So while k is 2, the array can on only be like [ 1, 2, 4, 5, 4, 2] and not be like [ 1, 2, 4, 5, 1, 4, 2]

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

    Isn’t this a max heap?

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

    Counter(nums).most_common(k)

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

      You know your python!

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

    It's 50/50 if the video is actually correct or not. You need to make sure you are correct in those vids

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

      Easy for you to say when you’re wrong 100% of the time.

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

      @@arsheyajain7055 Easy to say when you get your knowledge from resources with wrong calculations or even wrong solutions

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

      @@tarsala1995 get a life.

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

      I think I've been wrong precisely once. So like 119/120 at this point or so. I apologize if I'm ever wrong lol not perfect and don't pretend to be

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

      ​@@GregHogg Your vids are sometimes showing up on my feed so maybe I got those questionable ones. This one with a bit off calculations, just like TF2Shows mentioned. And the other where you presented linear solution with arrays that does not work in certain cases - unfortunately dont remember the specifics. I also don;'t assume you are perfect but when it comes from LeetCode it should fairly well tested.

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

    Bro we get it you love hashmaps

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

      Oh yeah I do

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

    This guy will be the reason you don’t get that interview at meta 😂

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

      Ouch

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

    I feel so weird for liking these too much.

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

      Hehe

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

    Is coding relevant in this AI era?

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

    connor

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

    Where's the code

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

    yikes. just do a O(n) scan of the hash map.

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

      That's not O(n).

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

      @@chillfill4866You're right. it's O(k) better than O(k log_k) with the heap.

    • @Ford-ju9yr
      @Ford-ju9yr 5 หลายเดือนก่อน

      I came to the same conclusion. Finding max element in hashmap will be O(n). Same for top two elements

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

      That is O(nk). Doing a O(n) scan k time...?

    • @Ford-ju9yr
      @Ford-ju9yr 5 หลายเดือนก่อน

      ​@mujtabarehman5255 I thought k is always 2 in this task. Now i realize it's a parameter too, so linear scan is no good for big k

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

    I swear it's always hashmaps/heaps

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

      Pretty common, yeah! Haha

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

    Ooh copilot

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

    why u sounds like squidward ?

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

      Why you types wrong?

  • @andrefreitas9936
    @andrefreitas9936 8 วันที่ผ่านมา

    bro is repeating questions 10times for the views💀 toxic content

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

    Don't listen to this guy 😅