## 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

1. Hi Nikhil,

Hip Hip Hooray! I was always told that slightly slow in the head, a slow learner. Not anymore! It’s like you have my back. I can’t tell you how much I’ve learnt here and how easily! Thank you for blessing me with this effortlessly ingestible digestible content.
Do you know something about that? is it coming? is going to progress? A friend of mine told me but I didn't understand very well.
There can be many reasons due to which application can be taking too long, however one reason can be wrong configuration of Xms. For instance, if Xms value is too high, close to physical memory (RAM) than pagination of memory will start happening, resulting in slowness of application.
THANK YOU!! This saved my butt today, I’m immensely grateful.

Kevin

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

3. Nice work dude....

1. Hi There,

Amaze! I have been looking bing for hours because of this and i also in the end think it is in this article! Maybe I recommend you something helps me all the time?

have created a Cache connection manager to store 20GB of data. My Physical memory on the server is 1.5TB and whey I run the package, it is failed at Cache connection with the following error.
Deactivate rollback segment R0 and activate the newly created rollback segments.
Error: The system reports 12 percent memory load. There are 1649153396736 bytes of physical memory with 1446557532160 bytes free. There are 4294836224 bytes of virtual memory with 39915520 bytes free. The paging file has 1711430443008 bytes with 1493163847680 bytes free.
Error: A buffer failed while allocating 14679520 bytes.
When it caches data, where is it stored? Physical Memory or Virtual Memory??
How to change it to store in Physical Memory?

I am so grateful for your blog. Really looking forward to read more.

Cheers,
Morgan

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/

10. Awesome..Thanks