Thursday, June 16, 2011

LINQ SKIP and TAKE interview questions

In this post we will review a few LINQ interview questions related to selecting for the result set.

Question: Given a list of student grades, skip the top 3 grades and find the remaining.

For this question, we will use the SKIP method. SKIP bypasses a specified number of elements in a sequence and then returns the remaining elements.

`int[] grades = { 59, 82, 70, 56, 92, 98, 85 }; IEnumerable<int> lowerGrades =     grades.OrderByDescending(g => g).Skip(3);     Console.WriteLine("All grades except the top three are:"); foreach (int grade in lowerGrades) {     Console.WriteLine(grade); } /*  This code produces the following output:  All grades except the top three are:  82  70  59  56 */`

Question: Find out all students who have a grade value of less than 80.

This question is slightly different from the above in the sense that we do not know how many grades to skip. In this case, we will use the SKIPWHILE method. It bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements.

`int[] grades = { 59, 82, 70, 56, 92, 98, 85 };IEnumerable<int> lowerGrades =    grades    .OrderByDescending(grade => grade)    .SkipWhile(grade => grade >= 80);    Console.WriteLine("All grades below 80:");foreach (int grade in lowerGrades){    Console.WriteLine(grade);}/* This code produces the following output: All grades below 80: 70 59 56*/`

There is another variation of SkipWhile that takes into account the index position of the element in the sequence. It bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function.

Question: For a given integer sequence, skip elements as long as they are 1000 times more than their position in the sequence.

`int[] amounts = { 5000, 2500, 9000, 8000,                     6500, 4000, 1500, 5500 };    IEnumerable<int> query =    amounts.SkipWhile((amount, index) => amount > index * 1000);    foreach (int amount in query){    Console.WriteLine(amount);}/* This code produces the following output: 4000 1500 5500*/`

SKIP and TAKE functions are exact opposites of each other.

Question: Find the top 3 grades.

To solve this question, we will first sort our sequence in descending order and then take the top 3 elements.

`int[] grades = { 59, 82, 70, 56, 92, 98, 85 };    IEnumerable<int> topThreeGrades =    grades.OrderByDescending(grade => grade).Take(3);    Console.WriteLine("The top three grades are:");foreach (int grade in topThreeGrades){    Console.WriteLine(grade);}    /* This code produces the following output: The top three grades are: 98 92 85*/`

Question: In a given sequence of fruit names, find all the fruits that come before orange.

This question requires us to use the TakeWhile function which returns elements from a sequence as long as a specified condition is true.

`string[] fruits = { "apple", "banana", "mango", "orange",                        "passionfruit", "grape" };     IEnumerable<string> query =     fruits.TakeWhile(fruit => String.Compare("orange", fruit, true) != 0);     foreach (string fruit in query) {     Console.WriteLine(fruit); }     /*  This code produces the following output:  apple  banana  mango */`

Another variation of TakeWhile (similar to SkipWhile) takes into account the position of the element in the sequence. Let’s modify our above question to include this new twist. It returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function.

Question: Find all the fruits in a sequence as long as their name length is more than the position they occupy in the sequence.

Given all the questions we have been answering till now, this should be very intuitive to answer.

`string[] fruits = { "apple", "passionfruit", "banana", "mango",                       "orange", "blueberry", "grape", "strawberry" };    IEnumerable<string> query =    fruits.TakeWhile((fruit, index) => fruit.Length >= index);    foreach (string fruit in query){    Console.WriteLine(fruit);}    /* This code produces the following output: apple passionfruit banana mango orange blueberry*/`

1. Hi Nikhil,

I love all the posts, I really enjoyed.
I’m using log4j, in a program there are different inputs, in each thread.
I need log file for each thread.
how to log each thread in separate file through log4j.
These techniques are applied at JVM Level. In general, they deal in configuring various parameters that are exposed by JVM implementation. These techniques are non -invasive in nature. Some of them are pretty trivial to use and do not require any special expertise hence having low complexity and ease of identifications.
Thanks a lot. This was a perfect step-by-step guide. Don’t think it could have been done better.