People really ask interesting questions on Quora. However this one got my attention and I thought of getting involved. The primary motivation was more like trying to figure out if global questions asked can be answered even with local implementations.  This was consistent with a challenge given by Bitange Ndemo in a conference in 2014 at the PWC towers in westlands, Nairobi. I wrote all about the conference in this post.

Back to Quora, the question is about if there are programmer who cant pass the FizzBuzz test. The specific question asked is  given below and I took it upon myself to write an implementation of a slightly different(somewhat harder) solution in C#. Definitely this implementation does not give much thought the algorithm complexity but I bet that’s what the answers in Quora try to address; solving simple programming problems.

Given a list of people with their birth and end years (all between 1900 and 2000), find the year with the most number of people alive.

Complete code.

class Program
    {
        static void Main(string[] args)
        {
           int yearWithMostalive = 1900;
           int highestAlive = 0;
           var listOfPeople = new PeopleTimes().GetPeople();

           for (int i = 1900; i <= 2000; i++)
           {
               int totalAliveinYear = 0;
               foreach (var person in listOfPeople)
               {
                   if (i >= person.Birth && i <= person.Gone)
                       totalAliveinYear = totalAliveinYear + 1;
               }

               if (totalAliveinYear > highestAlive)
               {
                   highestAlive = totalAliveinYear;
                   yearWithMostalive = i;
               }
           }
           Console.Write("Highest alive is {0} in the year {1}", highestAlive, yearWithMostalive);
           Console.ReadLine();
        }
    }

    public class PeopleTimes
    {
        public int Birth { get; set; }
        public int Gone { get; set; }

        public PeopleTimes()
        {

        }

        public PeopleTimes(int b, int g)
        {
            this.Birth = b;
            this.Gone = g;
        }

        public List<PeopleTimes> GetPeople()
        {
            var person1 = new PeopleTimes(1991, 1999);
            var person2 = new PeopleTimes(1954, 1996);
            var person3 = new PeopleTimes(1934, 1970);
            var person4 = new PeopleTimes(1971, 1997);
            var person5 = new PeopleTimes(1967, 2000);

            var list = new List<PeopleTimes>();
            list.Add(person1);
            list.Add(person2);
            list.Add(person3);
            list.Add(person4);
            list.Add(person5);

            return list;
        }
    }

In the next post I might be inspired or not, to write an implementation of the actual FizzBuzz test.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *