Please navigate to the bottom of the page for Table of Contents

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
*/

10 comments:

  1. Replies
    1. I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts. Python Projects for Students Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account. Project Center in Chennai

      Delete
  2. I'm back to this nice article, Thanks for sharing and keep sharing.
    erp software in chennai | cloud erp in chennai

    ReplyDelete
  3. Hi Nikhil,

    I love all the posts, I really enjoyed.
    I would like more information about this, because it is very nice., Thanks for sharing.
    I’m using log4j, in a program there are different inputs, in each thread.
    I had added logging in program.
    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.

    Obrigado,
    Hima

    ReplyDelete
  4. I have read your blog its very attractive and impressive. I like it your blog.
    kajal agarwal hot

    ReplyDelete
  5. This comment has been removed by the author.

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

    ReplyDelete
  7. I think this is a charming issue, I expect you would surely post on it again sometime near the future. Thanks guys!
    UI design companies

    ReplyDelete