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  :)


  1. Нello theгe I am so delighted I found
    yοur wеbsite, I rеаlly founԁ уou bу error,
    while I was гeseaгching on Bіng foг ѕomethіng
    else, Αnyhow І am heгe now anԁ would juѕt likе to ѕаy thanks for a tremendouѕ post and a all rοund thrilling blοg (I
    alѕo love the theme/design), I don't have time to read through it all at the minute but I have saved it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.
    Also visit my site

    1. Thanks for so much appreciation. I'm highly encouraged!


What are your thoughts on this post? Did you like it?