Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Created May 15, 2021 11:37
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 Ezeji/0ddd36d25d25e13dab75d8c794b2afc7 to your computer and use it in GitHub Desktop.
Save Ezeji/0ddd36d25d25e13dab75d8c794b2afc7 to your computer and use it in GitHub Desktop.
This algorithm shuffle pupils on an assembly line by moving a number of pupils either to the front of the line or to the end of the line.
class Program
{
private static List<int> Result { get; set; } = new List<int>();
static void Main(string[] args)
{
int[] nums = { 4, 1, 3 };
int val = -1;
ShufflePupils(nums, val);
PrintArray(Result);
}
public static void SplitFirstSetFromArrayIntoNewArray(int[] nums, int val)
{
for (int i = 0; i < nums.Length; i++)
{
if (i >= Math.Abs(val))
{
Result.Add(nums[i]);
}
}
}
public static void SplitSecondSetFromArrayIntoNewArray(int[] nums, int val)
{
for (int i = 0; i < nums.Length; i++)
{
if (i < Math.Abs(val))
{
Result.Add(nums[i]);
}
}
}
public static void ShufflePupils(int[] nums, int val)
{
if (nums == null) { }
else if (nums != null && val == 0)
{
Result.AddRange(nums);
}
else
{
SplitFirstSetFromArrayIntoNewArray(nums, val);
SplitSecondSetFromArrayIntoNewArray(nums, val);
}
}
public static void PrintArray(List<int> result)
{
for (int i = 0; i < result.Count; i++)
{
Console.Write(result[i] + " ");
}
Console.WriteLine("");
}
}
@meekg33k
Copy link

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

This is a good attempt at coming up with a solution for Apex College, and your solution passes most of the test cases. There is one test case that it didn't seem to account for:

  • When val is greater than the size of the nums array; in such cases, your solution returns the nums. For example, ShufflePupils({2, 3}, 3); // yours would return {2, 3} instead of {3, 2}. The expectation is that when val is greater than the nums, you should think of it as shuffling the entire pupils list for as many times as is possible until val becomes less than the size of nums. In mathematical terms, that would be val = val % nums.length.

  • Also your solution should return the modified list not print it out. That would mean ShufflePupils should return an int[] so the function signature should be public static int[] ShufflePupils(int[] nums, int val)

I understand part of this solution was made with some assumptions of how certain things should be implemented. I think it's always best to check in with your interviewer before making assumptions on how edge cases should be implemented. I wrote about that in this article, you might find it helpful.

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

@Ezeji
Copy link
Author

Ezeji commented May 17, 2021

Thank you so much @meekg33k for your detailed feedback, it means a lot to me and I appreciate.

All your points raised are noted with thanks.

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