Coding Interview: Can You RANDOMLY Reorder Array in O(N)?

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ต.ค. 2024

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

  • @drew4980
    @drew4980 7 ปีที่แล้ว +4

    Great explanation! I like the simple example and using arrows to point at which index is being changed. The repetition helps a ton as well. Hope you keep doing more of these!

  •  3 ปีที่แล้ว +6

    Just to a note, for swapping variables in Python you should be able to use a tuple
    arr[i], arr[pick] = arr[pick], arr[i]

  • @alshbsh2
    @alshbsh2 4 ปีที่แล้ว

    easy:
    array a with size n
    for i

  • @z08840
    @z08840 3 ปีที่แล้ว +2

    I suspect that by scaling random index to remaining array you will break uniformity - you actually don't need to scale - just take each index and pick random index for swap from whole array

  • @noamzilo6730
    @noamzilo6730 5 ปีที่แล้ว

    this solution lacks the very important PROOF that the probability for any number to land at any index is the same, so here it is:
    calculation for the probability for some index i to land in index 0:
    1/n, as it is the 1st iteration.
    calculation for the probability for some index i to land in index 1:
    (n-1)/n * (1/(n-1)). the left hand side is the probability for index j!=i to be selected, because i is already selected, and can't be selected again.
    the selection from (n-1) index has probability of (1/(n-1)).
    Now apply same logic for array of size k=n-1, for any k.
    stopping condition: n=1 (or 0), for probability 1 to land in index 0.
    Python code:
    def swap(arr, i, j):
    assert i < len(arr)
    assert j < len(arr)
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
    def reorder_in_place(arr):
    length = len(arr)
    for i in range(length):
    dice = random.randrange(i, length)
    swap(arr, i, dice)
    return arr

  • @researchandbuild1751
    @researchandbuild1751 6 ปีที่แล้ว +2

    Pretty sure Knuth shuffle is O(n) and it also does it in place. Just study Knuth shuffle, it's a super easy algorithm. It's useful in making card games too, so now you can use it for more than just coding interviews

  • @crazymajax
    @crazymajax 7 ปีที่แล้ว +16

    You should not shrink the size of the pool of positions that you randomize your index on. You should always use "floor(rand() * size_of_array)" for a truly random result.
    Here's one way to think about it. If it was truly random it should be possible to get the same array as an output as the one you get as an input. With the solution I gave you you can get there.
    Great video!

    • @ponaskocelas
      @ponaskocelas 6 ปีที่แล้ว

      Could you articulate why?

    • @mattcay
      @mattcay 6 ปีที่แล้ว +1

      This is the straight-forward approach, unfortunately it doesn't give you a random permutation. As a short mathematical pseudo-proof for an intuition: Assume that you have an array of size n. At each step your algorithm has n possible choices. Then after n iterations you have n^n total possible outcomes of the algorithm. However the total number of permutations of n element array is n!. Unfortunately n^n = n * n * ... * n cannot possibly be evenly divided by n! = n * (n-1) * ... * 2 * 1. Therefore there must be some permutations that are more likely then others. For a better explanation (also with simulations using the different algorithms) there's this wonderful blog post: blog.codinghorror.com/the-danger-of-naivete/

    • @toucan012
      @toucan012 6 ปีที่แล้ว +1

      His approach does let you get the same array as output because the algorithm let's an index swap with itself. Look at 3:16 in the video. He does screw that fact up in the rest of the evaluation. He should have multiplied by 5 for the first iteration instead of 4, and that would have made it completely random.

    • @researchandbuild1751
      @researchandbuild1751 6 ปีที่แล้ว

      Nope, look up Knuth Shuffle, it's the 100% proper way to randomize an array in place and it's dead simple too

    • @jayquelin
      @jayquelin 5 ปีที่แล้ว

      NOOO!!! Delete this comment, you are misleading people and it has too many upvotes. You will "over shuffle" if you do that. This is somewhat hand wavey, but consider this:
      You will have n^n shuffles in your naive method. There are n! possible arrangements of cards. Clearly n^n > n!, meaning you are generating more outcomes that possible arrangements, meaning multiple outcomes are getting mapped to the same arrangement.
      Rigorous proof:
      stats.stackexchange.com/a/3087

  • @connoryass4573
    @connoryass4573 6 ปีที่แล้ว +4

    I was just asked this question at an interview with Amazon yesterday. I wish I had seen this video a few days ago!

    • @iam_one_
      @iam_one_ 5 ปีที่แล้ว

      Was this interview an onsite interview? I ask because I have one coming up, and so how did it go for you?

    • @harshithpillai7782
      @harshithpillai7782 4 ปีที่แล้ว

      F

  • @fredbig6789
    @fredbig6789 6 ปีที่แล้ว

    You can get a randomize discrete number for each index by floor(rand()*100) then you O(n) sorting algorithms like bucket sort since the numbers are refined from 0 to 99.

  • @MohitSharma-nf8un
    @MohitSharma-nf8un 7 ปีที่แล้ว +14

    for i = 0 to n -1:
    temp1 = floor(rand()*n)
    temp2 = floor(rand()*n)
    swap(arr[temp1],arr[temp2])
    What is wrong with this?
    It is O(n) (Swapping can be done using another temp variable)

    • @MoodyG
      @MoodyG 6 ปีที่แล้ว +2

      Mohit Jarvis Sharma that's the first solution to come to my mind too, except I ran the loop n/2 times.

    • @timonpasslick
      @timonpasslick 6 ปีที่แล้ว +3

      The possible new orders aren't evenly distributed. One might occur more often than the others.
      But when you write "for i in range(r):", the distribution becomes more even when r becomes bigger and it becomes less even when the array size increases.
      We'd have 0(r) for time and 0(1) for space.

    • @z08840
      @z08840 3 ปีที่แล้ว

      @@timonpasslick it's not true if rand returns perfectly continuous distribution [0,1) - you always can map continuous distribution to any discrete

    • @timonpasslick
      @timonpasslick 3 ปีที่แล้ว

      @@z08840 Then it would be true if this was the Fisher Yates shuffle but with this algorithm it's not.

    • @z08840
      @z08840 3 ปีที่แล้ว

      @@timonpasslick I was referring to your suggestion that "One might occur more often than the others" - it's not true.
      The algorithm itself is not good, of course, - since both indexes are randomly selected known number of times we can calculate how many indexes with which probability will remain on their places.

  • @Toopa88
    @Toopa88 6 ปีที่แล้ว +1

    My solution is to call random and floor function to get the random number. You start at index 0 and go to index n-1. You get your random value (which is between 0 and n-1), and make it the new index for your current index, so you swap the values (need a temporary variable). Repeat this till you reached the end of the array and you are done.

    • @AK09037
      @AK09037 3 ปีที่แล้ว

      Random value should be between index and n-1 because list before index is already shuffled

  • @silvanojr
    @silvanojr 7 ปีที่แล้ว +17

    Great videos! What is the software you use for the drawings?

  • @sumantkanala
    @sumantkanala 6 ปีที่แล้ว

    a very systematic approach. Love it!

  • @shoooozzzz
    @shoooozzzz 7 ปีที่แล้ว +7

    If you swap the last index (in this case 4) with position 0 through 3.. that means there is a 0% chance the number will stay in its current position. This would not be a truly random number and predicting the outcome of the "shuffle" becomes easier

    • @Luffi98
      @Luffi98 6 ปีที่แล้ว

      Ben Wright You also count that number in the random

    • @q00u
      @q00u 5 ปีที่แล้ว +2

      When going through it the first time (around 2:20) he specifically says he's picking a value out of ALL the values in the array.
      Later, in the pseudocode (7:05) it specifically shows pick = floor( (i+1) * rand() )
      That i+i _includes_ the index being selected for.
      So you're never swapping the last index (in this case 4) with just positions 0 through 3. pick = floor( (4+1) * rand() ) = floor( 5 * rand() ) = {0, 1, 2, 3, 4} (each with a 20% chance of being picked)

  • @0969superman
    @0969superman 5 ปีที่แล้ว

    from random import randint
    import math
    def rand():
    alea = randint(0,1000)
    return alea/1000
    def floor(num):
    return math.floor(num)
    def switch(lst, i, j):
    temp = lst[j]
    lst[j] = lst[i]
    lst[i] = temp
    def random_reor(lst):
    for i in range(len(lst)):
    switch(lst, i, floor(rand()*len(lst)))
    that's my sol

  • @jayh5992
    @jayh5992 6 ปีที่แล้ว

    I would use
    floor((rand() * 10) / 2) = i
    for n in array:
    Swap value i and n
    Not sure if thats O(N) but it seems efficient to me

  • @SirSethery
    @SirSethery 6 ปีที่แล้ว

    I have a problem with that method in that it isn't random.
    If it were truly random, the last number would have just as high of a chance of _remaining_ as the last number as any other number would have a chance of replacing it.
    The solution I had was similar, but you could switch with either the left or the right side of the current number. You could pick a random number from the left side and from the right side and pick one of those two to switch, weighing the odds by how many numbers were on each side. If you wanted, you could repeat the whole thing a few times to make it more random, and it would remain O(n).
    *Edit*
    Actually, it may be easier to simply do this method from right to left and then repeat it from left to right while still being random **enough**

    • @q00u
      @q00u 5 ปีที่แล้ว

      The last number *DOES* have just as high of a chance of remaining as the last number as any other number has of replacing it. It's included in the indexes being selected from. It's stated in words when he starts his demonstration ("We choose from ALL the numbers in the array"), it happens during his demonstration, and it's coded in the pseudocode.
      Why are there so many comments with this mistake in them?

  • @faveladoboy
    @faveladoboy 6 ปีที่แล้ว

    floor(4*rand()) could be equal to 4 if rand()==1.
    This is unlikely to happen, but if it does, you'll try to access an invalid memory position

    • @GiorgioBalduzzi
      @GiorgioBalduzzi 6 ปีที่แล้ว +1

      Look at the explaination of the rand() function, he clearly says 0

    • @q00u
      @q00u 5 ปีที่แล้ว

      It's not only unlikely to happen, it's literally impossible. rand() will NEVER equal 1. It can't. Look at the definition.

  • @ravitanwar9537
    @ravitanwar9537 6 ปีที่แล้ว +1

    from random import shuffle
    def reorder(arr1):
    shuffle(arr1)
    print(arr1)
    arr1=[1,0,3,9,2]
    reorder(arr1)

  • @gopimurugesan8411
    @gopimurugesan8411 6 ปีที่แล้ว +4

    how did u learn this..
    I am a bit confused..
    can I learn by any source..
    have any link?????😢☺

  • @lovefor011
    @lovefor011 4 ปีที่แล้ว +2

    2:53 at this point you can choose 9 again.. then it will not be suffled?

    • @absence9443
      @absence9443 4 ปีที่แล้ว +1

      ik this is 7months old, but if you allow it to choose one of the ones that havent been shuffled yet, than 9 could be chosen as well which would be random as well. Random shuffling doesnt require new positions for each value. Theoretically you dont even need to abandon the already shuffled ones so you could switch the current value with any value in the array, but it raises the chance of doing the same steps reversed, so this is very likely the best solution for O(n).

  • @wulymammoth
    @wulymammoth 6 ปีที่แล้ว

    There is a lot of confusion here. I believe what you were going for is the modern Fisher-Yates shuffle algorithm. Anybody interested can look it up on Wikipedia.
    The modern FY states that when you first start from right to left at n-1, 0

    • @ZantierTasa
      @ZantierTasa 2 ปีที่แล้ว

      What error?? That's exactly what the code in the video does.
      The code in the video is:
      pick = floor((i + 1) * rand()),
      after which 0

    • @wulymammoth
      @wulymammoth 2 ปีที่แล้ว

      @@ZantierTasa if you look at 4:58, you'll see that the range is 0

    • @ZantierTasa
      @ZantierTasa 2 ปีที่แล้ว

      @@wulymammoth The example at 4:58 is the SECOND loop of the algorithm, where the 5th element of the array (highlighted in blue) has already been done. So picking a number between 0 and 3 inclusive is correct.

  • @kubic71
    @kubic71 6 ปีที่แล้ว +11

    You didn't actually prove that every permutation is equally likely...

    • @Schnorzel1337
      @Schnorzel1337 5 ปีที่แล้ว +3

      It kinda follows from the algorithm. First you have n possible positions, then n-1 down to 1. Exactly n! possible configurations. And that is the formula for calculating the number of permutations of an array of size n.

    • @thest.kitts-nevislabourpar3281
      @thest.kitts-nevislabourpar3281 4 ปีที่แล้ว +4

      Why couldn’t you just visit each position of the array and, for each such position, randomly generate a different position to which the current element should be transposed. Of course, this means swapping the current position with the random position. Some elements will definitely get swapped more than once. It is even possible that some elements don’t get swapped at all. But that’s all determined by the rand() function, which is completely ok.

    • @twofacer5
      @twofacer5 4 ปีที่แล้ว

      @@thest.kitts-nevislabourpar3281 Had the exact same approach. Even if some numbers don't change their position after execution, it is due to the rand() function and can't be predicted.

    • @moofymoo
      @moofymoo 3 ปีที่แล้ว

      to settle this, we can use a computer! write test - rearange array (2 or more elements, with unique values) billion times and calculate average value for each position.

  • @kyle7382
    @kyle7382 6 ปีที่แล้ว

    function randomize(arr){
    for(var i = 0; i

  • @CodeProps
    @CodeProps 5 ปีที่แล้ว

    In Python :
    # Rearranging elements of an array in O (n)
    import random
    x = [7, 5, 2, 11, 4, 9]
    s = set()
    i = 0
    while len(s) is not len(x):
    curr_index = random.randint(0, len(x)-1)
    if curr_index not in s:
    x[curr_index], x[i] = x[i], x[curr_index]
    s.add(curr_index)
    i+=1
    print(x)

  • @unoriginalusernameno999
    @unoriginalusernameno999 5 ปีที่แล้ว

    Well you did not define random in any particular way so, since every permutation of the array of n integers is equally "random" by using rand() we just set the initial index say, as 0. Then get a random number using rand() say 5 and then lets suppose the value of element at index 5 is 3. We swap the values of elements at initial index and at index 5. Then we set the new initial index to 3. This way we can go through n! iterations but not generate all n! permutations.

  • @felsherrr
    @felsherrr 2 ปีที่แล้ว

    Do we loop backwards just for the sake of handy floor( (i+1) * rand() ) ?

  • @killerthoughts6150
    @killerthoughts6150 3 ปีที่แล้ว

    i could only think of o(n), log n approach doesn't seem to be a natural thought for anyone

  • @aenesunal
    @aenesunal 4 ปีที่แล้ว

    You should've chosen ceil or floor equally likely.

  • @josephluce3801
    @josephluce3801 7 ปีที่แล้ว +1

    Just some improvements in respect to floor and rand. Completely confused on whether you must use these two functions or not because the way you used floor is similar to the way you would use mod. The way you explained the question didn't clarify if I could use other operations like multiply or mod. You could easily not need floor to solve this problem and used a different set of operations. Also, didn't clarify whether rand returned a float or an integer. Complete throw off.

    • @josephluce3801
      @josephluce3801 6 ปีที่แล้ว

      Peterolen one thing I learn during interviews is you cant assume anything. Everything must be explicit.

    • @ZantierTasa
      @ZantierTasa 2 ปีที่แล้ว

      At 00:29, the video shows "0

  • @BruceLeeInsights
    @BruceLeeInsights 7 ปีที่แล้ว +13

    We could use
    (100 * rand()) % current_index

    • @tobiasfuchs7016
      @tobiasfuchs7016 7 ปีที่แล้ว +11

      Mathematically, yes, but modulus is the most expensive arithmetic operation there is:
      embeddedgurus.com/stack-overflow/2011/02/efficient-c-tip-13-use-the-modulus-operator-with-caution/
      Does not affect complexity of course, but especially on architectures with less sophisticated ALUs it's an order of magnitude in performance.

    • @timonpasslick
      @timonpasslick 6 ปีที่แล้ว +1

      If you really want to do that, don't just pretend size < 100. Do (size * rand()) % current_index.
      However, the solution in the video is more efficient and more straight forward.

    • @toucan012
      @toucan012 6 ปีที่แล้ว +5

      That solution is incorrect. It isn't completely random. Take for example an array of 49 elements. The numbers that map to index 0 with your function are 0, 49, and 98 giving it a 3% chance of being picked. Similarly index 1 will also have a 3% chance of being selected. Every other index will have a 2% chance of being selected. The effect of this is that numbers in smaller indices will have a higher probability of being mapped to larger indices in the new mapping. This is a problem for every number of indices that does not evenly divide 100, so even iteration down to 25 would be affected too. Which means it is not truly random because the mapping is not independent of current position in the array. The lack of randomness would have a pretty noticeable effect in practice.

  • @subscripciones
    @subscripciones 5 ปีที่แล้ว

    why it is necesarry to do n to 1, and not 1 to n?

  • @WakefieldSeldon
    @WakefieldSeldon 6 ปีที่แล้ว

    My solution in JS before watching the video:
    function interval_int_rand(low, high) {
    return Math.floor(Math.random() * (high - low) + low)
    }
    function rearrange(array) {
    const result = [...array];
    for (let i = 0; i < result.length; i++) {
    const index = interval_int_rand(i, result.length);
    const temp = result[i];
    result[i] = result[index];
    result[index] = temp;
    }
    return result;
    }

  • @OrangeC7
    @OrangeC7 6 ปีที่แล้ว +2

    😱😱😱 so random 😂

  • @moofymoo
    @moofymoo 3 ปีที่แล้ว

    Or you could convince management that requirements are wrong and changing array order randomly is no good for business.

  • @abhirammadhu2973
    @abhirammadhu2973 3 ปีที่แล้ว

    This is kinda like selection sort

  • @Soap_js
    @Soap_js 6 ปีที่แล้ว

    complicated but helpful

  • @InderMasterPlayer
    @InderMasterPlayer 5 ปีที่แล้ว

    This should solve the problem:
    const shuffleOrder = (arr) =>{
    for(let i = 0; i

  • @pveentjer
    @pveentjer 5 ปีที่แล้ว

    Big O for quicksort is n^2
    en.wikipedia.org/wiki/Quicksort

  • @Oh_Sully
    @Oh_Sully 6 ปีที่แล้ว

    For the O(n) solution, isn't the fact that any given index is guaranteed to not stay the same (since only the other indices are valid choices to swap with) , mean that it is not randomly reordered because [0,1,2,3,4] -> [1,0,2,3,4] is a reordered array by only changing two indices and leaving the rest as is?

    • @Oh_Sully
      @Oh_Sully 6 ปีที่แล้ว

      Peterolen That's a yes, right?

    • @Oh_Sully
      @Oh_Sully 6 ปีที่แล้ว

      Peterolen But in the O(n) solution, he excludes picking index 'i' as an option to switch with. So wouldn't that make the array staying exactly the same impossible?
      To be clear, when I say 'the' O(n) solution, I am referring to the O(n) solution presented in this video.

  • @chrisliu5403
    @chrisliu5403 6 ปีที่แล้ว

    thanks dude

  • @indian6755
    @indian6755 7 ปีที่แล้ว +2

    good

  • @Amin-wd4du
    @Amin-wd4du 5 ปีที่แล้ว

    Your solution is not correct. Because the order is not random. For example 1 will always end up in first or second items of the array and no where else.

    • @alf1409
      @alf1409 4 ปีที่แล้ว +1

      Not true. You are missing the probability that any other item gets swapped with 1.

  • @ParasBansal10
    @ParasBansal10 7 ปีที่แล้ว

    What if you picked a number bigger than the size of the array. in your example lets say you picked 9 then you had to put the element on the 9th index and it doesn't exist. Also what about characters? You said there could be anything.
    I had a solution in just 2 min when you said to pause. Complexity O(n/2).
    Just loop to the half of array and generate 2 random numbers and swap arr[rand1] with arr[rand2]. It should be pure random because rand1 or rand2 could be same in another ittration.

    • @ParasBansal10
      @ParasBansal10 7 ปีที่แล้ว

      Looping half of the array. How is it equal to looping complete array?
      We are considering n as the size of the array. right?

    • @q00u
      @q00u 5 ปีที่แล้ว

      It's impossible to pick a number bigger than the size of the array, according to the definitions of the functions given. If you believe otherwise, then you have misunderstood the definitions.
      Characters, Strings, anything would also work. We're selecting from the _indexes_, and the contents don't matter, because they never enter into the equation.
      O(n/2) *is* O(n). If you believe otherwise, then you have misunderstood the meaning of Big O.
      Your solution is not uniformly random. It's mentioned many times in the comments, with many explanations as to why it isn't.

  • @sumantkanala
    @sumantkanala 6 ปีที่แล้ว

    Amazing. This feels like the bottom up approach. Correct me if I'm wrong!

  • @ravitanwar9537
    @ravitanwar9537 6 ปีที่แล้ว

    if this question was asked in a coding interview question should we use library function ?

    • @Tylauriente
      @Tylauriente 5 ปีที่แล้ว

      You can always ask during the interview. Most likely they won't mind.

  • @budhachandrayumkhaibam6079
    @budhachandrayumkhaibam6079 6 ปีที่แล้ว

    hey YK. how do we do "for i from n-1 down to 1" (6:32), in python. Its like a reverse loop which iterates from the opposite end index.
    plz help

    • @davbou
      @davbou 6 ปีที่แล้ว

      You might want to learn about the slice operator in python wich can do exactly what you want.
      www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/

  • @gsniteesh3794
    @gsniteesh3794 7 ปีที่แล้ว

    Can we generate a random number on every iteration and swap the current index element with a the random no. Is this a good solution. Code for this is below:
    #include
    #include
    #include
    using namespace std;
    int main(int argc, char const *argv[])
    {
    int a[] = {3,4,2,1,7,6};
    srand(unsigned (time(NULL)));
    int r;
    for (int i = 0; i < 6; ++i)
    {
    r = rand() % 6;
    int temp = a[i];
    a[i] = a[r];
    a[r] = temp;
    }
    for (int i = 0; i < 6; ++i)
    {
    cout

  • @abhay8437
    @abhay8437 7 ปีที่แล้ว +2

    eZpZ lemon squeezy

  • @orr4945
    @orr4945 6 ปีที่แล้ว +1

    The question's instructions are very unclear...
    Whats stopping you from setting a random index for each object in the array for example...

    • @kamoroso94
      @kamoroso94 6 ปีที่แล้ว

      You could try and put two things at the same random index. Then you would have to linear search for an empty slot. This is basically getting hashing collisions. It's inefficient.

  • @amanjain9958
    @amanjain9958 3 ปีที่แล้ว

    Just do this , swap(arr[i],arr[rand()%(i+1)]); from i to n;

  • @alecstrickland7182
    @alecstrickland7182 7 ปีที่แล้ว

    Wouldn't the first solution's time complexity be O(n+nlogn)? There's an n term for assigning each element a key, and then nlogn to quicksort the array.

    • @nju415
      @nju415 7 ปีที่แล้ว +8

      Alec Strickland O(n+nlogn) = O(nlogn). U can prove this using limit theorem. I.e lim(n + nlogn) / nlogn = 0

  • @getitjob
    @getitjob 4 ปีที่แล้ว

    I did this
    lista.sort(key=lambda x : random.random())
    simple and I using less registers then You

    • @coolfunmario
      @coolfunmario 4 ปีที่แล้ว +1

      Sorting is at best O(n log n) time complexity.
      The algorithm exposed in the video is O(n)

    • @getitjob
      @getitjob 4 ปีที่แล้ว

      @@coolfunmario you are right thanks :)

  • @ishaan2132
    @ishaan2132 6 ปีที่แล้ว

    Do we really learn all this in college.

  • @corejake
    @corejake 3 ปีที่แล้ว

    This one was kinda obvious.

  • @arch126
    @arch126 5 ปีที่แล้ว

    Bogo sort ftw

  • @pokoro97
    @pokoro97 5 ปีที่แล้ว

    C++ rand() function really sucks so it might not work well for c++ haha

    • @Tylauriente
      @Tylauriente 5 ปีที่แล้ว +1

      Works fine :)
      Slightly modified
      #include
      #include
      int Rand(int min, int max)
      {
      if (max - min == 0) return 0;
      return rand() % (max - min) + min;
      }
      template
      void RandomReorder(T arr[], int size)
      {
      srand(time(0)):
      for (int i = 0; i < size; ++i)
      {
      int swapIndex = Rand(0, size - 1);
      T temp = arr[i];
      arr[i] = arr[swapIndex];
      arr[swapIndex] = temp;
      }
      }

  • @fredbig6789
    @fredbig6789 6 ปีที่แล้ว +1

    Your solution is wrong. Any number never stays in their same place(0 probabily), so it's not random.

    • @AlejandroBachi
      @AlejandroBachi 6 ปีที่แล้ว +1

      Mmm, no, a number could stay or be put back later at it's initial place. Why do you think there is a restriction on that? The code allows it

    • @q00u
      @q00u 5 ปีที่แล้ว

      TWICE in the video it's specified that a number has a chance to stay where it is. Where are you getting this 0% probability from? Did you not understand the explanation or the pseudocode?

  • @siddhantdixit9066
    @siddhantdixit9066 6 ปีที่แล้ว

    I have an awesome solution

  • @RedPanther2030
    @RedPanther2030 5 ปีที่แล้ว

    Here's javascript solution (ps I didn't see the solution so it might be bad):
    function random(arr)
    {
    var rands = {};
    for(var i=0; i

  • @loremipsumproductivityengi7552
    @loremipsumproductivityengi7552 7 ปีที่แล้ว

    Frankly, this wouldn’t be O(n) if it is a large linked list

    • @Tylauriente
      @Tylauriente 5 ปีที่แล้ว

      It wouldn't be O(n) with any link list because accessing an item on a list itself is O(n). It is assumed that the data would have a constant look up time.

  • @Kenlauderdale123
    @Kenlauderdale123 6 ปีที่แล้ว

    I was thinking to solve it by randomly swapping the contents of the array n times... 😆

  • @calebpetrick1097
    @calebpetrick1097 5 ปีที่แล้ว

    This solution is far from random. The array will ALWAYS begin with 9,1 or 1,9 ...

    • @noamzilo6730
      @noamzilo6730 5 ปีที่แล้ว

      wrong.

    • @q00u
      @q00u 5 ปีที่แล้ว

      No? If 1 or 9 are randomly selected to be swapped to the end at ANY point, then obviously no? Remember that it was originally 1, 0 at the beginning and 0 got swapped out?
      It's only 9,1 or 1,9 _at the end_ when it's down to just two unplaced numbers.

  • @jayquelin
    @jayquelin 5 ปีที่แล้ว

    wtf this is so easy who would actually ask this .... for internships i guess?

    • @Tylauriente
      @Tylauriente 5 ปีที่แล้ว

      Most interviewers will ask many questions. Including easy to difficult problems. Often it's the way that one solves the problem that matters.
      Also, there are many different fields of software development, with many different requirements.

  • @Luffi98
    @Luffi98 6 ปีที่แล้ว +1

    You can also do it without extra space