The easiest way to integrate Social Login in your React Apps ...Checkout NPM

Wednesday, October 3, 2012

Coding Puzzle#1 - Generic Random Status Filler

Level: Intermediate-Advance
Topic: Plain Loops
Who Wins: One who accomplishes this task with least number of iterations
Short Description: Create a method such that it is capable of populating enum property all objects in a list with random values based on pre-defined ratio
Puzzle Details:

Let’s say I’ve a following enum and a class:
public enum TASK_STATUS

    public class TodoTask
        public int Id { get; set; }
        public string Comment { get; set; }
        public TASK_STATUS Status { get; set; }

I want to create 50 TodoTask fake instances and add it to a list List<TodoTask>(). However, I want status values to be randomly set. For example, out of 50 values,
25 should be randomly set as InProgress,
15 should be randomly set as Completed and
10 should be randomly set as ToBeStarted

Now, I need a generic method method such that I can use that method to accomplish above. I.e., method should be such that I can specify my list and ratio of enum values I want and populates the list accordingly.

Note, it should be generic as my list of  ratios may change in future or I may want to use to fill other enums. 

Tricky Part – How do you design signature of method to make it generic and also how do you manage desired ratio
Think you are a champ in loops... ahm ahm.. let's see if you get it done better and faster  :)