Efficient Algorithm for Finding the Mode | Quant Interview Questions

Поделиться
HTML-код
  • Опубликовано: 28 янв 2025

Комментарии • 17

  • @johnchessant3012
    @johnchessant3012 3 года назад +5

    Cool solution. Here was mine: Move along the vector, deleting each pair of consecutive entries that are different values. After deleting, check the new previous and next entries, deleting those if necessary. At the end, the only ones that remain will be equal to the mode.

    • @atypicalquant
      @atypicalquant  3 года назад +1

      Hi John,
      If I understood your comment correctly, you would do the following:
      For AAAAABCD:
      Step1: No deletions, Store [A]
      Step2: No deletions, Store [AA]
      Step3: No deletions, Store [AAA]
      Step4: No deletions, Store [AAAA]
      Step5: No deletions, Store [AAAAA]
      Step6: Delete (A,B), Store [AAAA]
      Step7: Delete (A,C), Store [AAA]
      Step8: Delete (A,D), Store [AA]
      And, from this, you will return A as the mode.
      If this is the case, the algorithm has the same 'core' as the one in the video (and thus, will work!), but is slightly less efficient as you're keeping a vector instead of counts.
      It's a cool solution (albeit its less than perfect efficiency), thanks for sharing!

    • @johnchessant3012
      @johnchessant3012 3 года назад +1

      @@atypicalquant Yep you're right! Now I understand your solution better too: It's more memory-efficient because at each step your solution stores the shorthand for the vector stored in my solution; e.g., (A, 4) instead of [AAAA], (?, 0) instead of [ ]. Thanks for replying!

  • @simonpedey6814
    @simonpedey6814 3 года назад

    Hey, I came across this interesting question you might want to have a go at.
    You have a biased coin with probability 0.7 heads. You flip once and it is tails. What is the expected number of flips until you get the same number of heads as tails. (Counting the first flip as #0)
    I dont have a solution yet so will be interested to see what you come up with

    • @atypicalquant
      @atypicalquant  3 года назад

      This is a really interesting question that I've never heard before, thanks for sharing!
      As for k throws you expect to get 0.7k Heads and 0.3k Tails, you would want 0.7k = 0.3k +1, which would imply k=2.5. This is indeed the right answer, as confirmed by a quick simulation, but the reasoning isn't very convincing.
      For a more rigorous solution, you would probably want to compute [Sum Over i] (2i+1) * P(X=2i+1), possibly using Catalan numbers to compute the probability.
      This is just from the top of my head. When I'll have more time I'll check it more thoroughly and maybe post a dedicated video about it.

    • @jugulartrujillo865
      @jugulartrujillo865 3 года назад

      E(x)= 0.7*1 + 0.3 * (2*E(X)+1)
      E(x) =2.5
      If you throw heads you will finish game in 1 step, otherwise you will have to repeat procedure of "decreasing number of tails by 1" twice

  • @onceappuonatime
    @onceappuonatime 3 года назад +1

    really helpful

  • @patrickchen7162
    @patrickchen7162 Год назад +1

    Can you help me with the understanding sorry I’m not very good math but I chanced upon your video.
    If considering the solution from the earlier parts,
    ABCDEDDDDE: mode would be D considering the calculations;
    A1 ?0 C1 ?0 E1 ?0 D1 D2 D3 D2
    However, what if the letters are ABBACCA: am I wrong to calculate by;
    A1 ?0 B1 ?0 C1 C2 C1. The mode would be C which is wrong

    • @atypicalquant
      @atypicalquant  Год назад

      You have correctly applied the algorithm to the example you have, and it indeed does not produce the correct result. This is because, as stated in the question, the algorithm works when the mode of the array has more than half of the frequency in the array. This is not the case for your example, giving that D has frequency 0.5.

  • @colinmignot6309
    @colinmignot6309 3 года назад +1

    Hi, sound is very low "compared to other videos"
    The voice sounds better great job. I'd advice you speak with more volume, rn it sounds like you are whispering (LOTS of vocal fry)

  • @hcasavantes
    @hcasavantes 2 года назад

    It doesn't work...... for example [1,1,1,2,3,4] will give "None" but not 1, and [4,1,1,2,3,1,4] wll give 4 but not 1

    • @atypicalquant
      @atypicalquant  2 года назад +1

      In both your examples, there is no element that appears more than half time (1 has frequency 3 in vectors of length 6 and 7). The algorithm guarantees a result if the most frequent element appears more than half the time, as stated at 0:14.

    • @hcasavantes
      @hcasavantes 2 года назад +3

      @@atypicalquant Agree, but then don't call it "mode" 0:20 but something else

    • @atypicalquant
      @atypicalquant  2 года назад +1

      @@hcasavantes What we are computing is actually that vector's mode, the extra conditions we're requiring don't change the name.
      Just like a Marsenne prime is still a prime.

    • @hcasavantes
      @hcasavantes 2 года назад +1

      @@atypicalquant Well, something that is actually in-correct and un-useful doesn't mean it's useful and correct due extra conditions. And the title as also appears missleading from my pov. Thanks anyway.

    • @jeehochoe542
      @jeehochoe542 2 года назад +2

      Agreed. Finding a law for Marsenne prime doesn't allow you to say you found a law for primes in general.