Skip to content

Instantly share code, notes, and snippets.

@chygoz2
Last active May 16, 2021 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chygoz2/144716c43d42e727b060c80ec2d2ebf6 to your computer and use it in GitHub Desktop.
Save chygoz2/144716c43d42e727b060c80ec2d2ebf6 to your computer and use it in GitHub Desktop.
Algorithm week 6 challenge
const shuffleClass = (classList, count) => {
if (isNaN(count) || !Array.isArray(classList)) throw new Error('Invalid input ');
let newClassList = [];
if (count === 0 || Math.abs(count) >= classList.length) return classList;
if (count > 0) {
for (let i = classList.length - count; i < classList.length; i++) {
newClassList.push(classList[i]);
}
for (let i = 0; i < classList.length - count; i++) {
newClassList.push(classList[i]);
}
} else {
count = Math.abs(count);
for (let i = count; i < classList.length; i++) {
newClassList.push(classList[i]);
}
for (let i = 0; i < count; i++) {
newClassList.push(classList[i]);
}
}
return newClassList;
};
@meekg33k
Copy link

meekg33k commented May 16, 2021

Hello @chygoz2, thank you for participating in Week 6 of #AlgorithmFridays.

This is a really good attempt at coming up with a solution for Apex College, and your solution passes most of the test cases. I particularly like your robust checks on lines 2 and 6.

However, your solution doesn't pass the following test cases:

  • When classList has a null value. In your solution, you threw an error but the preferred approach would have been to log an error and return an empty array instead.

  • The other test your solution doesn't pass is for cases where count has a value greater than the size of the classList. For such cases, your solution returns the classList. For example, shuffleClass([2, 3], 3); // yours would return [2, 3] instead of [3, 2]. The expectation is that when the value of count is greater than the classList, you should think of it as shuffling the entire pupils list for as many times as is possible until count becomes less than the size of classList. In mathematical terms, that would be count = count % classList.length.

It's always best to check in with your interviewer before making assumptions on how edge cases should be handled. I wrote about that in this article, you might find it helpful.

Apart from that, this was a good attempt. Please let me know your thoughts.

@chygoz2
Copy link
Author

chygoz2 commented May 16, 2021

Thank you @meekg33k. This is valuable feedback that I'll bear in mind going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment