Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: To implement a custom collection: Derive from existing base classes like Collection<T>, List<T>, or implement interfaces such as ICollection<T>, IEnumerable<T>, or IList<T>. Ex…
Short answer: Use the Stopwatch class from System.Diagnostics to time operations accurately. Profile your code using tools like Visual Studio Profiler, dotTrace, or PerfView for deeper insights. Measure specific operatio…
Short answer: Collection initializers allow you to create and populate a collection in a concise way at the time of declaration. Example: List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; Example code Dict…
Short answer: List<string> fruits = new List<string> { "Apple", "Banana" }; foreach (var fruit in fruits) { Console.WriteLine(fruit); } Example code List<string> fruits = new List<…
Short answer: Internally, Queue<T> uses a circular array to efficiently manage memory and operations. Head pointer marks the front (next item to be dequeued). Tail pointer marks where the next item will be enqueued…
Short answer: List<T> uses a contiguous array internally, so memory is compact and cache-friendly. LinkedList<T> stores elements in nodes with extra pointers (Next and Previous), leading to more memory overhe…
Short answer: Feature ConcurrentQueue<T> Queue<T> Thread safety Designed for concurrent access Not thread-safe; requires locks Locking mechanism Internal lock-free or fine-grained locking No internal synchron…
Short answer: LINQ itself doesn’t modify collections directly but produces new collections based on queries. You typically combine LINQ with collection methods to add elements, for example: var evenNumbers = new List<…
Short answer: Feature SortedSet<T> HashSet<T> Ordering Maintains sorted order No guaranteed order Implementation Balanced binary search tree Hash table Lookup complexity O(log n) O(1) average Memory overhead…
Short answer: Feature SortedList<TKey, TValue> Dictionary<TKey, TValue> Order Maintains keys in sorted order No guaranteed order Internal storage Uses two arrays (keys & values) Uses a hash table Lookup c…
Short answer: public void RotateMatrix(int[][] matrix) { int n = matrix.Length; // Transpose for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; mat…
Short answer: Follow on: List<string> GenerateParenthesis(int n) if (open < max) Generate(current + "(", open + 1, close, max, result); if (close < open) Generate(current + ")", open, clos…
Short answer: long LargestPrimeFactor(long n) { Example code long maxPrime = -1; while (n % 2 == 0) { maxPrime = 2; n /= 2; } for (long i = 3; i * i <= n; i += 2) { while (n % i == 0) { maxPrime = i; n /= i; } } if (n…
Short answer: Integers int FindMissingNumber(int[] nums) { int low = 0, high = nums.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] == mid) low = mid + 1; else high = mid - 1; } retur…
Short answer: bool CanPartition(int[] nums) { int sum = nums.Sum(); Follow on: if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target + 1]; dp[0] = true; foreach (int num in nums) { for (int j…
Short answer: bool IsPalindrome(string str) { int left = 0, right = str.Length - 1; while (left < right) { if (str[left] != str[right]) return false; left++; right--; } return true; } Example code bool IsPalindrome(st…
Short answer: int[] arr = { 3, 5, 1, 5, 2 }; HashSet<int> set = new HashSet<int>(); foreach (int num in arr) { if (!set.Add(num)) { Console.WriteLine(num); break; } } Example code int[] arr = { 3, 5, 1, 5, 2…
Short answer: int[] a = { 1, 3, 5 }; int[] b = { 2, 4, 6 }; int i = 0, j = 0; List<int> result = new List<int>(); while (i < a.Length && j < b.Length) result.Add(a[i] < b[j] ? a[i++] : b[j++]…
Short answer: string sentence = "CSharp makes backend development powerful"; string[] words = sentence.Split(' '); string longest = words[0]; foreach (string word in words) { if (word.Length > longest.Length…
Short answer: List<object> list = new List<object> { 1, new List<int> { 2, 3 }, 4 }; List<int> result = new List<int>(); void Flatten(List<object> input) { foreach (var item in input)…
Short answer: int[] arr = { 5, 1, 4, 2 }; for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; }…
Short answer: rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } Real-world example (ShopNest)…
Short answer: string input = "DotNet"; Dictionary<char, int> map = new Dictionary<char, int>(); foreach (char c in input.ToLower()) map[c] = map.ContainsKey(c) ? map[c] + 1 : 1; Example code string…
Short answer: bool IsSorted(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] > arr[i + 1]) return false; } return true; } Example code bool IsSorted(int[] arr) { for (int i = 0; i < arr.Length…
Short answer: string result = sb.ToString(); Final Notes These questions frequently appear in L1/L2 interviews They test logic clarity, memory, and problem-solving Ideal for LinkedIn posts, reels, ebooks, and interviews…
C# Collections C# Programming Tutorial · Collections
Short answer: To implement a custom collection: Derive from existing base classes like Collection<T>, List<T>, or implement interfaces such as ICollection<T>, IEnumerable<T>, or IList<T>.
Override or implement necessary methods like Add(), Remove(), GetEnumerator(), and indexers. Provide custom behavior, validation, or constraints as needed. Example: public class MyCustomCollection<T> : Collection<T> { protected override void InsertItem(int index, T item) { // Custom validation if (item == null) throw new ArgumentNullException(nameof(item)); base.InsertItem(index, item); }
In ShopNest checkout, choose the collection by need: List for ordered cart lines, Dictionary for fast SKU lookup, HashSet for unique tags.
C# Collections C# Programming Tutorial · Collections
Short answer: Use the Stopwatch class from System.Diagnostics to time operations accurately. Profile your code using tools like Visual Studio Profiler, dotTrace, or PerfView for deeper insights. Measure specific operations like add, remove, search, or iteration by running them multiple times and averaging results. Example: var stopwatch = Stopwatch.StartNew();
list.Add(1000); stopwatch.Stop(); Console.WriteLine($"Add operation took {stopwatch.ElapsedTicks} ticks");
In ShopNest checkout, choose the collection by need: List for ordered cart lines, Dictionary for fast SKU lookup, HashSet for unique tags.
C# Collections C# Programming Tutorial · Collections
Short answer: Collection initializers allow you to create and populate a collection in a concise way at the time of declaration. Example: List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
Dictionary<string, int> ages = new Dictionary<string, int>
{ { "Alice", 30 }, { "Bob", 25 } }; This syntax internally calls the collection’s Add() method for each element.
In ShopNest checkout, choose the collection by need: List for ordered cart lines, Dictionary for fast SKU lookup, HashSet for unique tags.
C# Collections C# Programming Tutorial · Collections
Short answer: List<string> fruits = new List<string> { "Apple", "Banana" }; foreach (var fruit in fruits) { Console.WriteLine(fruit); }
List<string> fruits = new List<string> { "Apple", "Banana" };
foreach (var fruit in fruits)
{ Console.WriteLine(fruit); }
In ShopNest checkout, choose the collection by need: List for ordered cart lines, Dictionary for fast SKU lookup, HashSet for unique tags.
C# Collections C# Programming Tutorial · Collections
Short answer: Internally, Queue<T> uses a circular array to efficiently manage memory and operations. Head pointer marks the front (next item to be dequeued). Tail pointer marks where the next item will be enqueued. Automatically resizes when capacity is exceeded. This implementation ensures constant time operations for enqueue and dequeue.
ShopNest uses a Queue<Order> for “orders waiting for payment confirmation,” and a Stack<Uri> for back-navigation in the admin UI.
C# Collections C# Programming Tutorial · Collections
Short answer: List<T> uses a contiguous array internally, so memory is compact and cache-friendly. LinkedList<T> stores elements in nodes with extra pointers (Next and Previous), leading to more memory overhead. Therefore, List<T> generally has lower memory usage and better cache performance than LinkedList<T>, especially for large collections.
In ShopNest, an order page loads products with List<Product> so you can Add items to the cart and access them by index. Use IEnumerable<T> when you only need to loop (for example, printing invoice lines).
C# Collections C# Programming Tutorial · Collections
Short answer: Feature ConcurrentQueue<T> Queue<T> Thread safety Designed for concurrent access Not thread-safe; requires locks Locking mechanism Internal lock-free or fine-grained locking No internal synchronization Suitable for Multi-threaded producer-consumer patterns Single-threaded scenarios or external synchronization ConcurrentQueue<T> allows safe enqueueing and dequeueing by multiple threads simultaneously without…
C# Collections C# Programming Tutorial · Collections
Short answer: LINQ itself doesn’t modify collections directly but produces new collections based on queries. You typically combine LINQ with collection methods to add elements, for example: var evenNumbers = new List<int> { 2, 4, 6 };
var allNumbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var combined = allNumbers.Where(n => n % 2 == 0).ToList(); // Filters even numbers If you want to add LINQ results to a collection:
List<int> filteredNumbers = allNumbers.Where(n => n % 2 == 0).ToList();
C# Collections C# Programming Tutorial · Collections
Short answer: Feature SortedSet<T> HashSet<T> Ordering Maintains sorted order No guaranteed order Implementation Balanced binary search tree Hash table Lookup complexity O(log n) O(1) average Memory overhead Higher (tree nodes) Lower (hash buckets) Use case When sorted data or range queries needed Fast insertion and lookup without ordering
C# Collections C# Programming Tutorial · Collections
Short answer: Feature SortedList<TKey, TValue> Dictionary<TKey, TValue> Order Maintains keys in sorted order No guaranteed order Internal storage Uses two arrays (keys & values) Uses a hash table Lookup complexity O(log n) (binary search) O(1) average Insertion complexity O(n) (due to shifting elements) O(1) average Memory overhead Lower (arrays) Higher (hash buckets, overhead)
In ShopNest, an order page loads products with List<Product> so you can Add items to the cart and access them by index. Use IEnumerable<T> when you only need to loop (for example, printing invoice lines).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: public void RotateMatrix(int[][] matrix) { int n = matrix.Length; // Transpose for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } } // Reverse each row for (int i = 0; i < n; i++) { Array.Reverse(matrix[i]); } } Explanation: Transpose matrix and then reverse each row to rotate 90° clockwise.
public void RotateMatrix(int[][] matrix) {
int n = matrix.Length; // Transpose for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
} // Reverse each row for (int i = 0; i < n; i++) { Array.Reverse(matrix[i]); }
} Explanation: Transpose matrix and then reverse each row to rotate 90° clockwise.
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: Follow on: List<string> GenerateParenthesis(int n) if (open < max) Generate(current + "(", open + 1, close, max, result); if (close < open) Generate(current + ")", open, close + 1, max, result); } Explanation: Use backtracking to add '(' and ')' only when valid.
{
List<string> result = new List<string>(); Generate("", 0, 0, n, result); return result;
} void Generate(string current, int open, int close, int max, List<string> result)
{
if (current.Length == max * 2)
{ result.Add(current); return;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: long LargestPrimeFactor(long n) {
long maxPrime = -1; while (n % 2 == 0) {
maxPrime = 2;
n /= 2;
}
for (long i = 3; i * i <= n; i += 2)
{ while (n % i == 0) {
maxPrime = i;
n /= i;
}
}
if (n > 2) maxPrime = n;
return maxPrime;
} Follow on: Explanation: Divide out factors of 2, then test odd factors; leftover > 2 is prime. Miscellaneous Problems
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: Integers int FindMissingNumber(int[] nums) { int low = 0, high = nums.Length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (nums[mid] == mid) low = mid + 1; else high = mid - 1; } return low; } Explanation: In perfect array nums[i] == i; missing number breaks this property, use binary search to find breakpoint. Mathematical Problems
Integers
int FindMissingNumber(int[] nums)
{
int low = 0, high = nums.Length - 1; while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == mid)
low = mid + 1; else high = mid - 1;
}
return low;
} Explanation: In perfect array nums[i] == i; missing number breaks this property, use binary search to find breakpoint. Mathematical Problems
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding
Short answer: bool CanPartition(int[] nums) { int sum = nums.Sum(); Follow on: if (sum % 2 != 0) return false; int target = sum / 2; bool[] dp = new bool[target + 1]; dp[0] = true; foreach (int num in nums) { for (int j = target; j >= num; j--) { dp[j] = dp[j] || dp[j - num]; } } return dp[target]; } Explanation: Subset sum to check if half the total sum is achievable. Sorting and Searching
bool CanPartition(int[] nums) {
int sum = nums.Sum(); Follow on: if (sum % 2 != 0) return false;
int target = sum / 2;
bool[] dp = new bool[target + 1];
dp[0] = true;
foreach (int num in nums)
{
for (int j = target; j >= num; j--)
{
dp[j] = dp[j] || dp[j - num];
}
}
return dp[target];
} Explanation: Subset sum to check if half the total sum is achievable. Sorting and Searching
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: bool IsPalindrome(string str) { int left = 0, right = str.Length - 1; while (left < right) { if (str[left] != str[right]) return false; left++; right--; } return true; }
bool IsPalindrome(string str) {
int left = 0, right = str.Length - 1; while (left < right) {
if (str[left] != str[right])
return false; left++; right--; }
return true;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: int[] arr = { 3, 5, 1, 5, 2 }; HashSet<int> set = new HashSet<int>(); foreach (int num in arr) { if (!set.Add(num)) { Console.WriteLine(num); break; } }
int[] arr = { 3, 5, 1, 5, 2 };
HashSet<int> set = new HashSet<int>();
foreach (int num in arr)
{
if (!set.Add(num))
{ Console.WriteLine(num); break; }
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: int[] a = { 1, 3, 5 }; int[] b = { 2, 4, 6 }; int i = 0, j = 0; List<int> result = new List<int>(); while (i < a.Length && j < b.Length) result.Add(a[i] < b[j] ? a[i++] : b[j++]); while (i < a.Length) result.Add(a[i++]); while (j < b.Length) result.Add(b[j++]);
int[] a = { 1, 3, 5 };
int[] b = { 2, 4, 6 };
int i = 0, j = 0;
List<int> result = new List<int>(); while (i < a.Length && j < b.Length) result.Add(a[i] < b[j] ? a[i++] : b[j++]); while (i < a.Length) result.Add(a[i++]); while (j < b.Length) result.Add(b[j++]);
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: string sentence = "CSharp makes backend development powerful"; string[] words = sentence.Split(' '); string longest = words[0]; foreach (string word in words) { if (word.Length > longest.Length) longest = word; }
string sentence = "CSharp makes backend development powerful";
string[] words = sentence.Split(' ');
string longest = words[0];
foreach (string word in words)
{
if (word.Length > longest.Length)
longest = word;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: List<object> list = new List<object> { 1, new List<int> { 2, 3 }, 4 }; List<int> result = new List<int>(); void Flatten(List<object> input) { foreach (var item in input) { if (item is int) result.Add((int)item); else Flatten((List<object>)item); } }
List<object> list = new List<object> { 1, new List<int> { 2, 3 }, 4 }; List<int> result = new List<int>(); void Flatten(List<object> input) {
foreach (var item in input)
{
if (item is int) result.Add((int)item); else Flatten((List<object>)item); }
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: int[] arr = { 5, 1, 4, 2 }; for (int i = 0; i < arr.Length; i++) { for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } }
int[] arr = { 5, 1, 4, 2 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } } rr[j] = arr[j + 1]; rr[j + 1] = temp; } } }
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: string input = "DotNet"; Dictionary<char, int> map = new Dictionary<char, int>(); foreach (char c in input.ToLower()) map[c] = map.ContainsKey(c) ? map[c] + 1 : 1;
string input = "DotNet";
Dictionary<char, int> map = new Dictionary<char, int>();
foreach (char c in input.ToLower())
map[c] = map.ContainsKey(c) ? map[c] + 1 : 1;
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: bool IsSorted(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] > arr[i + 1]) return false; } return true; }
bool IsSorted(int[] arr) {
for (int i = 0; i < arr.Length - 1; i++)
{
if (arr[i] > arr[i + 1])
return false;
}
return true;
}
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
C# Coding Interview C# Programming Tutorial · Coding Scenarios
Short answer: string result = sb.ToString(); Final Notes These questions frequently appear in L1/L2 interviews They test logic clarity, memory, and problem-solving Ideal for LinkedIn posts, reels, ebooks, and interviews
string input = "Dot@Net#2024!";
StringBuilder sb = new StringBuilder();
foreach (char c in input)
{
if (char.IsLetterOrDigit(c)) sb.Append(c); }
In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).
Install Toolliyo like an app Free
Home-screen access to tutorials, coding practice & career tools — no app store needed.
On iPhone/iPad: tap Share then Add to Home Screen.