⚡ LINQ Interview Questions (Real-World Scenarios)
Here are some important LINQ-based coding problems, structured cleanly for learning, interviews, and quick revision.
🔢 Find the Second Most Frequent Number
📌 Problem
Given a list of integers:[4, 5, 6, 5, 4, 6, 6, 4, 4]
Find the second most frequent number.
✅ Solution
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var list1 = new List<int> { 4, 5, 6, 5, 4, 6, 6, 4, 4 };
var frequentNumbers = list1
.GroupBy(x => x)
.Select(s => new { Element = s.Key, Occurence = s.Count() })
.OrderByDescending(x => x.Occurence);
var secondMostFrequent = frequentNumbers.Skip(1).Take(1).ToList();
Console.WriteLine(string.Join(" ", secondMostFrequent));
Console.ReadLine();
}
}
👨💻 Filter Employees Based on Multiple Conditions
📌 Problem
Find all employees who:
Work in IT department
Have salary > 50,000
Are under 40 years old
✅ Solution
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
public string Department { get; set; }
public double Salary { get; set; }
}
class Program
{
static void Main(string[] args)
{
var employees = new List<Employee>
{
new Employee { Name = "Alice", Age = 30, Department = "IT", Salary = 56000},
new Employee { Name = "Bob", Age = 45, Department = "HR", Salary = 50000},
new Employee { Name = "Charlie", Age = 35, Department = "IT", Salary = 35000 },
new Employee { Name = "David", Age = 28, Department = "Finance", Salary = 100000 }
};
var worksInIT = employees.Where(x => x.Department == "IT").Select(x => x.Name);
var salaryGreaterThan50000 = employees.Where(x => x.Salary > 50000).Select(x => x.Name);
var ageUnder40 = employees.Where(x => x.Age < 40).Select(x => x.Name);
Console.WriteLine("Works in IT: ");
Console.WriteLine(string.Join(", ", worksInIT));
Console.WriteLine("Salary Greater Than 50000: ");
Console.WriteLine(string.Join(", ", salaryGreaterThan50000));
Console.WriteLine("Age Under 40: ");
Console.WriteLine(string.Join(", ", ageUnder40));
Console.ReadLine();
}
}
🛒 Find Top N Records (Top 3 Expensive Products)
📌 Problem
Get the top 3 most expensive products.
✅ Solution
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Product> products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1500m },
new Product { Id = 2, Name = "Smartphone", Price = 800m },
new Product { Id = 3, Name = "Tablet", Price = 600m },
new Product { Id = 4, Name = "Smartwatch", Price = 200m },
new Product { Id = 5, Name = "Headphones", Price = 300m },
new Product { Id = 6, Name = "Monitor", Price = 400m },
};
var mostExpensiveProducts = products
.OrderByDescending(x => x.Price)
.Select(x => x.Name)
.Take(3)
.ToList();
Console.WriteLine(string.Join(", ", mostExpensiveProducts));
Console.ReadLine();
}
}
🏆 Highest Salary in Each Department
employees
.GroupBy(x => x.Department)
.Select(s => s.ToList()
.OrderByDescending(x => x.Salary)
.First());
💰 Total Sales for Each Product
transactions
.GroupBy(x => x.ProductName)
.Select(x => new
{
ProductName = x.Key,
TotalSales = x.Sum(g => g.Amount)
})
.ToList();
🥈 Second Highest Salary
var result = employees
.OrderByDescending(e => e.Salary)
.Skip(1)
.Take(1);
📈 Employees Earning More Than Average Salary
var averageSalary = employees.Average(x => x.Salary);
var result = employees
.Where(x => x.Salary > averageSalary);
⏳ Employees with More Than 5 Years Experience
employees
.Where(x => x.HireDate < DateTime.Now.AddYears(-5))
.ToList();
🏅 Department with Highest Average Salary
employees
.GroupBy(x => x.Department)
.Select(s => new
{
Average = s.Average(g => g.Salary)
})
.OrderByDescending(o => o.Average)
.FirstOrDefault();
🧠 Key Takeaways
GroupBy()is essential for aggregation problems.OrderByDescending()+Skip()+Take()helps solve ranking problems.Where()is used for filtering based on conditions.Select()helps shape the output.LINQ provides a clean and readable way to handle complex data operations.
These LINQ problems are very common in .NET interviews and real-world applications, especially when working with collections and in-memory data processing.
Comments
Post a Comment