Took a Codility Test : Decimal Reverse, Magnitude Pole and Most Number of Occurrence.

I took a test few days ago in Codility, I say this really is a good way to validate the skill set of programmers, it shows clearly how they think and approach problem vs normal multiple choice type of question or by simply having a technical interview.

Here's my answer to the Codility questions.

Decimal Reverse:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 public static int DecimalReverse(int n)
        {
            char[] arr = n.ToString().ToCharArray();
            string reversed = string.Empty;
            List<string> list = new List<string>();

            foreach (var item in arr.Reverse())
            {
                list.Add(item.ToString());
            }

            reversed = string.Join("", list);
            return Convert.ToInt32(reversed);
        }


Magnitude Pole:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public static int MagnitudePole(int[] A)
        {
            bool flag = true;

            for (int i = 0; i < A.Length; i++)
            {
                for (int j = i-1; j >=0; j--)
                {
                    if (A[i] - A[j] < 0)
                    {
                        flag = false;
                        break;
                    }
                    else
                    {
                        flag = true;
                    }
                }

                for (int k = i + 1; k < A.Length && flag; k++)
                {
                    if ((A[k] - A[i]) < 0)
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag) return i;
            }

            return -1;
        }

Most Number of Occurence:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 public static int OccurMost(int[] A)
        {
            var occursMost = A.ToList().GroupBy(x => x)
                .Select(x => new { number = x.Key, numCount = x.Count() })
                .OrderByDescending(x => x.numCount);

            int result = Convert.ToInt32(occursMost.ToList()[0].number);

            return result;
        }

Main Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
static void Main(string[] args)
        {
            int N = 250;
            var x = DecimalReverse(N);
            Console.WriteLine("Input : " + N.ToString());
            Console.WriteLine("Decimal Reverse : " + x.ToString());
            Console.ReadLine();

            int[] numbers = new int[] {4, 2, 2, 3, 1, 4, 7, 8, 6, 9,9, 9};
           
            var z = MagnitudePole(numbers);
            Console.WriteLine("Input : " + string.Join(",",numbers));
            Console.WriteLine("Magnitude Pole : " + z.ToString());
            Console.ReadLine();

            var y = OccurMost(numbers);
            Console.WriteLine("Input : " + string.Join(",", numbers));
            Console.WriteLine("Most Occurence : " + y.ToString());
            Console.ReadLine();

        }


Output :

Comments

Popular posts from this blog

Serializing JSON string to ExpandoObject

Adding an Obsolete Attribute to Class Methods in C#