🛠 1. Two Sum Problem (HashMap)
Write a method that takes an integer array and a target sum. Return the indices of the two numbers that add up to the target.
📌 Use a Dictionary (HashMap) for an optimal solution.
csharp
public int[] TwoSum(int[] nums, int target) {
Dictionary numMap = new Dictionary();
for (int i = 0; i < nums.Length; i++) {
int diff = target - nums[i];
if (numMap.ContainsKey(diff)) {
return new int[] { numMap[diff], i };
}
numMap[nums[i]] = i;
}
return new int[0]; // No solution
}
🔹 Practice: Try implementing this using nested loops (brute force) and compare performance.
📚 2. Reverse a Linked List
Write a function to reverse a singly linked list.
📌 Understand how pointers work in linked lists.
csharp
public ListNode ReverseList(ListNode head) {
ListNode prev = null, curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
🔹 Practice: Implement recursively as well.
🔍 3. Find the First Non-Repeating Character in a String
Write a method that returns the first character in a string that does not repeat.
📌 Use a dictionary for frequency counting.
csharp
public char FirstNonRepeatingChar(string s) {
Dictionary count = new Dictionary();
foreach (char c in s) count[c] = count.GetValueOrDefault(c, 0) + 1;
foreach (char c in s) if (count[c] == 1) return c;
return '_'; // No non-repeating character
}
🔹 Practice: Handle uppercase/lowercase sensitivity.
📈 4. FizzBuzz (Classic)
Print numbers from 1 to n.
If divisible by 3, print "Fizz".
If divisible by 5, print "Buzz".
If divisible by both, print "FizzBuzz".
csharp
public void FizzBuzz(int n) {
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) Console.WriteLine("FizzBuzz");
else if (i % 3 == 0) Console.WriteLine("Fizz");
else if (i % 5 == 0) Console.WriteLine("Buzz");
else Console.WriteLine(i);
}
}
🔹 Practice: Solve this using a switch expression in C#.
💾 5. Implement a Stack Using Two Queues
Implement a stack (LIFO) using only two queues (FIFO).
📌 Understand how stacks & queues differ.
csharp
using System.Collections.Generic;
public class MyStack {
private Queue q1 = new Queue();
private Queue q2 = new Queue();
public void Push(int x) {
q2.Enqueue(x);
while (q1.Count > 0) q2.Enqueue(q1.Dequeue());
var temp = q1;
q1 = q2;
q2 = temp;
}
public int Pop() => q1.Dequeue();
public int Top() => q1.Peek();
public bool Empty() => q1.Count == 0;
}
🔹 Practice: Implement using a single queue.
🌳 6. Binary Search Tree: Find Depth
Write a function to find the depth of a binary tree.
csharp
public int MaxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.Max(MaxDepth(root.left), MaxDepth(root.right));
}
🔹 Practice: Implement an iterative BFS solution.
🧠 7. OOP: Design a Parking System
Design a Parking System with the following classes:
ParkingSystem:
AddCar(int carType): Checks if there's space for a car.
Uses big, medium, and small slots.
csharp
public class ParkingSystem {
private int[] slots;
public ParkingSystem(int big, int medium, int small) {
slots = new int[] { big, medium, small };
}
public bool AddCar(int carType) {
if (slots[carType - 1] > 0) {
slots[carType - 1]--;
return true;
}
return false;
}
}
🔹 Practice: Extend with dynamic pricing.
⚡ 8. Implement an LRU Cache
Implement a Least Recently Used (LRU) Cache using a Dictionary + LinkedList.
csharp
using System.Collections.Generic;
public class LRUCache {
private int capacity;
private Dictionary> cache;
private LinkedList<(int key, int value)> order;
public LRUCache(int capacity) {
this.capacity = capacity;
cache = new Dictionary>();
order = new LinkedList<(int key, int value)>();
}
public int Get(int key) {
if (!cache.ContainsKey(key)) return -1;
var node = cache[key];
order.Remove(node);
order.AddFirst(node);
return node.Value.value;
}
public void Put(int key, int value) {
if (cache.ContainsKey(key)) order.Remove(cache[key]);
if (cache.Count == capacity) {
cache.Remove(order.Last.Value.key);
order.RemoveLast();
}
order.AddFirst((key, value));
cache[key] = order.First;
}
}
🔹 Practice: Extend it with time-based expiry.
🔢 9. Count Distinct Elements in an Array
Count the number of distinct elements in an array. 📌 Use a HashSet for efficiency.
csharp
public int CountDistinct(int[] nums) {
return new HashSet(nums).Count;
}
🔹 Practice: Do it without HashSet.
🔄 10. Rotate an Array
Write a function to rotate an array k times to the right.
📌 Use an efficient approach (O(n) time, O(1) space).
csharp
public void Rotate(int[] nums, int k) {
k %= nums.Length;
Array.Reverse(nums, 0, nums.Length);
Array.Reverse(nums, 0, k);
Array.Reverse(nums, k, nums.Length - k);
}
🔹 Practice: Solve without modifying the original array.
Next Steps
🔹 Time yourself while solving these.
🔹 Use unit tests to verify your solutions.
🔹 Try solving in multiple ways (brute force, optimized).