Skip to main content
3 of 4
deleted 697 characters in body; edited title
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Test questions - Number Generator, Linq, Delegates, Sequences

I have recently been asked to do a test prior to an interview to test my C# skills. I am fairly new to C# (6 months) and I think this showed with me producing an overly complex answer to their questions.

I will post the 3 questions below as well as my answers. Please can someone give me a better solution to this. I had thought about using Linq as well to do this, but again wasn't really sure how. I also tried to add delegates into the mix, but not sure if I should have done this or not. Also I wasn't sure what tests to do in my unit testing, again any quick examples would be very appreciated.

###Question 1

Write a program that prints the numbers from 1 to 100. For multiples of three print "Three" and for the multiples of five print "Five" and for numbers which are multiples of both three and five print "ThreeFive".

###Question 2

If we list all the whole numbers up to 15 that are multiples of 3 or 5, we get 3, 5, 6 and 9, 10, 12, 15. The sum of these multiples is 60. Find the sum of all the multiples of 3 or 5 up to 1000.

###Question 3

Calculate the sum of all the even numbers in the Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc.) where each number in the sequence represents the sum of the previous two, up to 4,000,000.

###Bonus Points

  • Unit tests your solutions
  • Bases the answer to 1 and 2 on the same 'number generator'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPaperApp
{
    public class TestPaperTestQuestions
    {
        public static List<string> TestPaperList = new List<string>();
        public static List<int> SumItUpList = new List<int>();
        public static List<int> FibonacciList = new List<int>();
        public static string range;
        public delegate void Question(int number);

        public static void Main(string[] args)
        {

            // Write numbers 1-100 - multiples of 3 write 'Three' multiples of 5 print ‘Five’ multiples of both print 'ThreeFive'
            QuestionOne(1, 100);
            //Sum the numbers between 1-1000 which are divisible by 3 or 5
            QuestionTwo(1, 1000);
            //Sum the even Fibonacci numbers less than 4,000,000
            QuestionThree(4000000);
        }

        public static void GenerateSequence(int startNumber, int finishNumber, Question Quest)
        {
            //Clear the lists for each time the sequence is ran
            SumItUpList.Clear();
            TestPaperList.Clear();

            //look from the start number to finish number and add the relevent values to the different lists
            for (int i = startNumber; i <= finishNumber; i++)
            {
                //Use delegate to make number generator reusable
                Quest(i);
                // get the range of the numbers you are looping through to add to the title labels
                range = startNumber.ToString() + "-" + finishNumber.ToString();
            }
        }

        public static void TestPaperListAdd(int i)
        {
            if (i % 3 == 0 && i % 5 == 0)
            {
                TestPaperList.Add("ThreeFive");
            }
            else if (i % 3 == 0)
            {
                TestPaperList.Add("Three");
            }
            else if (i % 5 == 0)
            {
                TestPaperList.Add("Five");
            }
            else
            {
                TestPaperList.Add(i.ToString());
            }
        }

        public static void SumItUpListAdd(int i)
        {
            if (i % 3 == 0 || i % 5 == 0)
            {
                SumItUpList.Add(i);
            }
        }

        public static List<String> QuestionOne(int start, int finish)
        {
            //Run the generate sequence method
            GenerateSequence(start, finish, TestPaperListAdd);
            Console.WriteLine("--- Write the numbers between " + range + " if the number is a multiples of 3 write 'Three'," +
    "a multiple of 5 print 'Five' and a multiple of both print 'ThreeFive'  ---");
            //Loop through all values and write to console window
            for (int i = 0; i < TestPaperList.Count(); i++)
            {
                Console.WriteLine(TestPaperList[i]);
            }
            return TestPaperList;
        }

        public static int QuestionTwo(int start, int finish)
        {
            //Run the generate sequence method
            GenerateSequence(start, finish, SumItUpListAdd);
            int sum = SumItUpList.Sum();
            Console.WriteLine("--- Sum of all numbers between " + range + " divisible by 3 or 5 ---");
            //Display sum of values in the console window
            Console.WriteLine(sum);
            return sum;
        }

        public static int QuestionThree(int finishNumber)
        {
            //Clear the Fibonacci list
            FibonacciList.Clear();

            //Add the first two values in the Fibonacci seequence
            FibonacciList.Add(0);
            FibonacciList.Add(1);
            //initiate twom integer values
            int highest = 0;
            int sumeven = 0;
            //loop through while the maximum Fibonacci number is less than the value selected for 'finish number'
            while (highest < finishNumber)
            {
                FibonacciList.Add(highest);
                highest = FibonacciList[FibonacciList.Count - 1] + FibonacciList[FibonacciList.Count - 2];
            }

            //Work out whether the number is even or not, if so add it to the sum
            for (int i = 0; i < FibonacciList.Count; i++)
            {
                if (FibonacciList[i] % 2 == 0)
                {
                    sumeven = sumeven + FibonacciList[i];
                }
            }
            //Write the sum of even Fibonacci numbers
            Console.WriteLine("--- Sum the even Fibonacci numbers less than " + finishNumber + " ---");
            Console.WriteLine(sumeven);

            return sumeven;
        }
    }
}
user3284707
  • 341
  • 6
  • 11