## Wednesday, June 15, 2011

### LINQ interview questions

In this post we will review a bunch of small and simple LINQ interview questions to get you started. Once you understand these standard query expressions, it should be a breeze for you to answer LINQ questions.

Question: Given an array of numbers, find if ALL numbers are a multiple of a provided number. For example, all of the following numbers - 30, 27, 15, 90, 99, 42, 75 are multiples of 3.

The trick here is to use the Enumerable.All<TSource> method which determines whether all elements of a sequence satisfy a condition.

`static void Main(string[] args){    int[] numbers = { 30, 27, 15, 90, 99, 42, 75 };    bool isMultiple = MultipleTester(numbers, 3);}private static bool MultipleTester(int[] numbers, int divisor){    bool isMultiple =        numbers.All(number => number % divisor == 0);        return isMultiple;}`

Question: Given an array of numbers, find if ANY of the number is divisible by 5. For example, one of the following numbers - 30, 27, 18, 92, 99, 42, 72 is divisible by 5.

Again, the key fact here is to use Enumerable.Any<TSource> method which determines whether any element of a sequence satisfies a condition. The code is very similar to the ALL case.

`static void Main(string[] args){    int[] numbers = { 30, 27, 18, 92, 99, 42, 72 };            bool isDivisible = IsDivisible(numbers, 3);}private static bool IsDivisible(int[] numbers, int divisor){    bool isDivisible =        numbers.Any(number => number % divisor == 0);        return isDivisible;}`

Another way to present a similar question that utilizes Enumerable.Any<TSource> method is shown below.

Question: Given a GPSManufacturer and GPSDevice data structure as defined below, find all the manufacturers that have at least 1 or more active GPS devices.

`class GpsDevice{    public string Name;    public bool IsActive;}    class GpsManufacturer{    public string Name;    public GpsDevice[] Devices;}    static List<GpsManufacturer> gpsManufacturers =     new List<GpsManufacturer>     {        new GpsManufacturer            {Name = "manf1",             Devices = new GpsDevice[]             {                new GpsDevice {Name = "device1", IsActive = true},                new GpsDevice {Name = "device2", IsActive = false}            }        },        new GpsManufacturer            {Name = "manf2",             Devices = new GpsDevice[]             {                new GpsDevice {Name = "device1", IsActive = false},                new GpsDevice {Name = "device2", IsActive = false}            }        },        new GpsManufacturer         {Name = "manf3"}    };`

The following function finds out the active GPS manufacturers.

`        public static void ActiveGpsManufacturers()        {            // Determine which manufacturers have a active device.            IEnumerable<string> activeManf =                from manf in gpsManufacturers // for all manufacturers                where manf.Devices != null && // they have a list of devices                    // and at least one of the device is active                      manf.Devices.Any(m => m.IsActive == true)                select manf.Name; // select them                            // just for debugging            foreach (string name in activeManf)                Console.WriteLine(name);        }`

As an extension question and using Lambda expressions, consider the following question:

Question: Find the count of all the manufacturers that have at least 1 or more active GPS devices.

`public static void ActiveGpsManufacturersCount(){    // to count the number of active manufacturers    int activeManfCount =        gpsManufacturers.Count            (manf => (manf.Devices != null &&                      manf.Devices.Any(m => m.IsActive == true)));    // debugging    Console.WriteLine("Active Manf count: {0}", activeManfCount);}`
MSDN has extensive documentation on LINQ. I recommend reading about the various functions and then trying out various queries on your own. LINQPad is an excellent tool to learn also.

1. nice piece of work

2. thanks for the nice questions. http://prasadaknair.blogspot.com

3. Nice work dude....

4. Thanks, looking more linq interview questions.

5. Gorgeous man that did the coding

6. thanks a lot .....

7. Realy great effort Thanks

8. Nice post Nikhil. Keep at it. You are doing a great favour to plenty of programmers around the world.

9. thanks for the nice questions..

http://www.sqlaspnet.com/interview/LINQ/