Timeline for Hacker Rank: Array left rotation
Current License: CC BY-SA 4.0
13 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Apr 10, 2020 at 7:28 | comment | added | Michel Billaud | @Edwin "perform d left rotations on the array. Then print the updated array". So updating the array is required. | |
| Mar 5, 2019 at 19:36 | comment | added | Voo | @Edwin Ah yes that makes sense, "First versions second loop" is easy to miss in short comments. The second one seems fine (I did write some tests to assure myself ;) ), except for the wraparound behavior you noted (which could be easily fixed by using a long, but then the chances of this ever being observed are slim to none). | |
| Mar 5, 2019 at 19:33 | history | edited | Edwin Buck | CC BY-SA 4.0 |
added 22 characters in body
|
| Mar 5, 2019 at 19:32 | comment | added | Edwin Buck | @Voo Ah, I see, I didn't add the mod to the first set of examples. I've been here thinking that the "condensed" second program was the one you were noting through an out-of-bounds, when all along it was the first (which works, but only for a more limited set of inputs). I know these comments are awfully short, so a short call-out to the failing text isn't always possible. I'm updating it now | |
| Mar 5, 2019 at 19:28 | comment | added | Voo | @Edwin I literally posted the exact set of numbers for which your first method will throw an exception and I already agreed that the second one doesn't, shouldn't be too confusing for people. | |
| Mar 5, 2019 at 19:17 | comment | added | Edwin Buck |
@Voo Now that you see your comments are misleading, please consider removing them so others are not misled. Yes, you can do N rotations with the above code. That's because on a 5 element array, a rotation of 10 and a second rotation of 3 combine as a rotation of 13 (which mod 5 equals 3). The code above doesn't care if you rotate beyond the end of the array (in combined rotations or single ones) as long as the sum of your combined rotations and the array size doesn't exceed MAX_INT.
|
|
| Mar 5, 2019 at 17:15 | comment | added | Voo |
@Edwin Yeah I missed the modulo on your second loop on my phone. Not entirely sure if it works correctly - I doubt it, but updating loop end conditions in every iteration makes it rather hard to reason about loops in your head. Your first version definitely crashes out of bounds on the second loop as soon as iterations > i >= arr.len.
|
|
| Mar 5, 2019 at 17:10 | comment | added | Edwin Buck |
@Voo I think you keep focusing on how (in my latter example) i is often out-of-range of the array. That's probably why you keep saying "out of bounds" which would cause an error if I was pulling out the i element of the array. But I'm not pulling i out of the array, I'm pulling i % a.Length which can't be out of bounds.
|
|
| Mar 5, 2019 at 8:15 | comment | added | Astrinus |
@Voo: if you read the challenge, you see that 1 <= d <= n, i.e. rotate an n elements array by d steps, so you cannot have more rotations than elements.
|
|
| Mar 4, 2019 at 21:14 | comment | added | Voo |
You're supposed to do n rotations. Nowhere does it say that n < arr.Length. You could have an array of 1,2,3 and d=5. Both of your solutions fail with an out of bounds there.
|
|
| Mar 4, 2019 at 20:20 | comment | added | Edwin Buck |
@Voo Out of curiosity, which modulo does this miss? I know I wrote it by hand without the aid of a computer, but the loop (in my 5 element example, starting at index 3 would be 3, 4, 5, 6, 7 (8 would be 5 + 3 - 8, so it wouldn't get there). and 3 % 5 = 3, 4 % 5 = 4, 5 % 5 = 0, 6 % 5 = 1, 7 % 5 = 2. Now, I just might have a bug in there after all; but, maybe not, which is a perfect example of why writing it this way is less than ideal and why hackerranks fascination with programming competitions as question banks is bad.
|
|
| Mar 4, 2019 at 19:21 | comment | added | Voo |
Well one thing one should certainly learn from this challenge is what string.Join does and how it avoids the rather awful for loop :) (The given solution also misses some necessary modulo ops)
|
|
| Mar 4, 2019 at 19:19 | history | answered | Edwin Buck | CC BY-SA 4.0 |