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

11. This comment has been removed by the author.

12. Thanks for the great post! Your QAs have been added to the www.fullstack.cafe portal and back-linked!

13. SVR Technologies provide Mulesoft Training with Mulesoft Video Tutorials, Live Project, Practicals - Realtime scenarios, CV, Interview and Certification Guidance.

SVR Technologies MuleSoft training is designed according to the latest features of Mule 4.It will enable you to gain in-depth knowledge on concepts of Anypoint Studio Integration techniques, testing and debugging of Mule applications, deploying and managing the Mule applications on the cloud hub, dataweave transformations, etc. You will also get an opportunity to work on two real-time projects under the guidance of skilled trainers during this training.

Enquire Now: +91 9885022027
Enroll Now: https://bit.ly/2OCYVgv

https://svrtechnologies.com/contact-us/

Angular Training,
AWS Training Online,
Best Online Training,
Devops Training,
Machine Learning Training,
Mulesoft Training,
Online Training Institute,
Python Training,
Salesforce Training,
SAP Training,
Tableau Training,
Tibco Training

14. Thanks for providing such a useful article containing valuable information. start learning the best online software courses.

Workday Online Training

15. Thank you for sharing such a nice and interesting blog with us. I have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information.

Workday Online Training

16. It's Incredibly grand. Really very helpful article , Thank you for sharing

Workday HCM Online Training

17. An overwhelming web journal I visit this blog, it's unfathomably amazing. Unusually, in this present blog's substance made inspiration driving truth and reasonable. The substance of data is enlightening
Oracle Fusion Financials Online Training
Oracle Fusion HCM Online Training
Oracle Fusion SCM Online Training

18. Descriptive Questions are always helpful to clear a job Interview. I am a programmer and I know how hard is to clear a job interview. That why I created Online Interview questions

19. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
workday studio online training

20. Useful information,don't stop sharing and Please keep updating us..... Thanks
wordpress interview questions

21. Nice post , thanks
Interview Queries

22. Thanks for sharing such a great information. Its really nice and informative kanban training.

23. Hi ,thanks for this awesome post . check Sql query questions asked in interviews

24. Nice blog to read, Thanks for sharing this valuable article.

Best Apache Spark Online Course
Apache Spark and Scala Online Training

25. nice Post thanks for the information, good information & very helpful for others.
Sailpoint Interview Questions
Oracle Fusion HCM Training
Workday Training